I am trying to unMarshall XML data from a file with the following Schema file, using NetBeans 8.2, in a java Web Application.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://xml.netbeans.org/schema/xSchema"
xmlns:tns="http://xml.netbeans.org/schema/xSchema"
elementFormDefault="qualified">
<xsd::complexType name="xItem">
<xsd:sequence>
<xsd:element name="item1" type="xsd:string"/>
<xsd:element name="item2" type="xsd:string"/>
<xsd:element name="item3" type="xsd:int"/>
<xsd:element name="x-price">
<xsd:complexType>
<xsd:sequence>
<xsd:element name = "item4" type="xsd:string"/>
<xsd:element name = item5" type = "xsd:float"/>
<xsd:element name = "item6" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:element name = "xList">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="x_details" type="tns:xItem" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
I created an XML Document using NetBeans and populated that with data, matching the Schema file. The XML schema is bound to the Java Web Application using Jaxb. I have created the unmarshalling code using the jaxbu method, which looks like this
xList currentx = new xList();
//UnMarshal data from XML to Object
try {
javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(currentx.getClass().getPackage().getName());
javax.xml.bind.Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
currentx = (xList) unmarshaller.unmarshal(fileHandle); //NOI18N
} catch (javax.xml.bind.JAXBException ex) {
// XXXTODO Handle exception
java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
}
The resulting ArrayList currentx, contains all the instances of item1, item2 and item 3. It does not however appear to contain items 4 and 5. 'fileHandle' is a FILE object, and is obviously pointing to the correct file. The generated files include classes for xItem and a nested static class for x-price, but whenever I try to retrieve data from the nested class, I get a null pointer exception for items4, 5 and 6, but can read items 1,2 and 3. The unmarshalling function appears to perform without any exceptions being created. Am I using the right methods for unmarshalling all the data or am I missing something to deal with the x-price ( items 4, 5 and 6)? How is that done ?
XLM first data record and header:
<ns1:xList
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:ns1='http://xml.netbeans.org/schema/xSchema'
xsi:schemaLocation='http://xml.netbeans.org/schema/xSchema xSchema1.xsd'>
<ns1:x_details>
<ns1:item1>AA</ns1:item1>
<ns1:item2>AA</ns1:item2>
<ns1:aitem3>212</ns1:item4>
<ns1:x-price>
<ns1:item4>GBP</ns1:item4>
<ns1:item5>75.26</ns1:item5>
<ns1:item6>04/12/2018</ns1:item6>
</ns1:x-price>
</ns1:x_details>
In your example Schema your schema seems to be invalid:
<xsd:element name = item5" type = "xsd:float"/>
and should be
<xsd:element name = "item5" type = "xsd:float"/>
also it is hard to help if your real data is missing (your XML). Could you add a full XML example?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.