无法使用生成的 JAXB 类 解析 XML 个文件
Can't parse XML files with generated JAXB classes
我正在尝试从 this xsd file according to this tutorial 生成 JAXB 绑定。首先我注意到有一些依赖关系,但我设法让它们离开同一个地址。下载后我 运行
xjc net_file.xsd
这确实生成了一些 java 代码,但是与教程中的不同,文件看起来像“...Type.java”。我觉得这很不方便,但我可以忍受。
之后我尝试解析 given 示例文件:
File file = new File("quickstart.net.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(NetType.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
NetType net = (NetType) jaxbUnmarshaller.unmarshal(file);
但是,我得到以下异常:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"net"). Expected elements are (none)
有谁知道我做错了什么吗?
我认为您的 XML 文件是由两个 XSD 定义的。看这里:
xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/netconvertConfiguration.xsd"
和 xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/net_file.xsd
您将错过 net
或 configuration
个元素。
我不知道 JAXB 如何支持部分 XSD。你确定你正确地遵循了教程吗?您应该能够将这两个 xsd 文件合并为一个 (?).
嗯,我找到问题了。看来您实际上需要使用生成的ObjectFactory
。我修改了这样的代码:
File file = new File("quickstart.net.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<NetType> netElement = (JAXBElement<NetType>) jaxbUnmarshaller.unmarshal(file);
现在一切正常。我想原因是 ObjectFactory
有一个带注释的构建器方法:
/**
* Create an instance of {@link JAXBElement }{@code <}{@link NetType }{@code >}}
*
*/
@XmlElementDecl(namespace = "", name = "net")
public JAXBElement<NetType> createNet(NetType value) {
return new JAXBElement<NetType>(_Net_QNAME, NetType.class, null, value);
}
恕我直言,这确实设计过度了,但无论如何 :)
我正在尝试从 this xsd file according to this tutorial 生成 JAXB 绑定。首先我注意到有一些依赖关系,但我设法让它们离开同一个地址。下载后我 运行
xjc net_file.xsd
这确实生成了一些 java 代码,但是与教程中的不同,文件看起来像“...Type.java”。我觉得这很不方便,但我可以忍受。 之后我尝试解析 given 示例文件:
File file = new File("quickstart.net.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(NetType.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
NetType net = (NetType) jaxbUnmarshaller.unmarshal(file);
但是,我得到以下异常:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"net"). Expected elements are (none)
有谁知道我做错了什么吗?
我认为您的 XML 文件是由两个 XSD 定义的。看这里:
xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/netconvertConfiguration.xsd"
和 xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/net_file.xsd
您将错过 net
或 configuration
个元素。
我不知道 JAXB 如何支持部分 XSD。你确定你正确地遵循了教程吗?您应该能够将这两个 xsd 文件合并为一个 (?).
嗯,我找到问题了。看来您实际上需要使用生成的ObjectFactory
。我修改了这样的代码:
File file = new File("quickstart.net.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<NetType> netElement = (JAXBElement<NetType>) jaxbUnmarshaller.unmarshal(file);
现在一切正常。我想原因是 ObjectFactory
有一个带注释的构建器方法:
/**
* Create an instance of {@link JAXBElement }{@code <}{@link NetType }{@code >}}
*
*/
@XmlElementDecl(namespace = "", name = "net")
public JAXBElement<NetType> createNet(NetType value) {
return new JAXBElement<NetType>(_Net_QNAME, NetType.class, null, value);
}
恕我直言,这确实设计过度了,但无论如何 :)