Wednesday, December 28, 2011

Transform XML into Objects

In the last post we saw how to serialize an object into XML content. Here we are going to just do the reverse of it.

So create a project named XMLBinding2 as described in the last post. Copy past the following XSD and the sample XML to your project as shown in the screenshot.
student-marks.xsd
<?xml version="1.0" encoding="UTF-8"?>

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
 targetNamespace="http://xml.netbeans.org/schema/student-marks"
 xmlns:tns="http://xml.netbeans.org/schema/student-marks"
 elementFormDefault="qualified">
<xsd:element name="report">
 <xsd:complexType>
   <xsd:sequence>
     <xsd:element name="students">
       <xsd:complexType>
         <xsd:sequence>
           <xsd:element name="student" maxOccurs="unbounded">
             <xsd:complexType>
               <xsd:sequence>
                 <xsd:element name="name">
                   <xsd:simpleType>
                     <xsd:restriction base="xsd:string"/>
                   </xsd:simpleType>
                 </xsd:element>
                 <xsd:element name="marks">
                   <xsd:complexType>
                     <xsd:sequence>
                       <xsd:element name="maths" type="xsd:int"/>
                       <xsd:element name="physics" type="xsd:int"/>
                       <xsd:element name="chemistry" type="xsd:int"/>
                       <xsd:element name="biology" type="xsd:int"/>
                     </xsd:sequence>
                   </xsd:complexType>
                 </xsd:element>
               </xsd:sequence>
             </xsd:complexType>
           </xsd:element>
         </xsd:sequence>
       </xsd:complexType>
     </xsd:element>
   </xsd:sequence>
 </xsd:complexType>
</xsd:element>
</xsd:schema>

student-sample.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<report xmlns="http://xml.netbeans.org/schema/student-marks">
 <students>
     <student>
         <name>Lydia</name>
         <marks>
             <maths>100</maths>
             <physics>98</physics>
             <chemistry>85</chemistry>
             <biology>93</biology>
         </marks>
     </student>
     <student>
         <name>Angel</name>
         <marks>
             <maths>100</maths>
             <physics>88</physics>
             <chemistry>95</chemistry>
             <biology>94</biology>
         </marks>
     </student>
 </students>
</report>




Now copy the following code to your main class and run it.
package xmlbinding2;

import java.io.InputStreamReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

/**
* This class de-serializes an XML content
*
* @author Pragalathan M
*/
public class XMLBinding2 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance("xmlbinding2");
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object result = unmarshaller.unmarshal(
            new InputStreamReader(XMLBinding2.class.
            getResourceAsStream("/xmlbinding2/student-sample.xml")));
    System.out.println(result);
}
}

Thats it. Now you have the objects.