Jaxb unmarshal不起作用

时间:2014-03-05 07:11:51

标签: java xml jaxb unmarshalling

我有这个xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<diffgr:diffgram xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"
    xmlns="http://tempuri.org/" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <DocumentElement xmlns="">
        <Result diffgr:id="Result1" msdata:rowOrder="0">
            <CITY>York</CITY>
        </Result>
    </DocumentElement>
</diffgr:diffgram>

我有这个java代码:

        XMLInputFactory xif = XMLInputFactory.newFactory();

        String content = readFile("D:\\work\\cim\\target\\my.xml", Charset.defaultCharset());    

        StreamSource xml = new StreamSource(new StringReader(content));    

        XMLStreamReader xsr = xif.createXMLStreamReader(xml);
        xsr.nextTag();
        while(!xsr.getLocalName().equals("Result")) {
            xsr.nextTag();
        }

        JAXBContext jc = JAXBContext.newInstance(CimRecordTest.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<CimRecordTest> jb = unmarshaller.unmarshal(xsr, CimRecordTest.class);
        xsr.close();

        CimRecordTest cimRecordTest = jb.getValue();
        System.out.println(cimRecordTest.getCity());

在控制台输出中,我看到null

我错了什么?

P.S

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 = "record")
@XmlAccessorType(XmlAccessType.FIELD)
public class CimRecordTest {
    /**
     * CITY
     */
   @XmlElement(name="CITY")
    private String city;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

}

我根据http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html制作 - 我没有看到差异

P.S。

java版“1.7.0_45”

1 个答案:

答案 0 :(得分:0)

我认为这是jaxb版本问题。

将此内容添加到POM.xml

       <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.7</version>
        </dependency>
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.2.7</version>
        </dependency>

这个问题消失了

我调查了问题,我很困惑。

我有2个项目 - project1和project2。我注意到了

对于project1有效

           <dependency>
               <groupId>javax.xml.bind</groupId>
                <artifactId>jaxb-api</artifactId>
                <version>2.2.7</version>
            </dependency>
            <dependency>
                <groupId>com.sun.xml.bind</groupId>
                <artifactId>jaxb-impl</artifactId>
                <version>2.2.7</version>
            </dependency>

           <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.2.6</version>
        </dependency>
        <dependency>
            <groupId>javax.xml</groupId>
            <artifactId>jaxb-impl</artifactId>
            <version>2.1</version>
        </dependency>

但是对于project2仅有效

                dependency>
                   <groupId>javax.xml.bind</groupId>
                    <artifactId>jaxb-api</artifactId>
                    <version>2.2.7</version>
                </dependency>
                <dependency>
                    <groupId>com.sun.xml.bind</groupId>
                    <artifactId>jaxb-impl</artifactId>
                    <version>2.2.7</version>
                </dependency>

我非常困惑和混乱。但我想知道这个问题是真的。