gpt4 book ai didi

java - UnmarshalException : unexpected element (uri :"http://www.namespace.com/RTS", 本地 :"container")

转载 作者:行者123 更新时间:2023-12-02 00:58:07 26 4
gpt4 key购买 nike

我正在尝试将 xml 映射到 java 类。Xml 来自第三方服务。结构是相同的,但可能有不同的前缀或命名空间。 XML:

<?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>
</>

我的类(class):

@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();
}

我收到错误:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.namespace.com/RTS", local:"container"). Expected elements are <{}container>,<{}requisites> at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538) at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374) at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613) at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132) at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852)

UPD:我添加了nameSpace,错误消失了。但我的对象的字段并没有充满信息。它们是空的,尽管在 xml 中它们填充了信息

Container{requisites=Requisites{documentKind='null', classfication='null', annotation='null'}}

最佳答案

您没有在 jaxb 类中指定命名空间

类似的事情:

@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;

}

您可能还需要为 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;

关于java - UnmarshalException : unexpected element (uri :"http://www.namespace.com/RTS", 本地 :"container"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61060432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com