Friday, July 31, 2015

Working With Chrome Profile With Selenium WebDriver

Previously we learn how to create Firefox Profile and how to access this profile using Selenium WebDriver.
Here We learn how to create Chrome profile and use this profile from Selenium WebDriver.

1. Create Chrome Profile :
  •  Open Chrome Browser and click on Setting option.
  • Scroll down the page and click on Add person option.Give a name and click on Add button. If we check this option"Create a desktop shortcut for the user" then we can find a chrome shortcut for this profile.
  • After creation this profile we can find that a folder created under "Documents and Settings\Administrator\Local Settings\Application Data\Google\Chrome\User Data"
     
2. Accessing Chrome Profile Using Selenium WebDriver :
  • Download Chrome Driver from here .
  • Extact the .exe file from downloaded zip and put this file(.exe) in our project folder.
  • Add this .exe to System property using 
    System.setProperty("webdriver.chrome.driver", "./chromedriver.exe");
                                                         Or

    Using ChromeDriverService class for this.
    ChromeDriverService chSvc = new ChromeDriverService.Builder()
              .usingDriverExecutable(new File("./chromedriver.exe")).usingAnyFreePort().build();

  • Create ChromeOption object and add use addArguments() method to set the user profile.
         ChromeOptions chOption = new ChromeOptions();
         chOption.addArguments("user-data-dir=ChromeProfile Location");

  • Now pass ChromeDriverService and ChromeOption object during ChromeDriver object creation.
            WebDriver driver = new ChromeDriver(chSvc,chOption); 
Example Code: 

package chromeprofile;

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;

public class LoadChromeProfile {

    public static void main(String[] args) {
        //System.setProperty("webdriver.chrome.driver", "./chromedriver.exe");

        ChromeDriverService chSvc = new ChromeDriverService.Builder()
                .usingDriverExecutable(new File("./chromedriver.exe")).usingAnyFreePort().build();

        ChromeOptions chOption = new ChromeOptions();

        /**
         * "user-data-dir = profilepath" --to open profile
         * "--start-maximized" for maximize the browser
         */

        chOption.addArguments("user-data-dir = Our Profile Path");
        chOption.addArguments("--start-maximized");        WebDriver driver = new ChromeDriver(chSvc, chOption);
        driver.quit();
    }
}

 

Sunday, June 21, 2015

Validate XML With Referenced XSD File Using SAX Parser

Previously we learn how to validated a XML file with reference or internal schema using DOM parser. Here we do the same thing but using SAX parser.

Step 1:

  Create a  SAXParserFactory object.
      SAXParserFactory saxFactory = SAXParserFactory.newInstance();  

Step 2:
  Set SAXParserFactory object as NameSpaceAware true.
      saxFactory.setNamespaceAware(true);

Step 3:
  Set SAXParserFactory object as Validating true.
      saxFactory.setValidating(true);

Step 4:
   Set SAXParser Property.
      parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                    "http://www.w3.org/2001/XMLSchema");


Step 5:
  Set ErrorHandler to XMLReader object.
                xmlReader.setErrorHandler(
                    new ErrorHandler() {
                        @Override
                        public void warning(SAXParseException e) throws SAXException {
                            System.out.println("WARNING: " + e.getMessage()); // do nothing
                        }
                       
                        @Override
                        public void error(SAXParseException e) throws SAXException {
                            System.out.println("ERROR: " + e.getMessage());
                            throw e;
                        }
                       
                        @Override
                        public void fatalError(SAXParseException e) throws SAXException {
                            System.out.println("FATAL: " + e.getMessage());
                            throw e;
                        }
                    });


Step 6:
  Parse XML file .
    xmlReader.parse("file.xml");

Our complete code looks like that:

package SAXParser;

import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;


public class SAXParserValidation {

    public static void main(String[] args) {
        try {
            SAXParserFactory saxFactory = SAXParserFactory.newInstance();
            saxFactory.setValidating(true);
            saxFactory.setNamespaceAware(true);
            SAXParser parser = saxFactory.newSAXParser();
            parser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
                    "http://www.w3.org/2001/XMLSchema");
           
            XMLReader xmlReader = parser.getXMLReader();
            xmlReader.setErrorHandler(
                    new ErrorHandler() {
                        @Override
                        public void warning(SAXParseException e) throws SAXException {
                            System.out.println("WARNING: " + e.getMessage()); // do nothing
                        }
                       
                        @Override
                        public void error(SAXParseException e) throws SAXException {
                            System.out.println("ERROR: " + e.getMessage());
                            throw e;
                        }
                       
                        @Override
                        public void fatalError(SAXParseException e) throws SAXException {
                            System.out.println("FATAL: " + e.getMessage());
                            throw e;
                        }
                    });
            xmlReader.parse("file.xml");
        } catch (ParserConfigurationException | SAXException | IOException e) {
            System.out.println("!!!!!!!!!!!!!!!Error!!!!!!!!!!!!" + e.getMessage());
        }
    }
   


 XML File :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students  xsi:noNamespaceSchemaLocation="file.xsd"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <student id="1">
    <fname>TestFirstName</fname>
    <lname>TestLastName</lname>
    <sectionname rollno="1">A</sectionname>
  </student>
