[我从某个第三方那里获得了xsd文件,该文件以前使用“ include”而不是“ import”。我正在使用jaxb使用这些xsd文件生成Java文件。最初的xsd结构导致输出,其中相同的类包含在不同的包中。例如,如果生成了两个软件包“ aa”和“ bb”,它们都包含相同的公用文件:
aa / commonElement.javaaa / a.java
bb / commonElement.javabb / b.java
这是我想避免的事情,我希望commonElement.java在单个程序包中创建一次,然后由其余程序导入,因此我开始改用import。
<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" jaxb:version="1.0" jaxb:extensionBindingPrefixes="xjc">
<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" ../>
和我期望的那样,创建并编译了Java类。
common / commonElement.javaaa / aa.java
[问题是,当我从api调用接收到aa的结果并解组结果时,我得到一个aa类,该类具有正确创建的commonElement,但具有空字段。
我的猜测是,这些字段为空,因为解组官不理解他需要在“ common”名称空间中查找定义,而是在“ aa” namesapce中查找它们,但如何使其正常工作?
感谢您的帮助
我没有足够的信息来诊断为什么您的解组未正确发生。以下将起作用,您也许可以将其与您发现错误的操作进行比较。
最可能的候选人是:
使用以下架构:
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-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>
使用以下代码:
Demo
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);
}
}
我有一个问题,请知道。我已经尝试了上述答案,并给出了详细信息,其工作正常。
但是假设我想在下面进行xml验证,就像您给的一样,但是我做了一些小的更改,我想验证
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Response xmlns="http://www.ns.com/aa"> <element xmlns="http://www.ns.com/common"> <commonChild>FOO</commonChild> </element> </Response>
但是它将给出以下错误
SAX Exception: cvc-complex-type.2.4.a: Invalid content was found starting with element 'element'. One of '{"http://www.ns.com/aa":element}' is expected.
is not valid against