使用JAXB解组xml - 使用XmlType和proporder的命名空间

时间:2012-12-04 16:30:06

标签: java jaxb unmarshalling

我正在尝试使用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;
  }
}

参数.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时是否有另一种声明命名空间的方法?或者我做错了什么?

1 个答案:

答案 0 :(得分:5)

也可以在名为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;