UnmarshalException: unexpected element (uri:"http://www.namespace.com/RTS", local:"container")

huangapple go评论100阅读模式
英文:

UnmarshalException: unexpected element (uri:"http://www.namespace.com/RTS", local:"container")

问题

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

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <xdms:requisites>
  4. <xdms:documentKind>letter</xdms:documentKind>
  5. <xdms:classification>main</xdms:classification>
  6. <xdms:annotation>unknown</xdms:annotation>
  7. </xdms:requisites>
  8. </xdms:container>

我的类:

  1. @XmlRootElement(name = "container")
  2. @XmlAccessorType(XmlAccessType.FIELD)
  3. public class Container {
  4. private static final long serialVersionUID = 1L;
  5. @XmlElement(name = "requisites")
  6. private Requisites requisites;
  7. public Container() {
  8. super();
  9. }
  10. @Override
  11. public String toString() {
  12. return "Container{" +
  13. "requisites=" + requisites +
  14. '}';
  15. }
  16. }
  17. @XmlRootElement(name = "requisites")
  18. @XmlAccessorType(XmlAccessType.FIELD)
  19. public class Requisites implements Serializable {
  20. private static final long serialVersionUID = 1L;
  21. private String documentKind;
  22. private String classification;
  23. private String annotation;
  24. public Requisites() {
  25. super();
  26. }
  27. @Override
  28. public String toString() {
  29. return "Requisites{" +
  30. "documentKind='" + documentKind + '\'' +
  31. ", classification='" + classification + '\'' +
  32. ", annotation='" + annotation + '\'' +
  33. '}';
  34. }
  35. }

以及我运行解析的主类:

  1. JAXBContext jaxbContext;
  2. File xmlFile = new File("test.xml");
  3. try
  4. {
  5. jaxbContext = JAXBContext.newInstance(Container.class);
  6. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  7. Container cont = (Container) jaxbUnmarshaller.unmarshal(xmlFile);
  8. System.out.println(cont);
  9. }
  10. catch (JAXBException e)
  11. {
  12. e.printStackTrace();
  13. }

我得到了错误:

  1. javax.xml.bind.UnmarshalException: unexpected element
  2. (uri:"http://www.namespace.com/RTS", local:"container"). Expected
  3. elements are <{}container>,<{}requisites> at
  4. com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
  5. ...

更新:
我添加了命名空间,错误消失了。但是我的对象字段没有填充信息。它们是空的,尽管在 XML 中它们填充有信息。

  1. Container{requisites=Requisites{documentKind='null',
  2. classfication='null', annotation='null'}}
英文:

I am trying to map my xml to java classes.
Xml comes from a third-party service. The structure is the same, but there may be a different prefix or namespace. XML:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;xdms:container xmlns:xdms=&quot;http://www.namespace.com/RTS&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xdms:uid=&quot;FHGHDFGDFJKGDFHG&quot; xdms:version=&quot;3.2&quot;&gt;
  3. &lt;xdms:requisites&gt;
  4. &lt;xdms:documentKind&gt;letter&lt;/xdms:documentKind&gt;
  5. &lt;xdms:classification&gt;main&lt;/xdms:classification&gt;
  6. &lt;xdms:annotation&gt;unknown&lt;/xdms:annotation&gt;
  7. &lt;/xdms:requisites&gt;
  8. &lt;/&gt;

