解组时错误"类型不匹配:无法从XmlAccessType转换为AccessType"

时间:2012-05-09 10:28:09

标签: java xml xsd jaxb

我正在使用Jaxb,Unmarshalling一个xml。我正在使用java 1.6。这是通过JWSDP 2.0生成的类。 (xjc.bat)但我的问题是我无法编译生成的类。我收到语法错误,如下所示。

  

“类型不匹配:无法从XmlAccessType转换为AccessType”

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;


@XmlAccessorType(XmlAccessType.FIELD)// here i am getting sytax error
@XmlType(name = "personinfo", propOrder = {
    "firstname",
    "lastname",
    "address"
})
public class Personinfo {

    @XmlElement(required = true)
    protected String firstname;
    @XmlElement(required = true)
    protected String lastname;
    @XmlElement(name = "Address", required = true)
    protected PersonAddress address;
............................

任何人都可以在这方面提供帮助,

1 个答案:

答案 0 :(得分:4)

我使用下面的演示代码尝试了您的问题中的Personinfo类,一切正常。由于您使用的是Java SE 6(包括JAXB实现),因此您需要确保在类路径中没有JWSDP 2.0中的任何JAXB API。

我还建议使用Java SE 6中的XJC实用程序而不是JWSDP,因为JWSDP已经很老了:

<强>演示

package forum10514244;

import java.io.File;
import javax.xml.bind.*;
import javax.xml.transform.stream.StreamSource;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Personinfo.class);

        File xml = new File("src/forum10514244/input.xml");
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<Personinfo> je = unmarshaller.unmarshal(new StreamSource(xml), Personinfo.class);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(je, System.out);
    }

}

<强> input.xml中/输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <firstname>Jane</firstname>
    <lastname>Doe</lastname>
    <Address/>
</root>