</students>




XML Schema :

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="students" type="studentsType"/>
  <xs:complexType name="sectionnameType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:byte" name="rollno"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="studentType">
    <xs:sequence>
      <xs:element type="xs:string" name="fname"/>
      <xs:element type="xs:string" name="lname"/>
      <xs:element type="sectionnameType" name="sectionname"/>
    </xs:sequence>
    <xs:attribute type="xs:byte" name="id"/>
  </xs:complexType>
  <xs:complexType name="studentsType">
    <xs:sequence>
      <xs:element type="studentType" name="student"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Validate XML With Referenced XSD File Using DOM Parser

On the last post we learn how to validated a XML file against an external XML schema file. Now we try to learn how to validate a XML file against referenced or internal schema definition.

Step 1 :

Create DocumentBuilderFactory object as given below.
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); 

Step 2 :

Set DocumentBuilderFactory object as NameSpace Aware true.
     docFactory.setNamespaceAware(true);

Step 3:

Set DocumentBuilderFactory object as Validating true.
     docFactory.setValidating(true);

Until we set docFactory as NameSpaceAware it will not validate the XML file with referenced Schema if we only set the Validating as true.

Step 4:

Set Attribute on DocumentBuilderFactory.

 If schemalocation specified in the XML Document using the schemaLocation or  noNamespaceSchemaLocation attributes then use
    docFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
              "http://www.w3.org/2001/XMLSchema");


If schemalocation specified by the 'http://java.sun.com/xml/jaxp/properties/schemaSource' property then use
    docFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
              "http://www.w3.org/2001/XMLSchema");


    docFactory.setAttribute( "http://java.sun.com/xml/jaxp/properties/schemaSource", "file:test2.xsd");


Step 5:

Set ErrorHandler to DocumentBuilderFactory object. It is optional,  it is required if we try to catch different level of parsing error like warning, error, fatal error etc.

We can set this on DocumentBuilder Object like this.
      docBuilder.setErrorHandler(new DefaultErrorHandler());

Or if we want to implement our own then it looks like this.
            docBuilder.setErrorHandler(
                    new ErrorHandler() {
                        @Override
                        public void warning(SAXParseException e) throws SAXException {
                            System.out.println("WARNING: " + e.getMessage()); // do nothing
                        }

                        @Override
                        public void error(SAXParseException e) throws SAXException {
                            System.out.println("ERROR: " + e.getMessage());
                            throw e;
                        }

                        @Override
                        public void fatalError(SAXParseException e) throws SAXException {
                            System.out.println("FATAL: " + e.getMessage());
                            throw e;
                        }
                    });


Step 6 :

Now parse the XML file.
    docBuilder.parse(new File("file.xml"));

Our complete code looks like that:

package domparsing;

import com.sun.org.apache.xml.internal.utils.DefaultErrorHandler;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class DOMValidation {

    /**
     * @param args the command line arguments
     * @throws javax.xml.parsers.ParserConfigurationException
     * @throws org.xml.sax.SAXException
     * @throws java.io.IOException
     */
    public static void main(String[] args) {
        try {
            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            docFactory.setNamespaceAware(true);
            docFactory.setValidating(true);
            /**
             * This line is required if We setValidating as true
             * but no noNamespaceSchemaLocation defined in XML file.
             */

            //docFactory.setFeature("http://apache.org/xml/features/validation/dynamic", true);
            docFactory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
              "http://www.w3.org/2001/XMLSchema");
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            //docBuilder.setErrorHandler(new DefaultErrorHandler());

            docBuilder.setErrorHandler(
                    new ErrorHandler() {
                        @Override
                        public void warning(SAXParseException e) throws SAXException {
                            System.out.println("WARNING: " + e.getMessage()); // do nothing
                        }

                        @Override
                        public void error(SAXParseException e) throws SAXException {
                            System.out.println("ERROR: " + e.getMessage());
                            throw e;
                        }

                        @Override
                        public void fatalError(SAXParseException e) throws SAXException {
                            System.out.println("FATAL: " + e.getMessage());
                            throw e;
                        }
                    });
            /**
             * Commented line required if we want to
             * show the output after parsing it.One is
             * for using DOM and another is one using STAX.
             */

            //TransformerFactory.newInstance().newTransformer().transform(new StreamSource("file.xml"), new StreamResult(System.out));
            //TransformerFactory.newInstance().newTransformer().transform(new DOMSource(docBuilder.parse(new File("file.xml"))),new StreamResult(System.out));
            docBuilder.parse(new File("file.xml"));
        } catch (IOException | SAXException | ParserConfigurationException | IllegalArgumentException e) {
            System.out.println("!!!!!!!!!!!!!!!Error!!!!!!!!!!!!" + e.getMessage());
        }
    }
}


XML File :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students  xsi:noNamespaceSchemaLocation="file.xsd"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <student id="1">
    <fname>TestFirstName</fname>
    <lname>TestLastName</lname>
    <sectionname rollno="1">A</sectionname>
  </student>
</students>




XML Schema :

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="students" type="studentsType"/>
  <xs:complexType name="sectionnameType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:byte" name="rollno"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="studentType">
    <xs:sequence>
      <xs:element type="xs:string" name="fname"/>
      <xs:element type="xs:string" name="lname"/>
      <xs:element type="sectionnameType" name="sectionname"/>
    </xs:sequence>
    <xs:attribute type="xs:byte" name="id"/>
  </xs:complexType>
  <xs:complexType name="studentsType">
    <xs:sequence>
      <xs:element type="studentType" name="student"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

Saturday, June 13, 2015

XML Validation And WellFormedNess Checking In Java

Previously we learn how to read and write a XML with different parser. Now we will concentrate how to check the wellformedness and validity of a XML file.

Now question is what is wellformedness and what is the validation of a XML file.

  1. WellFormedNess  : It is related with XML syntax of an XML document for an example : XML document must have a single root element, the elements must be properly nested, tag names cannot begin with a number or contain certain characters, and so on.
  2. Valid : A valid XML document means that the document complies with the rules of a particular document specification.This can be done with DTD(Document Type Definition) or Schema. Before a document can be valid to a standard, it must start by being well-formed. An XML document cannot be valid until it is well-formed

WellFormedNess checked in java when parse() method called. So we don't need to do any extra thing for that.

Now we go to find the way how to validate the xml file.

There are two way because some time schema definition is defined into the XML file then we need to use internal XML Validation and some time we need to validate the XML file with external schema file when schema definition is not defined into the XML file.

Here we will try to learn how to validate a XML file against an external XML Schema file.

Step 1: 
    
     Set Schema Language
        String schemaLanguage=XMLConstants.W3C_XML_SCHEMA_NS_URI;
        

Step 2:

     Create SchemaFactory Object
        SchemaFactory schemaFactory=SchemaFactory.newInstance(schemaLanguage);
        

Step 3:

      Create Schema Object
          Schema schema=schemaFactory.newSchema(new File("XSD FilePath"));
       
      If we have multiple schema file then use Source Object array

         Schema schema=schemaFactory.newSchema(new StreamSource[]{new StreamSource(new File("XSD FilePath"))});


Step 4:


     Create Validator object
        Validator xmlValidator= schema.newValidator();

Step 5:


     Now call validate method and pass XML file as a parameter to validate. 
        xmlValidator.validate(new StreamSource(new File("XML FilePath")));

Complete Code Example :


import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

public class SchemaValidationExample {

