Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unmarshalling xml

 
nikhil kreguri
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<?xml version="1.0"?>
<Blood_Report xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation='bloodreport.xsd'>
<PatientInfo>
<IdNo>342</IdNo>
<pname>Ajay</pname>
<Ref_Dr>Vijay kumar</Ref_Dr>
<Date>2005-07-22</Date>
<age>24</age>
<state>male</state>
<phone>9848023445</phone>
</PatientInfo>
<BloodSampleInfo>
<Haemoglobin>15</Haemoglobin>
<RBC>5.0</RBC>
<WBC>
<Neutrophils>50</Neutrophils>
<Eosinophils>3</Eosinophils>
<Lymphocytes>30</Lymphocytes>
<Monocytes>5</Monocytes>
<Basophils>0.4</Basophils>
</WBC>
<ESR>12</ESR>
<PCV>44</PCV>
<PlateletCount>250000</PlateletCount>
</BloodSampleInfo>
</Blood_Report>

Here is xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
<xsd:element name="Blood_Report" type="Blood_ReportType"/>
<xsd:complexType name="Blood_ReportType">
<xsd:sequence>
<xsd:element name="PatientInfo" type="PatientInfoType"/>
<xsd:element name="BloodSampleInfo" type="BloodSampleInfoType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="PatientInfoType">
<xsd:sequence>
<xsd:element name="IdNo" type="xsd:integer"/>
<xsd:element name="pname" type="xsd:string"/>
<xsd:element name="Ref_Dr" type="xsd:string"/>
<xsd:element name="Date" type="xsd:date"/>
<xsd:element name="age" type="ageType"/>
<xsd:element name="state" type="stateType"/>
<xsd:element name="phone" type="phoneType"/>
</xsd:sequence>
</xsd:complexType>
<xsd:simpleType name="phoneType">
<xsd:restriction base="xsd:integer">
<xsd:pattern value="\d{10}"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="ageType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="1"/>
<xsd:maxInclusive value="99"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="stateType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="male"/>
<xsd:enumeration value="female"/>
<xsd:enumeration value="child"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:complexType name="BloodSampleInfoType">
<xsd:sequence>
<xsd:element name="Haemoglobin" type="xsd:integer"/>
<xsd:element name="RBC" type="xsd:decimal"/>
<xsd:element name="WBC" type="WBCType"/>
<xsd:element name="ESR" type="ESRType"/>
<xsd:element name="PCV" type="PCVType"/>
<xsd:element name="PlateletCount" type="PCType"/>
</xsd:sequence>
</xsd:complexType>

<xsd:complexType name="WBCType">
<xsd:sequence>
<xsd:element name="Neutrophils" type="xsd:integer"/>
<xsd:element name="Eosinophils" type="xsd:integer"/>
<xsd:element name="Lymphocytes" type="xsd:integer"/>
<xsd:element name="Monocytes" type="xsd:integer"/>
<xsd:element name="Basophils" type="xsd:decimal"/>
</xsd:sequence>
</xsd:complexType>

<xsd:simpleType name="ESRType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="0"/>
<xsd:maxInclusive value="15"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="PCVType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="36"/>
<xsd:maxInclusive value="54"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="PCType">
<xsd:restriction base="xsd:integer">
<xsd:minInclusive value="150000"/>
<xsd:maxInclusive value="450000"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>

Here is java code to unmarshall
[color=orange]import generated.*;

import java.io.File;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Calendar;

import javax.activation.DataHandler;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.datatype.XMLGregorianCalendar;

public class Unmarshall {
private static final XMLGregorianCalendar XMLGregorianCalendar = null;

@SuppressWarnings("null")
public static void main(String[] args) {
try {

File file = new File("D:/nikhil/jax/Real/BloodReport.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(BloodSampleInfoType.class,BloodReportType.class,WBCType.class,PatientInfoType.class,ObjectFactory.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
/*JAXBElement<BloodReport> root = unmarshaller.unmarshal(new StreamSource(DataHandler.getDataSource().getInputStream()), BloodReportType.class);
return root.getValue();*/

BloodReportType bpt = (BloodReportType) jaxbUnmarshaller.unmarshal(file);
PatientInfoType p=(PatientInfoType) jaxbUnmarshaller.unmarshal(file);
System.out.println(p);
System.out.println(bpt);


} catch (JAXBException e) {
e.printStackTrace();
}

}
}
[/color]

i am getting the following error

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"BloodReport"). Expected elements are <{}Blood_Report>
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at Unmarshall.main(Unmarshall.java:63)

please help me to resolve this issue
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18272
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Nihil!

There's nothing about JSF in what you listed, so I'm moving your question over to where the XML experts live. They're more likely to give you a useful answer.

Incidentally, there's a "Code" button in the Ranch message editor GUI. You can use that to generate tags to wrap your java code and XML samples and it will make them more readable.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic