java 使用 JAXB 解组 xml - 使用 XmlType 和 proporder 的命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13707408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unmarshalling xml with JAXB - namespace with XmlType and proporder
提问by Exocom
I'm trying to unmarshal an xml file using JAXB. When I am using @XmlElement with the correct name and namespace the unmarshalling works (e.g. @XmlElement(name = "name", namespace="http://www.test.com"))
我正在尝试使用 JAXB 解组 xml 文件。当我使用具有正确名称和命名空间的 @XmlElement 时,解组工作(例如 @XmlElement(name = "name", namespace="http://www.test.com"))
If I use XmlType together with propOrder it unfortunately does not anymore (e.g. @XmlType(namespace="http://www.test.com", name = "", propOrder = {"name", "description"})).
如果我将 XmlType 与 propOrder 一起使用,不幸的是不再使用(例如 @XmlType(namespace="http://www.test.com", name = "", propOrder = {"name", "description"}))。
The content of the xml file (test.xml):
xml文件(test.xml)的内容:
<Operation xmlns="http://www.test.com">
<Parameter>
<name>Param1</name>
<description>Description of Parameter1</description>
</Parameter>
<Parameter>
<name>Param2</name>
<description>Description of Parameter2</description>
</Parameter>
</Operation>
The content of JAXBExample.java is:
JAXBExample.java 的内容是:
package stackoverflow.problem.jaxb.ns;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class JAXBExample {
public static void main(String[] args) throws JAXBException, FileNotFoundException {
String xmlFilename = "test.xml";
JAXBContext context = JAXBContext.newInstance(Operation.class);
System.out.println("Output from our XML File: ");
Unmarshaller um = context.createUnmarshaller();
Operation op = (Operation) um.unmarshal(new FileReader(xmlFilename));
System.out.println("Operation-Content: " + op);
}
}
The content of
的内容
package stackoverflow.problem.jaxb.ns;
包 stackoverflow.problem.jaxb.ns;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="Operation", namespace="http://www.test.com")
@XmlAccessorType(XmlAccessType.FIELD)
public class Operation {
@XmlElement(name = "Parameter", namespace="http://www.test.com")
List<Parameter> parameterList;
@Override
public String toString(){
String retVal = "";
for(Parameter currentParameter: parameterList){
retVal += currentParameter.toString() + "\n";
}
return retVal;
}
}
And the Content of Parameter.java is:
Parameter.java的内容是:
package stackoverflow.problem.jaxb.ns;
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;
@XmlType(namespace="http://www.test.com", name = "", propOrder = {"name", "description"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Parameter {
//@XmlElement(name = "name", namespace="http://www.test.com")
String name;
//@XmlElement(name = "description", namespace="http://www.test.com")
String description;
@Override
public String toString(){
return this.name + "\t" + this.description;
}
}
If I uncomment the two @XmlElement lines in the last code block (Parameter.java), the unmarshalling works fine. If those two lines are not included, both fields in the Parameter object are null. Is there another way to declare a namespace when using propOrder in XmlType? Or did I do something else wrong?
如果我取消注释最后一个代码块 (Parameter.java) 中的两个 @XmlElement 行,解组工作正常。如果不包括这两行,则 Parameter 对象中的两个字段都为空。在 XmlType 中使用 propOrder 时是否有另一种方法来声明命名空间?还是我做错了什么?
回答by J?rn Horstmann
The namespace can also be defined per package, in a file named package-info.java
, this way you don't have to repeat it on every class or field:
命名空间也可以在每个包中定义,在一个名为 的文件中package-info.java
,这样你就不必在每个类或字段上重复它:
@javax.xml.bind.annotation.XmlSchema (
namespace = "http://www.test.com",
elementFormDefault=javax.xml.bind.annotation.XmlNsForm.QUALIFIED
)
package stackoverflow.problem.jaxb.ns;