我没有足够的信息来诊断为什么你的解组没有正确发生。以下将起作用,您可以将其与您正在做的事情进行比较以查找错误。
最有可能的候选人是:
- 创建 JAXBContext 时,您没有告诉 JAXB 足够多的类。
- 您的 XML 文档没有正确的命名空间限定。
使用以下模式:
常见的.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ns.com/common"
xmlns="http://www.ns.com/common"
elementFormDefault="qualified">
<xs:complexType name="commonElement">
<xs:sequence>
<xs:element name="commonChild" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
aa.xsd
<xs:schema xmlns="http://www.ns.com/aa" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:DD="http://www.ns.com/common" targetNamespace="http://www.ns.com/aa"
elementFormDefault="qualified">
<xs:import namespace="http://www.ns.com/common"
schemaLocation="common.xsd" />
<xs:element name="Response">
<xs:complexType>
<xs:sequence>
<xs:element name="element" type="DD:commonElement" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
生成了以下类:
com.ns.aa.package-info
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ns.com/aa", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.ns.aa;
com.ns.aa.Response
package com.ns.aa;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import com.ns.common.CommonElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"element"
})
@XmlRootElement(name = "Response")
public class Response {
@XmlElement(required = true)
protected CommonElement element;
public CommonElement getElement() {
return element;
}
public void setElement(CommonElement value) {
this.element = value;
}
}
com.ns.common.package-info
@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.ns.com/common", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.ns.common;
com.ns.common.CommonElement
package com.ns.common;
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 = "commonElement", propOrder = {
"commonChild"
})
public class CommonElement {
@XmlElement(required = true)
protected String commonChild;
public String getCommonChild() {
return commonChild;
}
public void setCommonChild(String value) {
this.commonChild = value;
}
}
使用这些类,我可以解组以下 XML 文档:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Response xmlns="http://www.ns.com/common" xmlns:ns2="http://www.ns.com/aa">
<ns2:element>
<commonChild>FOO</commonChild>
</ns2:element>
</ns2:Response>
使用以下代码:
演示
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.ns.aa.Response;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Response.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("input.xml");
Response response = (Response) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(response, System.out);
}
}