使用JAXB从xml创建具有muitiple类的对象?

时间:2021-06-28 12:04:18

I have xsd and xml file. first I have generated Java classes from xsd file ,that part has done and now I have to feed data into objects using xml ? I am using below code , but this is throwing JAXBException.

我有xsd和xml文件。首先我从xsd文件生成了Java类,该部分已经完成,现在我必须使用xml将数据提供给对象?我使用下面的代码,但这是抛出JAXBException。

    try {

    File file = new File("D:\\file.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.generated");

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Employee empObj = (Employee) jaxbUnmarshaller.unmarshal(file);
    System.out.println(empObj.getName());

  } catch (JAXBException e) {
    e.printStackTrace();
  }

and here is my xml file which contains two classes :

这是我的xml文件,其中包含两个类:

   <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     <Employee>
       <name>John</name>            
       <salary>5000</salary>
    </Employee>
    <Customer>
       <name>Smith</name>
    </Customer>

could somebody help me ?

有人能帮助我吗?

2 个解决方案

#1


3  

The XML document in your question is invalid. XML documents need to have a single root element. The first step would be to ensure that your XML document is valid against the XML schema you generated the classes from.

您问题中的XML文档无效。 XML文档需要有一个根元素。第一步是确保XML文档对您生成类的XML模式有效。

#2


3  

IMPORTANT

You've an error in your code. You skipped this step:

你的代码中有错误。您跳过此步骤:

JAXBElement element = (JAXBElement) jaxbUnmarshaller.unmarshal(f);


Well, I've worked with JAXB a long time ago.

好吧,我很久以前就和JAXB合作过。

However what we used to to in such a sitatuation, was to define an top level element (in Java code or in xsd file) enclosing the other elements.

然而,我们过去常常在这种情况下定义一个包含其他元素的*元素(在Java代码或xsd文件中)。

e.g.:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<People>
   <Employee>
      <name>John</name>            
      <salary>5000</salary>
      </Employee>
    <Customer>
      <name>Smith</name>
    </Customer>
</People>

Java will generate the classes Employee and Customer as children of People.

Java将生成Employee和Customer类作为People的子级。

You could iterate through it in JAXB code in the following way:

您可以通过以下方式在JAXB代码中迭代它:

try {
   File file = new File("D:\\file.xml");
   JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.generated");

   Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
   JAXBElement element = (JAXBElement) jaxbUnmarshaller.unmarshal(file);
   People people = (People) element.getValue();
   Employee employee = (Employee)people.getChildren().get(0); // the name of the getChildren() methodm may vary
   Customer customer = (Customer)people.getChildren().get(1);
   System.out.println(empObj.getName());
} catch (JAXBException e) {
   e.printStackTrace();
}

You may also want to take a look at this similar question: iterate-through-the-elements-in-jaxb

您可能还想看看这个类似的问题:在jaxb中迭代通过元素

#1


3  

The XML document in your question is invalid. XML documents need to have a single root element. The first step would be to ensure that your XML document is valid against the XML schema you generated the classes from.

您问题中的XML文档无效。 XML文档需要有一个根元素。第一步是确保XML文档对您生成类的XML模式有效。

#2


3  

IMPORTANT

You've an error in your code. You skipped this step:

你的代码中有错误。您跳过此步骤:

JAXBElement element = (JAXBElement) jaxbUnmarshaller.unmarshal(f);


Well, I've worked with JAXB a long time ago.

好吧,我很久以前就和JAXB合作过。

However what we used to to in such a sitatuation, was to define an top level element (in Java code or in xsd file) enclosing the other elements.

然而,我们过去常常在这种情况下定义一个包含其他元素的*元素(在Java代码或xsd文件中)。

e.g.:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<People>
   <Employee>
      <name>John</name>            
      <salary>5000</salary>
      </Employee>
    <Customer>
      <name>Smith</name>
    </Customer>
</People>

Java will generate the classes Employee and Customer as children of People.

Java将生成Employee和Customer类作为People的子级。

You could iterate through it in JAXB code in the following way:

您可以通过以下方式在JAXB代码中迭代它:

try {
   File file = new File("D:\\file.xml");
   JAXBContext jaxbContext = JAXBContext.newInstance("com.jaxb.generated");

   Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
   JAXBElement element = (JAXBElement) jaxbUnmarshaller.unmarshal(file);
   People people = (People) element.getValue();
   Employee employee = (Employee)people.getChildren().get(0); // the name of the getChildren() methodm may vary
   Customer customer = (Customer)people.getChildren().get(1);
   System.out.println(empObj.getName());
} catch (JAXBException e) {
   e.printStackTrace();
}

You may also want to take a look at this similar question: iterate-through-the-elements-in-jaxb

您可能还想看看这个类似的问题:在jaxb中迭代通过元素