繁体   English   中英

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

[英]Unmarshalling xml with JAXB - namespace with XmlType and proporder

我正在尝试使用JAXB解组xml文件。 当我使用具有正确名称和名称空间的@XmlElement时,将进行编组工作(例如@XmlElement(name =“ name”,namespace =“ http://www.test.com”))

如果我将XmlType与propOrder一起使用,则不幸的是不再使用它(例如@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);
  }

}

内容

软件包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对象中的两个字段均为空。 在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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM