JAXB:请在JAXB中验证XML到java的结构

时间:2012-04-03 16:18:32

标签: jaxb

这是我的XML文件

   <BADFM>
<Given>
<Ord>
<Bag IDC="DM" />
</Ord>
</Given>
</BADFM>

这是我的Parser类

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

public class Test {
    public static void main(String args[]) throws Exception {

        File file = new File("D:\\BADML.xml");
        JAXBContext jaxbContext = JAXBContext
                .newInstance(MyMessage.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        MyMessage authentifyResult = (MyMessage) jaxbUnmarshaller
                .unmarshal(file);
        System.out.println(authentifyResult.getGiven().getOrd().getBag().getIDC());

    }
}

这是MyMessage

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="BADFM")
public class MyMessage
{
@XmlElement(name="Given")
private Given given;

public Given getGiven() {
    return given;
}

public void setGiven(Given given) {
    this.given = given;
}


}

这是Given.java

import javax.xml.bind.annotation.XmlElement;

public class Given {
    private Ord ord;
    @XmlElement(name = "Ord")
    public Ord getOrd() {
        return ord;
    }
    public void setOrd(Ord ord) {
        this.ord = ord;
    }
}

这是Ord.java

 import javax.xml.bind.annotation.XmlElement;

public class Ord {

    private Bag bag;
    @XmlElement(name="Bag")
    public Bag getBag() {
        return bag;
    }

    public void setBag(Bag bag) {
        this.bag = bag;
    }

}

这是Bag.java

  import javax.xml.bind.annotation.XmlAttribute;

public class Bag {
      @XmlAttribute(name="IDC")
    private String IDC ;

    public String getIDC() {
        return IDC;
    }
    @XmlAttribute(name="IDC")
    public void setIDC(String IDC) {
        IDC = IDC;
    }

}

当我跑步时,我正在

  Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
Class has two properties of the same name "IDC"
    this problem is related to the following location:
        at public java.lang.String Bag.getIDC()
        at Bag
        at public Bag Ord.getBag()
        at Ord
        at public Ord Given.getOrd()
        at Given
        at public Given MyMessage.getGiven()
        at MyMessage
    this problem is related to the following location:
        at private java.lang.String Bag.IDC
        at Bag
        at public Bag Ord.getBag()
        at Ord
        at public Ord Given.getOrd()
        at Given
        at public Given MyMessage.getGiven()
        at MyMessage

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
    at javax.xml.bind.ContextFinder.find(Unknown Source)
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
    at Test.main(Test.java:11)

1 个答案:

答案 0 :(得分:1)

您需要将@XmlAttribute用于IDC类的Bag变量。完成更改后,您在顶部引用的XML将起作用。

至于您当前的代码,它期望XML看起来像:

<Bag>
  <IDC>DM</IDC>
</Bag>

您可以通过填充所有属性字段然后将对象封送到文件来轻松查看您的类所期望的XML类型。

<强>更新

如果您要使用正确命名的getter和setter,则应始终将属性声明为private。否则,JAXB将抛出错误Class has two properties of the same name

当声明类属性为@XmlAttribute时,您还应该将注释放在getter上,以便JAXB认为您不希望同时使用@XmlAttribute@XmlElement名。

public class Bag {
    private String IDC ;

    @XmlAttribute(name="IDC")
    public String getIDC() {
        return IDC;
    }

    public void setIDC(String IDC) {
        this.IDC = IDC;
    }
}