1
Parse Decrypted Data
  • Now , we have the decrypted XML File. How to parse the data from the XML File.

  • Two options - JAXB or Simple Parsing using DOM or SAX Parser.

  • How JAXB works ?

  • UnMarshalling from XML to Java Objects

  • Marshalling from Java Objects to XML

  • <?xml version="1.0" encoding="utf-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="
    creditcardpayment">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="paymentamount" type="xs:string"/>
    <xs:element name="paymentdate" type="xs:string"/>
    <xs:element name="paymentdesc" type="xs:string"/>
    <xs:element ref="customer" minOccurs="1" maxOccurs="1"/>
    <xs:element ref="creditcardinfo" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="payerid" type="xs:positiveInteger"/>
    </xs:complexType>
    </xs:element>

    <xs:element name="
    customer">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="firstname" type="xs:string"/>
    <xs:element name="lastname" type="xs:string"/>
    <xs:element ref="address" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
    <xs:attribute name="customerid" type="xs:positiveInteger"/>
    </xs:complexType>
    </xs:element>

    <xs:element name="
    address">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="street1" type="xs:string"/>
    <xs:element name="street2" type="xs:string"/>
    <xs:element name="city" type="xs:string"/>
    <xs:element name="state" type="xs:string"/>
    <xs:element name="zip" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    <xs:element name="
    creditcardinfo">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="cardnumber" type="xs:string"/>
    <xs:element name="cardtype" type="xs:string"/>
    <xs:element name="expirydate" type="xs:string"/>
    <xs:element name="securitycode" type="xs:string"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    </xs:schema>

  • After running the JAXB compiler, following Java interfaces and their implementation classes are generated
       Address, AddressType, Creditcardinfo, CreditcardinfoType, Creditcardpayment,  CreditcardpaymentType, Customer, CustomerType

  • How to unmarshal data from the XML file to the Java Objects ?

  • public class CreditCardProcessor {

    private String packageName = "generated";
    private String xmlFileName = "";
    private JAXBContext jaxbContext;
    private Unmarshaller unmarshaller;
    private File file;
    private Creditcardpayment creditCardpayment;

    CreditCardProcessor(String xmlFile) {
    this.xmlFileName = xmlFile;
    System.out.println("Inside CreditCardProcessor " + this.xmlFileName);
    createCreditcardpayment();
    }

    private void createCreditcardpayment() {
    try
    {
    createContext();
    createUnmarshaller();
    createFile();
    unmarshalFile();
    }
    catch (JAXBException e) {
    System.out.println("There has been a problem either creating the "
    + "context for package '" + packageName +
    "', creating an unmarshaller for it, or unmarshalling the '" +
    xmlFileName + "' file. Formally, the problem is a " + e);
    }
    catch (FileNotFoundException e) {
    System.out.println("There has been a problem locating the '" +
    xmlFileName + "' file. Formally, the problem is a " + e);
    }

    }

    private void createContext() throws JAXBException {
    try
    {
    System.out.println("Before creating Context ");
    jaxbContext = JAXBContext.newInstance(packageName);
    System.out.println("Context created");
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }

    private void createUnmarshaller() throws JAXBException {
    System.out.println("Inside createUnmarshaller" );
    try
    {
    unmarshaller = jaxbContext.createUnmarshaller();
    unmarshaller.setValidating(true);
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }

    }

    private void createFile() throws FileNotFoundException{
    file = new File(xmlFileName);
    if (!file.exists()) throw new FileNotFoundException();
    }

    private void unmarshalFile() throws JAXBException {
    System.out.println("Inside unmarshalFile" );
    creditCardpayment = (Creditcardpayment) unmarshaller.unmarshal(file);
    }

    public Creditcardpayment readCreditcardpayment() {

    return creditCardpayment; //Client creates an instance of this class and invokes this method

    }
    }