Showing posts with label JAXP. Show all posts
Showing posts with label JAXP. Show all posts

July 20, 2011

Creating a new XML document with DOM Parser and persisting the DOM Tree

The following code example demonstrates the power of the DOM Parser by creating an XML document on-the-fly with the create methods from the DOM Parser API - something that is not possible to do with a SAX Parser. The code also shows how to take a DOM Tree and convert it to a String (XML content). This String can then be persisted to an XML document file.

package info.icontraining.parsers;

import java.io.StringWriter;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.xml.sax.*;

public class DOMParserNewDocument implements ErrorHandler {
   public static void main(String[] args) throws TransformerException {

      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

      try {
         factory.setNamespaceAware(true);
         factory.setValidating(true);

         DocumentBuilder dom = factory.newDocumentBuilder();            
         dom.setErrorHandler(new DOMParserNewDocument());
         Document doc = dom.newDocument();

         Element invoiceElement = doc.createElement("Invoice");
         Attr invoiceNumberAttr = doc.createAttribute("invoice-number");
         invoiceNumberAttr.setNodeValue("123456");
         invoiceElement.setAttributeNode(invoiceNumberAttr);
         doc.appendChild(invoiceElement);

         Element dateElement = doc.createElement("date");         
         Element monthElement = doc.createElement("month");         
         Element dayElement = doc.createElement("day");
         Element yearElement = doc.createElement("year");

         monthElement.appendChild(doc.createTextNode("July"));
         dayElement.appendChild(doc.createTextNode("22"));
         yearElement.appendChild(doc.createTextNode("2011"));

         dateElement.appendChild(monthElement);          
         dateElement.appendChild(dayElement);
         dateElement.appendChild(yearElement);

         invoiceElement.appendChild(dateElement);  
         // convert DOM tree to XML string
   
         Transformer transformer =
                        TransformerFactory.newInstance().newTransformer();
         transformer.setOutputProperty(OutputKeys.INDENT, "yes");

         StreamResult result = new StreamResult(new StringWriter());
         DOMSource source = new DOMSource(doc);
         transformer.transform(source, result);

         String xmlString = result.getWriter().toString();
         System.out.println(xmlString);
     
      } catch (ParserConfigurationException e) {
         e.printStackTrace();
      } 
   }
 
   public void fatalError(SAXParseException err)
        throws SAXException {
      System.out.println("** Fatal Error"
                          + ", line " + err.getLineNumber()
                          + ", uri " + err.getSystemId());
      System.out.println(" " + err.getMessage());
   }

   public void error(SAXParseException err)        
         throws SAXParseException {
      System.out.println("** Error"
                          + ", line " + err.getLineNumber()
                          + ", uri " + err.getSystemId());
      System.out.println(" " + err.getMessage());
   }

   public void warning(SAXParseException err)         
        throws SAXParseException {
      System.out.println("** Warning"
                          + ", line " + err.getLineNumber()
                          + ", uri " + err.getSystemId());
      System.out.println(" " + err.getMessage());
   }
}

Parsing XML with a DOM Parser

The DOM Parser in the example code below is a validating parser. The parser code, processes the element, attribute and the text nodes of the document and prints them to the console (while ignoring the rest of the XML artifacts). It does not modify the XML document eventhough it maintains an in-memory representation of the XML Document.

package info.icontraining.parsers;

import java.io.IOException;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;

public class DOMParserWithDTD implements ErrorHandler {
 
   public static void main(String[] args) throws SAXException, IOException  {
  
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  
      try {
         factory.setNamespaceAware(true);
         factory.setValidating(true);
         factory.setFeature("http://apache.org/xml/features/validation/schema", true);
         factory.setIgnoringElementContentWhitespace(true);
   
         DocumentBuilder dom = factory.newDocumentBuilder();
         dom.setErrorHandler(new TestDOMParser());
         Document doc = dom.parse("Invoice.xml");
   
         processNode(doc);

      } catch (ParserConfigurationException e) {
         e.printStackTrace();
      }     
   } 
   
   private static void processNode(Node node) {

      NodeList list = node.getChildNodes();   
      for(int i=0; i<list.getLength(); i++) {
 
         if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
            processElementNode(list.item(i));
            processNode(list.item(i));
         }
   
         if (list.item(i).getNodeType() == Node.TEXT_NODE) {
            processTextNode(list.item(i));
         }
      }
   }
 
   private static void processTextNode(Node text) {  
      if (text.getNodeValue().trim().length() != 0)
         System.out.println(text.getNodeValue().trim());
   }    

   private static void processElementNode(Node element) {
  
      String temp = "";
      temp = temp + "<" + element.getNodeName();
  
      if (element.hasAttributes()) {
         NamedNodeMap map = element.getAttributes();
        
         for(int i=0; i<map.getLength(); i++) {
            temp = temp + " " + map.item(i).getNodeName();
            temp = temp + "=\"" + map.item(i).getNodeValue() + "\"";
         }
      }
  
      temp = temp + ">";
      System.out.println(temp);
   }
 
   public void fatalError(SAXParseException err) throws SAXException {

      System.out.println("** Fatal Error" 
                          + ", line " + err.getLineNumber() 
                          + ", uri " + err.getSystemId());
      System.out.println(" " + err.getMessage());
   }     

   public void error(SAXParseException err) throws SAXParseException {
      System.out.println("** Error" 
                          + ", line " + err.getLineNumber() 
                          + ", uri " + err.getSystemId());
      System.out.println(" " + err.getMessage());
   }

   public void warning(SAXParseException err) throws SAXParseException {
      System.out.println("** Warning" 
                          + ", line " + err.getLineNumber()              
                          + ", uri " + err.getSystemId());
      System.out.println(" " + err.getMessage());
   }
}

