2

这是“PersonType”类的生成代码。

package demo;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "PersonType", propOrder = {
"name",
"address"
})
public class PersonType {

@XmlElement(name = "Name", required = true)
protected String name;
@XmlElement(name = "Address", required = true)
protected List<AddressType> address;

public String getName() {
    return name;
}


public void setName(String value) {
    this.name = value;
}


public List<AddressType> getAddress() {
    if (address == null) {
        address = new ArrayList<AddressType>();
    }
    return this.address;
}

}

这是“AddressType”类的生成代码。

    package demo;

 import javax.xml.bind.annotation.XmlAccessType;
 import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
 import javax.xml.bind.annotation.XmlSchemaType;
 import javax.xml.bind.annotation.XmlType;

 @XmlAccessorType(XmlAccessType.FIELD)
 @XmlType(name = "AddressType", propOrder = {
"number",
"street"
 })
public class AddressType {

@XmlElement(name = "Number")
@XmlSchemaType(name = "unsignedInt")
protected long number;
@XmlElement(name = "Street", required = true)
protected String street;


public long getNumber() {
    return number;
}


public void setNumber(long value) {
    this.number = value;
}


public String getStreet() {
    return street;
}

public void setStreet(String value) {
    this.street = value;
}

}

这是我要解组的 xml 文件

<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\JAXB Demo\demo.xsd">
  <Name>Sharon Krisher</Name>
  <Address>
    <Street>Iben Gevirol</Street>
    <Number>57</Number>
  </Address>
<Address>
  <Street>Moshe Sharet</Street>
   <Number>89</Number>
  </Address>
</Person>

Code foe unarchall

JAXBContext context = JAXBContext.newInstance(PersonType.class);
    Unmarshaller unmarshaller =context.createUnmarshaller();
    //unmarshaller.setValidating(true);
    PersonType person =(PersonType) unmarshaller.unmarshal(new File("C:\\Users\\sithi\\workspace\\TestJAXB\\src\\data.xml") );

这段代码给出了一个例外:

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"PersonType"). Expected elements are (none)
4

2 回答 2

2

有两种方法可以将类与根元素相关联。第一个是@XmlRootElement在类上,第二个是@XmlElementDecl在一个类上的注解@XmlRegistry(当模型是从 XML 生成时,这个类被称为 Schema ObjectFactory

创建JAXBContext

当您从 XML Schema 生成模型时,您应该JAXBContext在包名称上创建:

JAXBContext jc = JAXBContext.newInstance("demo");

或生成的ObjectFactory类:

JAXBContext jc = JAXBContext.newInstance(demo.ObjectFactory.class);

这将确保ObjectFactory该类得到处理。

没有@XmlRootElement@XmlElementDecl

如果没有@XmlRootElement@XmlElementDecl将类与文档的根元素关联,那么您将需要使用采用类参数的解组方法之一。

PersonType pt = unmarshaller.unmarshal(xml, PersonType.class).getValue();
于 2013-11-11T11:17:18.273 回答
0

问题是您的 PersonType 类没有@XmlRootElement. 我认为您希望在此页面中执行“解组为已知类型”示例,这不需要 PersonType 上的 @XmlRootElement 。

于 2013-11-11T06:17:20.170 回答