    public static void main(String[] args) {
        try {
            /**
             * Define Schema Language.
             */

            String schemaLanguage = XMLConstants.W3C_XML_SCHEMA_NS_URI;
           
            /**
             * Create SchemaFactory Object.
             */

            SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
           
            /**
             * Create Schema Object,here we pass Schema File
             * If we require multiple Schema File then use
             * StreamSource object array.
             */

            Schema schema = schemaFactory.newSchema(new File("file.xsd"));
           
            //To valide XML against multiple xchema file.
            //Schema schema=schemaFactory.newSchema(new StreamSource[]{new StreamSource(new File("file.xsd"))});
           
            /**
             * Create Validator Object.
             */
            Validator xmlValidator = schema.newValidator();

           
            /**
             * Validate XML File.
             */

            xmlValidator.validate(new StreamSource(new File("file.xml")));
           
        } catch (SAXException |IOException ex) {
            Logger.getLogger(SchemaValidationExample.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
 

 
XSD File :
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified">
  <xs:element name="students" type="studentsType"/>
  <xs:complexType name="sectionnameType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:byte" name="rollno"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="studentType">
    <xs:sequence>
      <xs:element type="xs:string" name="fname"/>
      <xs:element type="xs:string" name="lname"/>
      <xs:element type="sectionnameType" name="sectionname"/>
    </xs:sequence>
    <xs:attribute type="xs:byte" name="id"/>
  </xs:complexType>
  <xs:complexType name="studentsType">
    <xs:sequence>
      <xs:element type="studentType" name="student"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
        

XML File :

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students xsi:noNamespaceSchemaLocation="file.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <student id="1">
    <fname>TestFirstName</fname>
    <lname>TestLastName</lname>
    <sectionname rollno="1">A</sectionname>
  </student>
</students>

 
     

Saturday, June 6, 2015

Writing XML File With STAX Stream Parser

Here we try to learn how to use STAX Stream Parser in XML writing. We know that STAX stream parser is consuming lower memory and more efficient than STAX Event Parser . Now we follow this steps to create a XML file using STAX Stream Parser.

Step : 1
       Create XMLOutputFactory object.
          XMLOutputFactory xof = XMLOutputFactory.newFactory();

Step : 2 
        Create XMLStreamWriter object.

           XMLStreamWriter writer = xof.createXMLStreamWriter(new FileWriter("C:\\file.xml"));

 Step : 3
         Start Document
            writer.writeStartDocument();

         Start Element
           writer.writeStartElement("students");

         Add Attribute
            writer.writeAttribute("id", "1");

         Add Character in XML Node
            writer.writeCharacters("TestLaststName");

        End Element
             writer.writeEndElement();

        End Document
             writer.writeEndDocument();

Step : 4
      Flush XMLStreamWriter object and close this object.
        
          writer.flush();
          writer.close();


Complete Code Is Given Below :


package STAXParser;

import com.sun.xml.internal.txw2.output.IndentingXMLStreamWriter;
import java.io.FileWriter;
import java.io.IOException;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.stream.StreamResult;

public class STAXStreamWriter {

    public static void main(String[] args) throws IOException, XMLStreamException, TransformerConfigurationException, TransformerException {
        /**
         * Create XMLOutputFactory Object.
         */

        XMLOutputFactory xof = XMLOutputFactory.newFactory();

        /**
         * Create XMLStreamWriter Object.
         */

        XMLStreamWriter writer = xof.createXMLStreamWriter(new FileWriter("C:\\file.xml"));
//        XMLStreamWriter writer = xof.createXMLStreamWriter(new StreamResult(System.out));
        /**
         * This is created for formatting.
         */

        writer = new IndentingXMLStreamWriter(writer);

        /**
         * Handling Start Document,Start Element,Add Attribute,
         * Add Character,EndDocument and EndElement.
         */

        writer.writeStartDocument();
       
        writer.writeStartElement("students");
       
        writer.writeStartElement("student");
        writer.writeAttribute("id", "1");

        writer.writeStartElement("fname");
        writer.writeCharacters("TestFirstName");
        writer.writeEndElement();

        writer.writeStartElement("lname");
        writer.writeCharacters("TestLaststName");
        writer.writeEndElement();

        writer.writeStartElement("section");
        writer.writeAttribute("rollno", "1");
        writer.writeCharacters("A");
        writer.writeEndElement();
       
        writer.writeEndElement();
        writer.writeEndElement();
        writer.writeEndDocument();

        /**
         * Flush XMLStreamWriter Object and Close.
         */

        writer.flush();
        writer.close();
    }
}


OutPut :
 

<?xml version="1.0" encoding="UTF-8"?>
<studens>
    <student id="1">
        <fname>TestFirstName</fname>
        <lname>TestLastName</lname>
        <section rollno="1">A</section>
    </student>
</studens>

Writing XML File Using STAX Event Parser

Using STAX parser is another way to write XML file. As we know STAX have two type of parsing technique one is Event or Iterator and another one is Stream or Cursor type,here STAX have same two types to write XML file.

Here we take a look on STAX event Parser, later we will know the another one.

Step 1 : 
      Create XMLOutputFactory object.
        XMLOutputFactory outFactory=XMLOutputFactory.newFactory();


Step 2 :
      Create XMLEventWriter object.
         XMLEventWriter xew=outFactory.createXMLEventWriter(new FileWriter("File Name"));

Step 3:
      Create XMLEventFactoryobject.
         XMLEventFactory evntFactory=XMLEventFactory.newFactory();

Step 4 :
      Start Document
         xew.add(evntFactory.createStartDocument());
     
      StartElement
        xew.add(evntFactory.createStartElement("", "", "Element Name"));   

      Add Attribute
        xew.add(evntFactory.createAttribute("Attribute Name", "Attribute Value"));

      Add Character
        xew.add(evntFactory.createCharacters("XML Node Character Value"));
 
     Close Element
        xew.add(evntFactory.createEndElement("", "", "Element Name Which we want to close"));
     
     Close Document 
        xew.add(evntFactory.createEndDocument());

Step 5 :
     Flush the writer object.
        xew.flush();

Step 6 :
     Close writer object.
        xew.close();
       

Complete Code Given Below :

package STAXParser;

import java.io.FileWriter;
import java.io.IOException;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;

public class STAXEventWriter {

    public static void main(String[] args) throws IOException, XMLStreamException {
        

        /**
         * Create XMLOutputFactory object.
         */

 
        XMLOutputFactory outFactory=XMLOutputFactory.newFactory();
        

        /**
         * Create XMLEventWriter object.
         */

 
        XMLEventWriter xew=outFactory.createXMLEventWriter(new FileWriter("C:\\file.xml"));
        //XMLEventWriter xew=outFactory.createXMLEventWriter(System.out);


        /**
         * Create XMLEventFactory object.
         */

        XMLEventFactory evntFactory=XMLEventFactory.newFactory();
       
        /**
         * This are created to decorate XML file.
         */

 
        XMLEvent end = evntFactory.createDTD("\n");
        XMLEvent tab = evntFactory.createDTD("\t");
       
        /**
         * Handling Stat Document, CreateElement, Add Attribute
         * Create Characters, Close Element and close Document.
         */

        xew.add(evntFactory.createStartDocument());
        xew.add(end);
       
        xew.add(evntFactory.createStartElement("", "", "studens"));
        xew.add(end);
        xew.add(tab);
       
        xew.add(evntFactory.createStartElement("", "", "student"));
        xew.add(evntFactory.createAttribute("id", "1"));
        xew.add(end);
        xew.add(tab);
        xew.add(tab);
       
        xew.add(evntFactory.createStartElement("", "", "fname"));
        xew.add(evntFactory.createCharacters("TestFirstName"));
        xew.add(evntFactory.createEndElement("", "", "fname"));
        xew.add(end);
        xew.add(tab);
        xew.add(tab);
       
        xew.add(evntFactory.createStartElement("", "", "lname"));
        xew.add(evntFactory.createCharacters("TestLastName"));
        xew.add(evntFactory.createEndElement("", "", "lname"));
        xew.add(end);
        xew.add(tab);
        xew.add(tab);
       
        xew.add(evntFactory.createStartElement("", "", "section"));
        xew.add(evntFactory.createAttribute("rollno", "1"));
        xew.add(evntFactory.createCharacters("A"));
        xew.add(evntFactory.createEndElement("", "", "section"));
        xew.add(end);
        xew.add(tab);
       
        xew.add(evntFactory.createEndElement("", "", "student"));
        xew.add(end);
       
        xew.add(evntFactory.createEndElement("", "", "students"));
        xew.add(evntFactory.createEndDocument());
       
        /**
         * Flush writer object and Close this object.
         */

        xew.flush();
        xew.close();
    }
}


OutPut :
 

<?xml version="1.0" encoding="UTF-8"?>
<studens>
    <student id="1">
        <fname>TestFirstName</fname>
        <lname>TestLastName</lname>
        <section rollno="1">A</section>
    </student>
</studens>


Saturday, May 23, 2015

Writing XML File Using DOM Parser

Still now we learn how to read XMLfile using DOM, SAX and STAX parser and we know that only DOM and STAX have the ability to write XML file.Here we learn how to write XML file using DOM and later we know how to write XML File using STAX parser.

Step 1:
    Create DocumentBuilderFactory object .
      DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();

Step 2:

    Create DocumentBuilder object .
      DocumentBuilder domBuilder = domFactory.newDocumentBuilder();

Step 3:
    Create Document object .
      Document dom = domBuilder.newDocument();

Step 4:
    Create Element object .
      Element rootStart = dom.createElement("elementName")

Step 5:
   Add this object to dom .
      dom.appendChild(rootStart);

  If  this node is under any node then add this under any element object  
      rootStart.appendChild(rootStart)

Step 6: 
    Create Attribute Object and set value 
        Attr attribte = dom.createAttribute("attributeName");
        attribte.setValue("attributeValue");

    Add Attribute object with appropriate node
        root.setAttributeNode(attribte);

 

Step 7:
   Create TransformerFactory object
       TransformerFactory transFactory = TransformerFactory.newInstance();

Step 8:
    Create Transformer object.
        Transformer transformer = transFactory.newTransformer();

Step 9:
    Create DOMSource Object .
        DOMSource domSource = new DOMSource(dom);

Step 10:
    Create  StreamResult object and show the file into the console or save in file.

        //Show the file in console
        StreamResult console = new StreamResult(System.out);
        transformer.transform(domSource, console);


         //Save the XML File
        StreamResult xmlFile = new StreamResult(new File("File Path"));
        transformer.transform(domSource, xmlFile);

   
Example Code Given Below :


package domparsing;

import java.io.File;
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;


public class DOMWriterExample {

    public static void main(String[] args) throws ParserConfigurationException, TransformerConfigurationException, TransformerException {
        /**
         * Create DocumentBuilderFactory Object.
         */

        DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
        /**
         * Create DocumentBuilder object.
         */

        DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
        /**
         * Create Document object.
         */

        Document dom = domBuilder.newDocument();
        /**
         * Create root element students and add to DOM .
         */

        Element rootStart = dom.createElement("students");
        dom.appendChild(rootStart);

        /**
         * Add student node under students node.
         */

        Element root = dom.createElement("student");
        rootStart.appendChild(root);

        /**
         * Adding Attribute in student node.
         */

        Attr attribte = dom.createAttribute("id");
        attribte.setValue("1");
        root.setAttributeNode(attribte);

        /**
         * Add fname,lname and section under student node and attribute also.
         */

        Element childNodeFirstName = dom.createElement("fname");
        childNodeFirstName.setTextContent("TestFirstName");
        root.appendChild(childNodeFirstName);

        Element childNodeLastName = dom.createElement("lname");
        childNodeLastName.setTextContent("TestLastName");
        root.appendChild(childNodeLastName);

        Element childNodeSectionName = dom.createElement("sectionname");
        childNodeSectionName.setAttribute("rollno", "1");
        childNodeSectionName.setTextContent("A");
        root.appendChild(childNodeSectionName);
        /**
         * Create TransformerFactory and set property.
         */

        TransformerFactory transFactory = TransformerFactory.newInstance();
        //transFactory.setAttribute("indent-number", 4);
        Transformer transformer = transFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        /**
         * Create DOMSource object and pass DOM object as a parameter.
         */

        DOMSource domSource = new DOMSource(dom);

        /**
         * Showing output on Console.
         */

        StreamResult console = new StreamResult(System.out);
        transformer.transform(domSource, console);
        /*
         StreamResult result = new StreamResult(new StringWriter());
         transformer.transform(domSource, result);
         String xmlString = result.getWriter().toString();
         System.out.println(xmlString);
         */


        /**
         * Storing XML into the file.
         */

        StreamResult xmlFile = new StreamResult(new File("C:\\file.xml"));
        transformer.transform(domSource, xmlFile);
    }
}

 


 

Unknown XML Parsing With STAX

On our last discussion we can now say that we know how to parse a XML file using STAX parser.Now here we try to parse a XML file with unknown structure.It has similar logic which we use in SAX parser to parse a unknown XML.We get the text when Starting Element and End Element name matched.

Code is Given Below For Both Type Of STAX Parser :

Event Or Iterator STAX
----------------------------------------------------------------------------------------------


package STAXParser;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.XMLEvent;

public class STAXEventParserExample {

    public static void main(String[] args) throws FileNotFoundException, XMLStreamException, UnsupportedEncodingException {
        String startElement = null;
        String endElement = null;
        String elementTxt = null;
        Map<String, String> tmpAtrb = null;
        /**
         * Create XMLInputFactory Factory.
         */

        XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
        /**
         * Create XMLEventReader.
         */

        XMLEventReader xmlEvtReader = xmlFactory.createXMLEventReader(new InputStreamReader(new FileInputStream("c:\\file.xml"), "UTF-8"));
        /**
         * Iterate until last Element in XML.
         */

        while (xmlEvtReader.hasNext()) {
            /**
             * Move To Next XML Element And Check Element Type.
             */

            XMLEvent evnt = xmlEvtReader.nextEvent();
            if (evnt.isStartElement()) {
                tmpAtrb=new HashMap();
                startElement = evnt.asStartElement().getName().getLocalPart();
                for (Iterator<Attribute> iterator = evnt.asStartElement().getAttributes(); iterator.hasNext();) {
                    Attribute attribute = iterator.next();
                    String aname = attribute.getName().getLocalPart();
                    String value = attribute.getValue();
                    tmpAtrb.put(aname, value);
                }
            }
            if (evnt.isEndElement()) {
                endElement = evnt.asEndElement().getName().toString();
                if (startElement.equalsIgnoreCase(endElement)) {
                    System.out.println(" ElementName : " + startElement + " ElementText : " + elementTxt);
                    for (Map.Entry<String, String> entrySet : tmpAtrb.entrySet()) {
                        System.out.println(" Attribute Name :" + entrySet.getKey() + " Attribute Value :" + entrySet.getValue());
                    }
                }
            }
            if (evnt.isCharacters()) {
                elementTxt = (evnt.asCharacters().getData().contains("\n")) ? "" : evnt.asCharacters().getData();
            }
        }
        /**
         * Close XMLEventReader
         */

        xmlEvtReader.close();
    }
}
 

Stream Or Cursor STAX
----------------------------------------------------------------------------------------------
package STAXParser;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class STAXStreamParserExample {

    public static void main(String[] args) throws FileNotFoundException, XMLStreamException {
        String startElement = null;
        String endElement = null;
        String elementTxt = null;
        Map<String, String> tmpAtrb = null;
        /**
         * Create XMLInputFactory Factory.
         */

        XMLInputFactory xf = XMLInputFactory.newInstance();
        /**
         * Create XMLStreamReader.
         */

        XMLStreamReader xsr = xf.createXMLStreamReader(new InputStreamReader(new FileInputStream("c:\\file.xml")));
        /**
         * Iterate until last Element in XML.
         */

        while (xsr.hasNext()) {
            /**
             * Move To Next XML Element And Check Element Type.
             */

            int e = xsr.next();
            if (e == XMLStreamConstants.START_ELEMENT) {
                startElement = xsr.getLocalName();
                tmpAtrb = new HashMap();
                for (int i = 0; i < xsr.getAttributeCount(); i++) {
                    String aname = xsr.getAttributeLocalName(i);
                    String value = xsr.getAttributeValue(i);
                    tmpAtrb.put(aname, value);
                }
            }

            if (e == XMLStreamConstants.END_ELEMENT) {
                endElement = xsr.getLocalName();
                if (startElement.equalsIgnoreCase(endElement)) {
                    System.out.println(" ElementName : " + startElement + " ElementText : " + elementTxt);
                    for (Map.Entry<String, String> entrySet : tmpAtrb.entrySet()) {
                        System.out.println(" Attribute Name :" + entrySet.getKey() + " Attribute Value :" + entrySet.getValue());
                    }
                }
            }
            if (e == XMLStreamConstants.CHARACTERS) {
                elementTxt = (xsr.getText().contains("\n")) ? "" : xsr.getText();
            }
        }
        /**
         * Close XMLEventReader
         */

        xsr.close();
    }
}
 


Sunday, May 17, 2015

Reading XML With STAX Stream Or Cursor API

STAX Stream or Cursor API is a low level pull based API for parsing XML.Its' need less memory than Event or Iterator API because it does not require to create object for each event.

We need to follow this steps to parse a XML file
Step 1:
    Create XMLInputFactory object
         XMLInputFactory xf=XMLInputFactory.newInstance();

Step 2:
     Create XMLStreamReader object  for the input XML.
          XMLStreamReader xsr=xf.createXMLStreamReader(new InputStreamReader(File));

Step 3:
    Iterate the XMLStreamReader object  and identify the various elements in the document
          while (xsr.hasNext()) {
              int e = xsr.next();

          }

Step 4: 
    After reading the entire document , close the XMLStreamReader object.
         xsr.close(); 

Sample Example Code Given Below :


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class STAXStreamParserExample {

    public static void main(String[] args) throws FileNotFoundException, XMLStreamException {
        /**
         * Create XMLInputFactory Factory.
         */

        XMLInputFactory xf = XMLInputFactory.newInstance();
        /**
         * Iterate until last Element in XML.
         */

        XMLStreamReader xsr = xf.createXMLStreamReader(new InputStreamReader(new FileInputStream("c:\\file.xml")));
        /**
         * Iterate until last Element in XML.
         */

        while (xsr.hasNext()) {
            /**
             * Move To Next XML Element And Check Element Type.
             */

            int e = xsr.next();
            if (e == XMLStreamConstants.START_ELEMENT) {
                System.out.println("StartElement Name :" + xsr.getLocalName());
            }
            if (e == XMLStreamConstants.END_ELEMENT) {
                System.out.println("EndElement Name :" + xsr.getLocalName());
            }
            if (e == XMLStreamConstants.CHARACTERS) {
                System.out.println("Element TextValue :" + xsr.getText());
            }
        }
        /**
         * Close XMLEventReader
         */

        xsr.close();
    }
}

 

Reading XML With STAX Event Or Iterator API

STAX Parser is modern parser which is act as pull based parser, means application have control over this parser how and when read the next tag on xml file. It have another advantage that using this parser we can also write a xml file.

STAX is event base xml parser API so it will not load the whole xml file into the memory so we can safe from memory out of bound exception.

STAX API have 2 type.

 1. Event Or Iterator base
 2. Stream Or Cursor base

We will learn here STAX Event or Iterator base API.

Step 1:
  Create and XMLInputFactory instance.
      
        XMLInputFactory xmlFactory=XMLInputFactory.newInstance();

Step 2:
  Create XMLEventReader instance with FileInputStream of input file as argument.
        XMLEventReader xmlEvtReader=xmlFactory.createXMLEventReader("File");
Step 3:
  Iterate the XMLEventReader object  and identify the various elements in the document
       while (xmlEvtReader.hasNext()) {
            XMLEvent evnt = xmlEvtReader.nextEvent();

        }

Step 4:
  After reading the entire document , close the XMLEventReader object.
       xmlEvtReader.close();

Sample Example Code Given Below :


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;


public class STAXEventParserExample {

    public static void main(String[] args) throws FileNotFoundException, XMLStreamException, UnsupportedEncodingException {
        /**
         * Create XMLInputFactory Factory.
         */

        XMLInputFactory xmlFactory=XMLInputFactory.newInstance();
        /**
         * Create XMLEventReader.
         */

        XMLEventReader xmlEvtReader=xmlFactory.createXMLEventReader(new InputStreamReader(new FileInputStream("c:\\file.xml"), "UTF-8"));
        /**
         * Iterate until last Element in XML.
         */

        while (xmlEvtReader.hasNext()) {
            /**
             * Move To Next XML Element And Check Element Type.
             */

            XMLEvent evnt = xmlEvtReader.nextEvent();
            if(evnt.isStartElement()){
                System.out.println(evnt.asStartElement().getName());
            }
            if(evnt.isEndElement()){
                System.out.println(evnt.asEndElement().getName());
            }
            if(evnt.isCharacters()){
                System.out.println(evnt.asCharacters().getData());
            }
        }
        /**
         * Close XMLEventReader
         */

        xmlEvtReader.close();
    }   
}

Saturday, May 9, 2015

Unknown XML Structure Parsing Using SAX Parser

We learn how to parse a XML file using SAX parser previously but there we know the XML file structure means node structure.

Now we concentrate how to parse a XML file when we not aware of the structure of a XML file using SAX parser.

It can be possible if we do some modification on DefaultHandler part.

Here we parse a xml in such a manner where we find only the child element at last depth means, this node does not contains any child element .We also retrive attributes of this element.
So logic is that if we find that start element name follows the end element name  then we print the value of this node and attribute value.

Example code given below:


package SAXParser;


import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class SAXParserExample {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, VerifyError {

        /**
         * We can pass the class name of the XML parser
         * to the SAXParserFactory.newInstance().
         */


        //SAXParserFactory saxDoc = SAXParserFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl", null);

        SAXParserFactory saxDoc = SAXParserFactory.newInstance();
        SAXParser saxParser = saxDoc.newSAXParser();

        DefaultHandler handler = new DefaultHandler() {
            String tmpElementName = null;
            String tmpElementValue = null;
            Map<String,String> tmpAtrb=null;

            @Override
            public void startElement(String uri, String localName, String qName, 

                                                                Attributes attributes) throws SAXException {
                tmpElementValue = "";
                tmpElementName = qName;
                tmpAtrb=new HashMap();
                //System.out.println("Start Element :" + qName);

                /**
                 * Store attributes in HashMap
                 */

                for (int i=0; i<attributes.getLength(); i++) {
                    String aname = attributes.getLocalName(i);
                    String value = attributes.getValue(i);
                    tmpAtrb.put(aname, value);
                }

            }

            @Override
            public void endElement(String uri, String localName, String qName) 

                                                        throws SAXException {
              
                if(tmpElementName.equals(qName)){
                    System.out.println("Element Name :"+tmpElementName);


                /**
                 * Retrive attributes from HashMap
                 */
                    for (Map.Entry<String, String> entrySet : tmpAtrb.entrySet()) {
                        System.out.println("Attribute Name :"+ entrySet.getKey() + "Attribute Value :"+ entrySet.getValue());
                    }
                    System.out.println("Element Value :"+tmpElementValue);
                }
            }

            @Override
            public void characters(char ch[], int start, int length) throws SAXException {
                tmpElementValue = new String(ch, start, length) ;
              
            }
        };
      
        /**
         * Below two line used if we use SAX 2.0
         * Then last line not needed.
         */

      
        //saxParser.setContentHandler(handler);
        //saxParser.parse(new InputSource("c:/file.xml"));


        saxParser.parse(new File("c:/file.xml"), handler);
    }
}