<?xml version="1.0" encoding="UTF-8"?>
<xdms:container xmlns:xdms="http://www.namespace.com/RTS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xdms:uid="FHGHDFGDFJKGDFHG" xdms:version="3.2">
<xdms:requisites>
<xdms:documentKind>letter</xdms:documentKind>
<xdms:classification>main</xdms:classification>
<xdms:annotation>unknown</xdms:annotation>
</xdms:requisites>
</>
@XmlRootElement(name = "container")
@XmlAccessorType(XmlAccessType.FIELD)
public class Container {
private static final long serialVersionUID = 1L;
@XmlElement(name = "requisites")
private Requisites requisites;
public Container() {
super();
}
@Override
public String toString() {
return "Container{" +
"requisites=" + requisites +
'}';
}
}
@XmlRootElement(name = "requisites")
@XmlAccessorType(XmlAccessType.FIELD)
public class Requisites implements Serializable {
private static final long serialVersionUID = 1L;
private String documentKind;
private String classification;
private String annotation;
public Requisites() {
super();
}
@Override
public String toString() {
return "Requisites{" +
"documentKind='" + documentKind + '\'' +
", classfication='" + classification + '\'' +
", annotation='" + annotation + '\'' +
'}';
}
}
和运行解析的主类:
JAXBContext jaxbContext;
File xmlFile = new File("test.xml");
try
{
jaxbContext = JAXBContext.newInstance(Container.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Container cont = (Container) jaxbUnmarshaller.unmarshal(xmlFile);
System.out.println(cont);
}
catch (JAXBException e)
{
e.printStackTrace();
}
我得到的错误是:
UnMarshalException:意外元素(uri:“http://www.namespace.com/rts”,本地:“container”)。需要的元素为<{}Container>,<{}Requisite>(位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallingcontext.handleEvent(unmarshallingcontext.java:726)(位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.loader.reporterror(loader.java:247)(位于com.sun.xml.internal.bind.v2.runtime.unmarshaller.loader.reporterror(loader.java:242)(位于ment(AbstractSAXParser.java:509)在com.sun.org.apache.xerces.internal.impl.XmlnsDocumentScannerImpl.ScanStartElement(XmlnsDocumentScannerImpl.java:374)在com.sun.org.apache.xerces.internal.impl.XmlnsDocumentScannerImpl.java:374)在com.sun.org.apache.xerces.internal.impl.XmlnsDocumentScannerImpl.ScanRotelEmentHook(
UPD:我添加了名称空间,错误就消失了。但我的对象的字段中没有填充信息。它们是空的,尽管在xml中它们填充了信息
容器{requisites=requisites{documentkind='null',classfication='null',annotation='null'}}
@XmlRootElement(name = "container", namespace = "http://www.namespace.com/RTS")
@XmlAccessorType(XmlAccessType.FIELD)
public class Container {
@XmlElement(name = "requisites", namespace="http://www.namespace.com/RTS")
private Requisites requisites;
}
@XmlRootElement(name = "requisites")
@XmlAccessorType(XmlAccessType.FIELD)
public class Requisites implements Serializable {
private static final long serialVersionUID = 1L;
@XmlElement(name = "documentKind", namespace="http://www.namespace.com/RTS")
private String documentKind;
@XmlElement(name = "classification", namespace="http://www.namespace.com/RTS")
private String classification;
@XmlElement(name = "annotation", namespace="http://www.namespace.com/RTS")
private String annotation;
unmarshalException:意外元素(URI:“http://www.namespace.com/rts”,本地:“容器”)。在com.sun.xml.internal.bind.v2.runtime.unmarshaller.unmarshallingContext.HandleEvent(unmarshallingContext.java:726)在com.sun.xml.inter
UnMarshalException:意外元素(URI:“”,本地:“SystemUnit”)。需要的元素为<{http://xyz.abc.com/consumer/systems}SystemUnit>
当我试图从web服务获取对象时,会出现以下异常: User类使用进行注释,通过浏览器访问web服务将显示User的xml表示形式 客户端代码(我使用是因为导致): Web服务代码:
我在将XML响应从服务转换为POJO时遇到一个异常。XML如下所示: 我是这样用的: 下面是我的文件的详细信息 包-info.java ItemLink.java ItemLinks.java
在这里,我将从XML创建java对象: