Showing posts with label jaxb. Show all posts
Showing posts with label jaxb. Show all posts

Sunday, October 19, 2014

JAXB XML Parsing

How easy to parse an xml??? You might be having a huge xml, but still its not a matter to parse it even you don't know its structure. Now! you will see how to parse any xml using jaxb. All necessary is only a xsd. Don't worry even if you don't have it. You can create your xsd using different tools/online. If you need to create it your self you should be knowing xsd rules. Lets see an example 1)This is your xml

200
1234567890
12311
HWH DLI KLK MAI
23-12-2013

DLI
Delhi


KLK
Kalka


KLK
Kalka


DLI
Delhi

1A
3
N


1
CNF ,GN
CNF


2
CNF ,GN
CNF


3
CNF ,GN
CNF





2) Generate xsd using eclipse/tools/online/yourself... 3) Generate jaxb classes. a)Create a maven based project b)add the following lines to your pom.xml(its a plugin to convert your xsd schema to corresponding jaxb classes.)

<build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.8.3</version>
                <configuration>
                   <schemaDirectory>src/main/resources/schema</schemaDirectory>
                   <bindingDirectory>src/main/resources/schema</bindingDirectory>
                   <generatePackage>org.pnr</generatePackage>
                   <strict>false</strict>
                   <extension>true</extension>
                   <plugins>
                       <plugin>
                           <groupId>org.jvnet.jaxb2_commons</groupId>
                           <artifactId>jaxb2-basics</artifactId>
                           <version>0.6.2</version>
                       </plugin>
                       <plugin>
                           <groupId>org.jvnet.jaxb2_commons</groupId>
                           <artifactId>jaxb2-basics-annotate</artifactId>
                           <version>0.6.2</version>
                       </plugin>
                   </plugins>
                   <args>
                       <arg>-Xannotate</arg>
                       <arg>-XtoString</arg>
                   </args>
                </configuration>
                <executions>
                    <execution>
                        <id>generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-runtime</artifactId>
            <version>0.6.2</version>
        </dependency>
    </dependencies>

       
4) Create a directory with name schema in your resource folder in your project(it can be any place, but specify the location of xsd in your pom.xml) Place the xsd file in resources/schema folder for which jaxb classes should be generated.

5) Build the project using command mvn clean install -e 6) you can see all the generated files in target/generated-sources/xjc path present in their packages, specified in pom.xml i.e org.pnr 7)Place the generated ObjectFactory.java and Xml.java in your class path. Or directly place them in your package structure. 8)Finally it's time to see jaxb parsing.
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.StringReader;


import pnr.Xml;
import pnr.ObjectFactory;
/**
 * Created with IntelliJ IDEA.
 * User: RAVI
 * Date: 11/10/14
 * Time: 12:10 PM
 * To change this template use File | Settings | File Templates.
 */
public class TestXmlToJaxb {

    public static void main(String args[]){
         Converter c = new Converter();
        c.doConversion();
    }
}

class Converter{
    public void doConversion(){
                
               String xmlString = "" +
                                  "200" +
                                  "1234567890" +
                                  "12311" +
                                  "HWH DLI KLK MAI" +
                                  "23-12-2013" +
                                  "" +
                                  "DLI" +
                                  "Delhi" +
                                  "" +
                                  "" +
                                  "KLK" +
                                  "Kalka" +
                                  "" +
                                  "" +
                                  "KLK" +
                                  "Kalka" +
                                  "" +
                                  "" +
                                  "DLI" +
                                  "Delhi" +
                                  "" +
                                  "1A" +
                                  "3" +
                                  "N" +
                                  "" +
                                  "" +
                                  "1" +
                                  "CNF ,GN" +
                                  "CNF" +
                                  "" +
                                  "" +
                                  "2" +
                                  "CNF ,GN" +
                                  "CNF" +
                                  "" +
                                  "" +
                                  "3" +
                                  "CNF ,GN" +
                                  "CNF" +
                                  "" +
                                  "" +
                                  "" +
                                  "";
    try{

        JAXBContext jaxbContext = JAXBContext.newInstance(Xml.class);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Xml xmll = (Xml) jaxbUnmarshaller.unmarshal(new StringReader(xmlString));
        System.out.println(xmll);
        System.out.println(xmll.getResponseCode());
        Xml.Passengers pass = xmll.getPassengers();
        System.out.println(pass.getPassenger().get(1));

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

    }
}
Output:
pnr.Xml@311671b2[responseCode=200, pnr=1234567890, trainNum=12311, trainName=HWH DLI KLK MAI, doj=23-12-2013, fromStation=pnr.Xml$FromStation@7d2452e8[code=DLI, name=Delhi], toStation=pnr.Xml$ToStation@5bbf3d87[code=KLK, name=Kalka], reservationUpto=pnr.Xml$ReservationUpto@6860991f[code=KLK, name=Kalka],

boardingPoint=pnr.Xml$BoardingPoint@1de4f7c2[code=DLI, name=Delhi], clazz=1A, noOfPassengers=3, chartPrepared=N, 

passengers=pnr.Xml$Passengers@2345f0e3[passenger={pnr.Xml$Passengers$Passenger@44c9d92c[sr=1, bookingStatus=CNF ,GN, currentStatus=CNF],pnr.Xml$Passengers$Passenger@1fd0fafc[sr=2, bookingStatus=CNF ,GN, currentStatus=CNF],pnr.Xml$Passengers$Passenger@510dc6b5[sr=3, bookingStatus=CNF ,GN, currentStatus=CNF]}], error=]
200
pnr.Xml$Passengers$Passenger@1fd0fafc[sr=2, bookingStatus=CNF ,GN, currentStatus=CNF]