April 7, 2011

SAX Parser in Java

The code example below is of a SAX Parser in Java that is a validating parser (validation by DTD but not by W3C Schema), but not a namespace-aware parser.

To make the parser namespace-aware, add any one of the following lines of code,

factory.setFeature("http://xml.org/sax/features/namespaces", true);

OR

factory.setNamespaceAware(true);


To make the parser a validating parser by W3C Schema, add the following code,

//set the validation to be against W3C XML Schema
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");

OR

//set the schema against which the validation is to be done
saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaSource", new File("myschema.xsd"));

SAX Parser code:

package info.icontraining.parsers;

import java.io.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.ext.LexicalHandler;
import org.xml.sax.helpers.DefaultHandler;

public class MySAXParser extends DefaultHandler implements LexicalHandler {

   private StringBuffer textBuffer;
   private Locator locator;

   public static void main(String[] args) {

      MySAXParser handler = new MySAXParser();
      SAXParserFactory factory = SAXParserFactory.newInstance();

      try {
         factory.setFeature("http://xml.org/sax/features/validation", true);
   
         SAXParser saxParser = factory.newSAXParser();
         saxParser.getXMLReader().setErrorHandler(handler);
         saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
         saxParser.parse( new File("FileName.xml"), handler );
      }
      catch (Throwable t) {
         t.printStackTrace();
      }
   }

   public void startDocument() throws SAXException {
      System.out.println("START DOCUMENT");
   }

   public void endDocument() throws SAXException { 
      System.out.println("END DOCUMENT");
   }

   public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) throws SAXException {

      System.out.print("ELEMENT: ");
      String eName = sName;

      if ("".equals(eName)) 
         eName = qName;
      System.out.print("<"+eName);
  
      if (attrs != null) {
         for (int i = 0; i < attrs.getLength(); i++) {
            System.out.println();
            String aName = attrs.getLocalName(i);
            if ("".equals(aName)) 
               aName = attrs.getQName(i);
            System.out.println(" ATTR: " + aName + "\t\"" 
                 + attrs.getValue(i) + "\"");
         }
      }
  
      if (attrs.getLength() > 0) 
         System.out.println();
      System.out.print(">");
      System.out.println();
   }

   public void endElement(String namespaceURI, String sName, String qName) {

      System.out.print("END_ELEMENT: ");
      String eName = sName; // element name

      if ("".equals(eName)) 
         eName = qName;

      System.out.println("</"+eName+">");
   }

   public void characters(char buf[], int offset, int len) throws SAXException {
  
      String s = new String(buf, offset, len);

      if (textBuffer == null) {
         textBuffer = new StringBuffer(s);
      } else {
         textBuffer.append(s);
      }
  
      if (textBuffer == null) 
         return;  

      System.out.print("CHARACTERS: ");
      String str = ""+textBuffer;
      System.out.println(str);
      textBuffer = null;
   }

   public void setDocumentLocator(Locator l) {
      locator = l;
   }

   public void error(SAXParseException exception)  {
      System.out.println("My Error: " + exception.getLineNumber() +  
          ":" + exception.getMessage());
   }
 
   public void fatalError(SAXParseException exception) {
      System.out.println("My Fatal Error: " + exception.getLineNumber() +  
          ": " + exception.getMessage());
   }

   public void warning(SAXParseException exception) {
      System.out.println("My Warning: " + exception.getLineNumber() +  
          ": " + exception.getMessage());
   }

   public void comment(char[] ch, int start, int length) throws SAXException {
      String text = new String(ch, start, length);
      System.out.println("COMMENT: "+text);
   }

   public void endCDATA() throws SAXException {
      System.out.println("CDATA Section ended");
   }

   public void endDTD() throws SAXException {
      System.out.println("DTD Ended");
   }

   public void endEntity(String name) throws SAXException {
      System.out.println("End Entity: " + name);
   }

   public void startCDATA() throws SAXException {
      System.out.println("CDATA section started");
   }

   public void startDTD(String name, String publicId, String systemId) throws SAXException {
      System.out.println("DTD Started");
   }

   public void startEntity(String name) throws SAXException {
      System.out.println("Start Entity: " + name);
   }
}