My classes:

  1. @XmlRootElement(name = &quot;container&quot;)
  2. @XmlAccessorType(XmlAccessType.FIELD)
  3. public class Container {
  4. private static final long serialVersionUID = 1L;
  5. @XmlElement(name = &quot;requisites&quot;)
  6. private Requisites requisites;
  7. public Container() {
  8. super();
  9. }
  10. @Override
  11. public String toString() {
  12. return &quot;Container{&quot; +
  13. &quot;requisites=&quot; + requisites +
  14. &#39;}&#39;;
  15. }
  16. }
  17. @XmlRootElement(name = &quot;requisites&quot;)
  18. @XmlAccessorType(XmlAccessType.FIELD)
  19. public class Requisites implements Serializable {
  20. private static final long serialVersionUID = 1L;
  21. private String documentKind;
  22. private String classification;
  23. private String annotation;
  24. public Requisites() {
  25. super();
  26. }
  27. @Override
  28. public String toString() {
  29. return &quot;Requisites{&quot; +
  30. &quot;documentKind=&#39;&quot; + documentKind + &#39;\&#39;&#39; +
  31. &quot;, classfication=&#39;&quot; + classification + &#39;\&#39;&#39; +
  32. &quot;, annotation=&#39;&quot; + annotation + &#39;\&#39;&#39; +
  33. &#39;}&#39;;
  34. }
  35. }

and the main class in which I run parsing:

  1. JAXBContext jaxbContext;
  2. File xmlFile = new File(&quot;test.xml&quot;);
  3. try
  4. {
  5. jaxbContext = JAXBContext.newInstance(Container.class);
  6. Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
  7. Container cont = (Container) jaxbUnmarshaller.unmarshal(xmlFile);
  8. System.out.println(cont);
  9. }
  10. catch (JAXBException e)
  11. {
  12. e.printStackTrace();
  13. }

And I get the error:

> 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:
I added nameSpace, the error is gone. But the fields of my objects are not filled with information. They are empty, although in xml they are filled with information

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

答案1

得分: 1

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

类似这样的:

  1. @XmlRootElement(name = "container", namespace = "http://www.namespace.com/RTS")
  2. @XmlAccessorType(XmlAccessType.FIELD)
  3. public class Container {
  4. @XmlElement(name = "requisites", namespace="http://www.namespace.com/RTS")
  5. private Requisites requisites;
  6. }

你可能还需要为Requisites中的每个元素添加命名空间。

  1. @XmlRootElement(name = "requisites")
  2. @XmlAccessorType(XmlAccessType.FIELD)
  3. public class Requisites implements Serializable {
  4. private static final long serialVersionUID = 1L;
  5. @XmlElement(name = "documentKind", namespace="http://www.namespace.com/RTS")
  6. private String documentKind;
  7. @XmlElement(name = "classification", namespace="http://www.namespace.com/RTS")
  8. private String classification;
  9. @XmlElement(name = "annotation", namespace="http://www.namespace.com/RTS")
  10. private String annotation;
  11. }
英文:

You don't specify the namespace in your jaxb class

Something like that:

  1. @XmlRootElement(name = &quot;container&quot;, namespace = &quot;http://www.namespace.com/RTS&quot;)
  2. @XmlAccessorType(XmlAccessType.FIELD)
  3. public class Container {
  4. @XmlElement(name = &quot;requisites&quot;, namespace=&quot;http://www.namespace.com/RTS&quot;)
  5. private Requisites requisites;
  6. }

you might need to add namespace to every element in Requisites as well.

  1. @XmlRootElement(name = &quot;requisites&quot;)
  2. @XmlAccessorType(XmlAccessType.FIELD)
  3. public class Requisites implements Serializable {
  4. private static final long serialVersionUID = 1L;
  5. @XmlElement(name = &quot;documentKind&quot;, namespace=&quot;http://www.namespace.com/RTS&quot;)
  6. private String documentKind;
  7. @XmlElement(name = &quot;classification&quot;, namespace=&quot;http://www.namespace.com/RTS&quot;)
  8. private String classification;
  9. @XmlElement(name = &quot;annotation&quot;, namespace=&quot;http://www.namespace.com/RTS&quot;)
  10. private String annotation;

答案2

得分: 1

@XmlRootElement中使用namespace属性,如下所示:

  1. @XmlRootElement(name="container", namespace="http://www.namespace.com/RTS")

如果仍然出现相同的错误,您还需要在@XmlElement中指定namespace属性:

  1. @XmlElement(name="requisites", namespace="http://www.namespace.com/RTS")

您在Requisites标签内的每个属性也应在其@XmlElement注释中包含命名空间。

英文:

Use the namespace attribute in the @XmlRootElement as follows:

  1. @XmlRootElement(name=&quot;container&quot;, namespace=&quot;http://www.namespace.com/RTS&quot;)

In case, if it still throws the same error, you also have to specify the namespace attribute in your @XmlElement:

  1. @XmlElement(name=&quot;requisites&quot;, namespace=&quot;http://www.namespace.com/RTS&quot;)

Your each attribute inside the Requisites tag should also contain the namespace within its @XmlElement annotation.

huangapple
  • 本文由 发表于 2020年4月6日 20:58:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61060432.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定