我沒有足夠的信息來診斷爲什麼你解組沒有正確發生。以下內容將起作用,您可以將其與您正在做的查找錯誤進行比較。
最有可能的候選人是:
- 在創建JAXBContext而你沒有告訴JAXB約夠班。
- 您的XML文檔沒有正確的命名空間限定。
使用以下模式:
common.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-信息
@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.響應
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-信息
@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);
}
}
你有一個按比例縮小的XSD例如XML&你可以發佈,演示您的問題? – 2010-08-31 19:54:43