2014-03-05 60 views
0

我有這個XML文件:JAXB解組沒有作品

<?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代碼:

​​

在控制檯輸出我看到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」

+0

爲我工作和打印'約克'。你確定這就是你得到的? –

+0

在我家的電腦 - 也是。但在工作中不起作用 – gstackoverflow

+0

什麼是Java版本都運行? –

回答

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> 

但對於項目2只適用

   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> 

我很困惑和混亂。但我想知道這個問題是真的。