简体   繁体   English

JAXB从.xml文件解组到jlist

[英]JAXB Unmarshalling from .xml file to jlist

I am very new to marshalling and unmarshalling. 我是编组和解组的新手。 I need to be able to save and load an xml file in my program. 我需要能够在程序中保存和加载xml文件。 First I will post my wrapper class. 首先,我将发布包装器课程。

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


//Class created to wrap the list of pc parts on the right Jlist. 
@XmlRootElement

public class JlistWrapper {
    private List<String> Pcpart;

    @XmlElement(name = "Pcpart")
    public List<String> getPcParts(){
    return Pcpart;
    }
    public void setPcparts(List<String> Pcpart){
        this.Pcpart = Pcpart;
    }
}

Now here is my save() method that works: 现在,这里是我的save()方法起作用了:

public static void save(File file) throws Exception{


    for (int i = 0; i < computerParts1.size(); i++){
        save.add((String)computerParts1.getElementAt(i));
    }

    //wrapping Jlist data
    JlistWrapper wrapper = new JlistWrapper();
    wrapper.setPcparts(save);

    JAXBContext c = JAXBContext.newInstance(JlistWrapper.class);
    Marshaller m = c.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(wrapper, file);   


}

For my save method, I originally had the last line set this way to just output the xml to the console. 对于我的保存方法,本来我将最后一行设置为仅将xml输出到控制台。 But I was unsure of how to unmarshall from there (any ideas on that as well?). 但是我不确定如何从那里解组(对此还有任何想法吗?)。

m.marshal(wrapper, System.out); 

Now finally, my load() method that I can't get to unmarshal correctly... 现在最后,我无法正确解组的load()方法...

public static void load(File file) throws JAXBException{
    JAXBContext context = JAXBContext.newInstance(JlistWrapper.class);
    Unmarshaller unmar = context.createUnmarshaller();

    //Read XML from file
    JlistWrapper wrapper = (JlistWrapper) unmar.unmarshal(file);
    save.clear();
    save.addAll(wrapper.getPcParts());





}

And here is the error I get when I hit the load button: 这是我按下加载按钮时遇到的错误:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.add(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at com.cooksys.assessment.Window.load(Window.java:236)
at com.cooksys.assessment.Window$2.actionPerformed(Window.java:149)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Let me know if there is anything else you need to answer my question. 让我知道您是否还需要回答我的问题。 Thanks ahead of time! 提前谢谢!

EDIT: I forgot to clarify that I need to unmarshall a list of objects to a JList in my GUI. 编辑:我忘了澄清一下,我需要在我的GUI中解组到JList的对象列表。 Sofar I am only able to save the list of objects from a JList into xml. Sofar我只能将对象列表从JList保存到xml。

You need to ensure that getPcParts doesn't return null, either by initializing the field or by doing a lazy init in the getter: 您需要通过初始化字段或在getter中进行惰性初始化来确保getPcParts不返回null:

@XmlElement(name = "Pcpart")
public List<String> getPcParts(){
  if(Pcpart == null) {
    Pcpart = new ArrayList<String>();
  }
  return Pcpart;
}

The way JAXB unmarshals lists is a bit unusual - it doesn't call the setter, instead it assumes that the getter returns a mutable list which it can add to. JAXB解组列表的方式有点不寻常-它不调用setter,而是假定getter返回一个可变列表,可以将其添加到列表中。 The unmarshaller essentially does 解组员本质上确实

theObj.getPcParts().add(theStringValueFromXml)

for each Pcpart element in the XML. XML中的每个Pcpart元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM