XML Validation DOM by bns26590

VIEWS: 19 PAGES: 19

									XML Validation & DOM
        Jagdish Gangolly
        State University of New York at Albany




                                                   1
Acc 683, Spring, 2001 Jagdish S. Gangolly   2/1/2010
XML Validation & DOM
  SAX validation
  SAX vs. DOM




                                                   2
Acc 683, Spring, 2001 Jagdish S. Gangolly   2/1/2010
     XML Validation
1.     Imports
2.     Implement ContentHandler interface &
       Define the methods of the ContentHandler
       interface
3.     Implement DTDHandler interface (*)
4.     Instantiate parser & Register handlers
5.     Turn on/off validation, namespace
       awareness, & parse the document
6.     Parse & validate document (whole program)


                                                        3
     Acc 683, Spring, 2001 Jagdish S. Gangolly   2/1/2010
 Imports

import        org.xml.sax.Attributes;
import        org.xml.sax.ContentHandler;
import        org.xml.sax.DTDHandler;
import        org.xml.sax.ErrorHandler;
import        org.xml.sax.Locator;
import        org.xml.sax.SAXException;
import        org.xml.sax.SAXParseException;
import        org.xml.sax.XMLReader;
import        org.xml.sax.helpers.XMLReaderFactory;


                                                         4
 Acc 683, Spring, 2001 Jagdish S. Gangolly        2/1/2010
Implement ContentHandler interface &
Define the methods of the ContentHandler
interface
class MyContentHandler implements ContentHandler {
 private Locator locator;
 public void setDocumentLocator(Locator locator) {
 }
 public void startDocument() throws SAXException {
}
…
…
 public void skippedEntity(String name) throws
   SAXException {
}


                                                            5
  Acc 683, Spring, 2001 Jagdish S. Gangolly          2/1/2010
Implement DTDHandler interface (*)
 The DTDHandler provides callback methods
 for during the process of reading and parsing
 DTDs. Since it does not define events during
 validation, but only those occuring during
 reading of DTDs, they are not crucial.




                                                      6
   Acc 683, Spring, 2001 Jagdish S. Gangolly   2/1/2010
  Instantiate parser & Register Handlers
// Get instances of our handlers
 ContentHandler contentHandler = new MyContentHandler();
 ErrorHandler errorHandler = new MyErrorHandler();
 DTDHandler dtdHandler = new MyDTDHandler();

// Instantiate a parser
   XMLReader parser = XMLReaderFactory.createXMLReader(

  "org.apache.xerces.parsers.SAXParser");

// Register the content handler
   parser.setContentHandler(contentHandler);

// Register the error handler
   parser.setErrorHandler(errorHandler);

// Register the DTD handler
   parser.setDTDHandler(dtdHandler);
                                                           7
  Acc 683, Spring, 2001 Jagdish S. Gangolly         2/1/2010
Turn on/off validation, namespace
    awareness, & Parse document
// Turn on validation

parser.setFeature
     ("http://xml.org/sax/features/validation",true);

// Turn off namespace awareness not needed for Xerces 1.2+

parser.setFeature
 ("http://xml.org/sax/features/namespaces", false);

// Parse the document

parser.parse(uri);


                                                               8
   Acc 683, Spring, 2001 Jagdish S. Gangolly            2/1/2010
Parse & validate document (whole program)
 //Imports
 import java.io.IOException;
 import org.xml.sax.Attributes;
 import org.xml.sax.ContentHandler;
 import org.xml.sax.DTDHandler;
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 import org.xml.sax.SAXParseException;
 import org.xml.sax.XMLReader;
 import org.xml.sax.helpers.XMLReaderFactory;

 public class SAXParserDemo {
 public void performDemo(String uri) {
         System.out.println("Parsing XML File: " + uri + "\n\n");
  ContentHandler contentHandler = new MyContentHandler();
  ErrorHandler errorHandler = new MyErrorHandler();
  DTDHandler dtdHandler = new MyDTDHandler();

                                          (Continued next slide)


                                                                           9
     Acc 683, Spring, 2001 Jagdish S. Gangolly                      2/1/2010
Parse & validate document (whole program)
 try {
    XMLReader parser = XMLReaderFactory.createXMLReader(
                                     "org.apache.xerces.parsers.SAXParser");
 parser.setContentHandler(contentHandler);
 parser.setErrorHandler(errorHandler);
 parser.setDTDHandler(dtdHandler);
 parser.setFeature("http://xml.org/sax/features/validation", true);
 parser.setFeature("http://xml.org/sax/features/namespaces", false);
 parser.parse(uri);
 } catch (IOException e) {
             System.out.println("Error reading URI: " + e.getMessage());
         } catch (SAXException e) {
             System.out.println("Error in parsing: " + e.getMessage());
         }
     }

 public static void main(String[] args) {
         if (args.length != 1) {
             System.out.println("Usage: java SAXParserDemo [XML URI]");
             System.exit(0);
         }
         String uri = args[0];
         SAXParserDemo parserDemo = new SAXParserDemo();
         parserDemo.performDemo(uri);
     }
 }
                                                                                     10
     Acc 683, Spring, 2001 Jagdish S. Gangolly                                 2/1/2010
SAX vs. DOM
  SAX is sequential, so it does not allow
   for random access to a document
  No concept of sibling, next,… elements
   in SAX, so difficult to move laterally
  DOMtree representation consumes
   memory




                                                   11
Acc 683, Spring, 2001 Jagdish S. Gangolly    2/1/2010
                                            DOM
  Imports
  DOMParserDemo class
  Command line input
  Parse document
  PrintNode method




                                                        12
Acc 683, Spring, 2001 Jagdish S. Gangolly         2/1/2010
  DOM: Imports

import       java.io.IOException;
import       org.xml.sax.SAXException;
import       org.w3c.dom.Document;
import       org.w3c.dom.DocumentType;
import       org.w3c.dom.NamedNodeMap;
import       org.w3c.dom.Node;
import       org.w3c.dom.NodeList;
import       org.apache.xerces.parsers.DOMParser;




                                                          13
  Acc 683, Spring, 2001 Jagdish S. Gangolly         2/1/2010
DOM: DOMParserDemo class

 public class DOMParserDemo {

       PerformDemo method to parse the document

       Method to print nodes

       Main method to obtain uri to be parsed
 }




                                                        14
Acc 683, Spring, 2001 Jagdish S. Gangolly         2/1/2010
DOM: Main method to obtain uri to be
parsed
 public static void main(String[] args) {
   if (args.length != 1) {
    System.out.println("Usage: java DOMParserDemo [XML
                                                  URI]");
    System.exit(0);
    }

   String uri = args[0];
   DOMParserDemo parserDemo = new DOMParserDemo();
   parserDemo.performDemo(uri);
    }




                                                           15
   Acc 683, Spring, 2001 Jagdish S. Gangolly         2/1/2010
DOM: Method to print nodes I
 public void printNode(Node node, String indent)      {

            switch (node.getNodeType()) {
                case Node.DOCUMENT_NODE:
                    System.out.println("<xml version=\"1.0\">\n");
                    NodeList nodes = node.getChildNodes();
                    if (nodes != null) {
                        for (int i=0; i<nodes.getLength(); i++) {
                            printNode(nodes.item(i), "");
                        }
                    }
                    break;

                  case Node.ELEMENT_NODE:
                  case Node.TEXT_NODE:
                  case Node.CDATA_SECTION_NODE:
                  case Node.PROCESSING_INSTRUCTION_NODE:
                  case Node.ENTITY_REFERENCE_NODE:
                  case Node.DOCUMENT_TYPE_NODE:
            }
      }


                                                                           16
Acc 683, Spring, 2001 Jagdish S. Gangolly                            2/1/2010
  DOM: Method to print nodes II
case Node.ELEMENT_NODE:
      String name = node.getNodeName();
      System.out.print(indent + "<" + name);
      NamedNodeMap attributes = node.getAttributes();

        for (int i=0; i<attributes.getLength(); i++) {
            Node current = attributes.item(i);
            System.out.print(" " + current.getNodeName()
              + "=\"" + current.getNodeValue() + "\"");
        }

        System.out.println(">");
        NodeList children = node.getChildNodes();
        if (children != null) {
           for (int i=0; i<children.getLength(); i++) {
              printNode(children.item(i), indent + " ");
           }
        }
                 System.out.println(indent + "</" + name + ">");
                 break;
                                                                  17
  Acc 683, Spring, 2001 Jagdish S. Gangolly                 2/1/2010
DOM: Method to print nodes III
   case Node.TEXT_NODE:

   case Node.CDATA_SECTION_NODE:
      System.out.print(node.getNodeValue());
      break;

   case Node.PROCESSING_INSTRUCTION_NODE:
      System.out.println("<?" + node.getNodeName() +
                          " " + node.getNodeValue() +
     "?>");
      break;

   case Node.ENTITY_REFERENCE_NODE:
      System.out.println("&" + node.getNodeName() +
     ";");
      break;


                                                              18
Acc 683, Spring, 2001 Jagdish S. Gangolly               2/1/2010
DOM: Method to print nodes III
 case Node.DOCUMENT_TYPE_NODE:
     DocumentType docType = (DocumentType)node;
     System.out.print("<!DOCTYPE " + docType.getName());
     if (docType.getPublicId() != null) {
          System.out.print(" PUBLIC \"" +
                            docType.getPublicId() + "\" ");
          } else {
                     System.out.print(" SYSTEM ");
                 }
     System.out.println("\"" + docType.getSystemId() +
                          "\">");
     break;




                                                              19
Acc 683, Spring, 2001 Jagdish S. Gangolly               2/1/2010

								
To top