问题
I have this xml file:
<?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>
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());
In console output I see null
What do I wrong?
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;
}
}
P.S I make according http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html - I don't see differences
P.S.
java version "1.7.0_45"
回答1:
I think it is jaxb version problem.
after adding this content to 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>
this problem is disappeared
I investigated problem and I confused.
I have 2 projects - project1 and project2. I noticed that
for project1 valid
<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>
and
<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>
But for project2 valid only
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.
来源:https://stackoverflow.com/questions/22191021/jaxb-unmarshal-doesnt-works