Download presentation
Presentation is loading. Please wait.
Published byΉφαιστος Σπανού Modified over 6 years ago
1
Jagdish Gangolly State University of New York at Albany
XML Validation & DOM Jagdish Gangolly State University of New York at Albany Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
2
XML Validation & DOM SAX validation SAX vs. DOM
Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
3
XML Validation Imports
Implement ContentHandler interface & Define the methods of the ContentHandler interface Implement DTDHandler interface (*) Instantiate parser & Register handlers Turn on/off validation, namespace awareness, & parse the document Parse & validate document (whole program) Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
4
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; Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
5
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 { Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
6
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. Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
7
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); Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
8
Turn on/off validation, namespace awareness, & Parse document
// Turn on validation parser.setFeature (" // Turn off namespace awareness not needed for Xerces 1.2+ (" false); // Parse the document parser.parse(uri); Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
9
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) Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
10
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(" true); parser.setFeature(" 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); Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
11
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 Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
12
DOM Imports DOMParserDemo class Command line input Parse document
PrintNode method Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
13
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; Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
14
DOM: DOMParserDemo class
public class DOMParserDemo { PerformDemo method to parse the document Method to print nodes Main method to obtain uri to be parsed } Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
15
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); Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
16
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: Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
17
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; Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
18
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() + "?>"); case Node.ENTITY_REFERENCE_NODE: System.out.println("&" + node.getNodeName() + ";"); Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
19
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; Acc 683, Spring, 2001 Jagdish S. Gangolly 11/19/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.