Jaxb unmarshal不起作用 [英] Jaxb unmarshal doesn't works
问题描述
我有这个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代码:
I have this java code:
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
In console output I see null
我错了什么?
PS
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;
}
}
PS我根据 http://blog.bdoughan.com/2012/ 08 / handle-middle-of-xml-document-with-jaxb.html - 我没有看到差异
P.S I make according http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html - I don't see differences
PS
java版1.7.0_45
java version "1.7.0_45"
推荐答案
我认为将此内容添加到POM.xml后,这是jaxb版本问题。
I think it is jaxb version problem.
<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>
此问题已消失
我调查过问题,我很困惑。
I investigated problem and I confused.
我有2个项目 - project1和project2。我注意到
I have 2 projects - project1 and project2. I noticed that
对于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>
我非常困惑和混乱。但我想知道这个问题是真的。
I am very confused and messed. But I want to know true about this problem.
这篇关于Jaxb unmarshal不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!