java – 使用JAXB解组xml – 带有XmlType和proporder的命名空间
作者:互联网
我正在尝试使用JAXB解组xml文件.当我使用具有正确名称和命名空间的@XmlElement时,解组工作(例如@XmlElement(name =“name”,namespace =“http://www.test.com”))
如果我与propOrder一起使用XmlType,则不幸的是它不再存在(例如@XmlType(namespace =“http://www.test.com”,name =“”,propOrder = {“name”,“description”})).
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>
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);
}
}
的内容
package 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;
}
}
而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;
}
}
如果我取消注释最后一个代码块(Parameter.java)中的两个@XmlElement行,则解组工作正常.如果不包括这两行,则Parameter对象中的两个字段都为null.在XmlType中使用propOrder时是否有另一种声明命名空间的方法?或者我做错了什么?
解决方法:
也可以在名为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;
标签:java,jaxb,unmarshalling 来源: https://codeday.me/bug/20190520/1143993.html