Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. SC Cheung (HKUST), Prof. Francis Lau (HKU) Reference: XML How To Program, Deitel, Prentice Hall 2001 CSIT600b:

Similar presentations


Presentation on theme: "1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. SC Cheung (HKUST), Prof. Francis Lau (HKU) Reference: XML How To Program, Deitel, Prentice Hall 2001 CSIT600b:"— Presentation transcript:

1 1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. SC Cheung (HKUST), Prof. Francis Lau (HKU) Reference: XML How To Program, Deitel, Prentice Hall 2001 CSIT600b: XML Programming DOM Programming

2 Dickson Chiu 2004CSIT600b 01-2 Overview of Java API for XML Java Web Services Developer Pack (Java WSDP) http://java.sun.com/webservices/tutorial.html http://java.sun.com/webservices/tutorial.html Now in J2EE 1.4 core Document-oriented Java API for XML Processing (JAXP) -- processes XML documents using various parsers Java Architecture for XML Binding (JAXB) -- processes XML documents using schema-derived JavaBeans component classes Procedure-oriented Java API for XML-based RPC (JAX-RPC) -- sends SOAP method calls to remote parties over the Internet and receives the results Java API for XML Messaging (JAXM) -- sends SOAP messages over the Internet in a standard way Java API for XML Registries (JAXR) -- provides a standard way to access business registries and share information

3 Dickson Chiu 2004CSIT600b 01-3 The DOM Core (JAXP) Fundamental Interfaces Required in all implementations DOMException, ExceptionCode, DOMImplementation, DocumentFragment, Document, Node, NodeList, NamedNodeMap, CharacterData, Attr, Element, Text, Comment Extended Interfaces For DOM implementations working with XML CDATASection, DocumentType, Notation, Entity, EntityReference, ProcessingInstruction http://java.sun.com/xml/jaxp/ Supports Schema and DTD validation + XSLT More powerful than JDOM and dom4jJDOMdom4j

4 Dickson Chiu 2004CSIT600b 01-4 DOM classes and interfaces (Ref) Deitel, XML How to Program, Fig 8.4

5 Dickson Chiu 2004CSIT600b 01-5 Some Document methods. Deitel, XML How to Program, Fig 8.5

6 Dickson Chiu 2004CSIT600b 01-6 XmlDocument methods Deitel, XML How to Program, Fig 8.6

7 Dickson Chiu 2004CSIT600b 01-7 Node methods Deitel, XML How to Program, Fig 8.7

8 Dickson Chiu 2004CSIT600b 01-8 Some node types Deitel, XML How to Program, Fig 8.8

9 Dickson Chiu 2004CSIT600b 01-9 Element methods Deitel, XML How to Program, Fig 8.9

10 Dickson Chiu 2004CSIT600b 01-10 DOM API of JAXP PackageDescription org.w3c.dom Defines the DOM programming interfaces for XML (and, optionally, HTML) documents, as specified by the W3C. javax.xml.parsers Defines the DocumentBuilderFactory class and the DocumentBuilder class, which returns an object that implements the W3C Document interface. The factory that is used to create the builder is determined by the javax.xml.parsers system property, which can be set from the command line or overridden when invoking the new Instance method. This package also defines the ParserConfigurationException class for reporting errors.

11 Dickson Chiu 2004CSIT600b 01-11 Merging 2 DOM trees (for assignment) import java.net.*;import java.io.*; import org.w3c.tidy.*;import org.w3c.dom.*; import javax.xml.transform.*;import javax.xml.transform.stream.*; import javax.xml.transform.dom.*;import org.w3c.dom.Node; import javax.xml.parsers.*;import javax.imageio.metadata.*; public class tester { public tester() { } static public void main(String[] arg){ try { String urlStr1 = "http://finance.yahoo.com/q/cp?s=^HSI"; String urlStr2 = "http://finance.yahoo.com/q/cp?s=^DJI"; // open the connection with that url URL url1 = null; URL url2 = null; url1 = new URL(urlStr1);url2 = new URL(urlStr2); URLConnection cn1 = null; URLConnection cn2 = null; cn1 = url1.openConnection();cn2 = url2.openConnection(); // parse the html file into dom Tidy tidy = new Tidy(); tidy.setCharEncoding(Configuration.UTF8); tidy.setIndentContent(true); tidy.setXHTML(true); tidy.setWraplen(Integer.MAX_VALUE); Document doc1 = tidy.parseDOM(cn1.getInputStream(), null); Document doc2 = tidy.parseDOM(cn2.getInputStream(), null);

12 Dickson Chiu 2004CSIT600b 01-12 Merging 2 DOM trees - cont // xsl File xslFile1 = new File("xslt1.xsl"); File xslFile2 = new File("xslt1.xsl"); // transform obj TransformerFactory t = TransformerFactory.newInstance(); Transformer transformer1 = t.newTransformer(new StreamSource(xslFile1)); Transformer transformer2 = t.newTransformer(new StreamSource(xslFile2)); //transform DOMSource source1 = new DOMSource(doc1); DOMResult result1 = new DOMResult(); DOMSource source2 = new DOMSource(doc2); DOMResult result2 = new DOMResult(); transformer1.transform(source1, result1); transformer2.transform(source2, result2); Document resultDoc1 = (Document)result1.getNode(); Document resultDoc2 = (Document)result2.getNode();

13 Dickson Chiu 2004CSIT600b 01-13 Merging 2 DOM trees - cont // merge two dom // 1. create new root Node newRoot = resultDoc1.createElement("indexes"); // 2. get the root element from both dom trees Node hsiIndex = resultDoc1.getFirstChild(); Node djiIndex = resultDoc2.getFirstChild(); // 3. *must* make all the nodes belongs to the same document djiIndex = resultDoc1.importNode(djiIndex, true); // 4. change the pointers newRoot.appendChild(hsiIndex); newRoot.appendChild(djiIndex); resultDoc1.appendChild(newRoot); t.newTransformer().transform(new DOMSource(resultDoc1), new StreamResult(System.out)); } catch (MalformedURLException ex) { } catch (IOException ex1) { } catch (TransformerConfigurationException ex2) { } catch (TransformerException ex3) { } } True => deep copy

14 Dickson Chiu 2004CSIT600b 01-14 Operation on the Command Line Compiling H:\Sun\AppServer\jdk\bin\javac -classpath H:\Sun\Appserver\lib\tidy.jar tester.java Running H:\Sun\AppServer\jdk\bin\java -classpath H:\Sun\Appserver\lib\tidy.jar;. tester > out.wml Library

15 Dickson Chiu 2004CSIT600b 01-15 Building an XML Document with DOM Desired Output Sue Green, and ?]]>

16 Dickson Chiu 2004CSIT600b 01-16 Building an XML Document with DOM // Dietel, XML How to Program, Fig. 8.14 : BuildXml.java. import java.io.*; import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import com.sun.xml.tree.XmlDocument; public class BuildXml { private Document document; public BuildXml() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { // get DocumentBuilder DocumentBuilder builder = factory.newDocumentBuilder(); // create root node document = builder.newDocument(); } catch ( ParserConfigurationException pce ) { pce.printStackTrace(); } Use JAXP default parser DocumentBuilder loads and parses XML documents Obtain XML Document reference import specifies location of classes needed by application

17 Dickson Chiu 2004CSIT600b 01-17 Building an XML Document with DOM Element root = document.createElement( "root" ); document.appendChild( root ); Comment simpleComment = document.createComment( "This is a simple contact list" ); // add a comment root.appendChild( simpleComment ); Node contactNode = createContactNode( document ); root.appendChild( contactNode ); // add a child element ProcessingInstruction pi = document.createProcessingInstruction( "myInstruction", "action silent" ); root.appendChild( pi ); // add processing instruction CDATASection cdata = document.createCDATASection( "I can add, and ?" ); root.appendChild( cdata ); // add a CDATA section try { // write the XML document to a file ( (XmlDocument) document).write( new FileOutputStream( "myDocument.xml" ) ); } catch ( IOException ioe ) { ioe.printStackTrace(); } Create root Element and append to Document Call method createContactNode (next slide) to create child node Create ProcessingInstruction node with target myInstruction and value action silent Create CDATA node and append to root node Write XML Document to myDocument.xml OUT!!! Use transformer

18 Dickson Chiu 2004CSIT600b 01-18 Building an XML Document with DOM public Node createContactNode( Document document ) { // create FirstName and LastName elements Element firstName = document.createElement( "FirstName" ); firstName.appendChild( document.createTextNode( "Sue" ) ); Element lastName = document.createElement( "LastName" ); lastName.appendChild( document.createTextNode( "Green" ) ); // create contact element Element contact = document.createElement( "contact" ); // create an attribute Attr genderAttribute = document.createAttribute( "gender" ); genderAttribute.setValue( "F" ); // append attribute to contact element contact.setAttributeNode( genderAttribute ); contact.appendChild( firstName ); contact.appendChild( lastName ); return contact; } public static void main( String args[] ) { BuildXml buildXml = new BuildXml(); } Creates and returns Element node Create Element FirstName with text Sue Create Element LastName with text Green Create Element contact with attribute gender Append Element s FirstName and LastName to Element contact

19 Dickson Chiu 2004CSIT600b 01-19 Write an XML file with Transformer // write XML document to disk import javax.xml.transform.*;import javax.xml.transform.stream.*; import javax.xml.transform.dom.*; try { // create DOMSource for source XML document Source xmlSource = new DOMSource( document ); // create StreamResult for transformation result // Write to console: Result result = new StreamResult( System.out ); Result result = new StreamResult( new FileOutputStream( new File( “test.xml") ) ); // create TransformerFactory TransformerFactory transformerFactory = TransformerFactory.newInstance(); // create Transformer for transformation Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty( "indent", "yes" ); // transform and deliver content to client transformer.transform( xmlSource, result ); }

20 Dickson Chiu 2004CSIT600b 01-20 Modifying XML Document with DOM // Fig 8.10 : ReplaceText.java import java.io.*; import org.w3c.dom.*; import javax.xml.parsers.*; import com.sun.xml.tree.XmlDocument; import org.xml.sax.*; public class ReplaceText { private Document document; public ReplaceText() { try { // obtain the default parser DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // set the parser to validating factory.setValidating( true ); DocumentBuilder builder = factory.newDocumentBuilder(); // set error handler for validation errors builder.setErrorHandler( new MyErrorHandler() ); // obtain document object from XML document document = builder.parse( new File( "intro.xml" ) );

21 Dickson Chiu 2004CSIT600b 01-21 Modifying XML Document with DOM // fetch the root node Node root = document.getDocumentElement(); if ( root.getNodeType() == Node.ELEMENT_NODE ) { Element myMessageNode = ( Element ) root; NodeList messageNodes = myMessageNode.getElementsByTagName( "message" ); if ( messageNodes.getLength() != 0 ) { Node message = messageNodes.item( 0 ); // create a text node Text newText = document.createTextNode("New Message!!" ); // get the old text node Text oldText = ( Text ) message.getChildNodes().item( 0 ); // replace the text message.replaceChild( newText, oldText ); } ( (XmlDocument) document).write( new FileOutputStream( "intro1.xml" ) ); } Cast root node as element (subclass), then get list of all message elements If message element exists, replace old text node with new one Write new XML document to intro1.xml OUT!!! Use Transformer Item() returns type Object and need casting Welcome to XML! New Message!!

22 Dickson Chiu 2004CSIT600b 01-22 Modifying XML Document with DOM catch ( SAXParseException spe ) { System.err.println( "Parse error: " + spe.getMessage() ); System.exit( 1 ); } catch ( SAXException se ) { se.printStackTrace(); } catch ( FileNotFoundException fne ) { System.err.println( "File \'intro.xml\' not found. " ); System.exit( 1 ); } catch ( Exception e ) { e.printStackTrace(); } public static void main( String args[] ) { ReplaceText d = new ReplaceText(); }

23 Dickson Chiu 2004CSIT600b 01-23 Handling Complexities To be more robust, a DOM application must handle more cases. When data comes from outside world When searching for an element: Ignore comments, attributes, and processing instructions. Allow for the possibility that subelements do not occur in the expected order. Skip over TEXT nodes that contain ignorable whitespace, if not validating. When extracting text for a node: Extract text from CDATA nodes as well as text nodes. Ignore comments, attributes, and processing instructions when gathering the text. If an entity reference node or another element node is encountered, recurse (that is, apply the text-extraction procedure to all subnodes). + ELEMENT: sentence + TEXT: The + ENTITY REF: projectName + COMMENT: The latest name we're using + TEXT: Eagle + CDATA: project + TEXT: is + PI: editor: red + ELEMENT: bold + TEXT: important + PI: editor: normal

24 Dickson Chiu 2004CSIT600b 01-24 Error Handler for Validation Errors // Fig 8.11 : MyErrorHandler.java // Error Handler for validation errors. import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class MyErrorHandler implements ErrorHandler { // throw SAXException for fatal errors public void fatalError( SAXParseException exception ) throws SAXException { throw exception; } public void error( SAXParseException e ) throws SAXParseException { throw e; } // print any warnings public void warning( SAXParseException err ) throws SAXParseException { System.err.println( "Warning: " + err.getMessage() ); }

25 Dickson Chiu 2004CSIT600b 01-25 Traversing the DOM // Dietel, XML How to Program, Fig. 8.15 : TraverseDOM.java import java.io.*; import org.w3c.dom.*; import org.xml.sax.*; import javax.xml.parsers.*; import com.sun.xml.tree.XmlDocument; public class TraverseDOM { private Document document; public TraverseDOM( String file ) { try { // obtain the default parser DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating( true ); DocumentBuilder builder = factory.newDocumentBuilder(); // set error handler for validation errors builder.setErrorHandler( new MyErrorHandler() ); // obtain document object from XML document document = builder.parse( new File( file ) ); processNode( document ); } Obtain JAXP default parser and DocumentBuilder to load and parse XML documents Load and parse XML document Pass Document to method processNode Require parser to validate documents

26 Dickson Chiu 2004CSIT600b 01-26 Traversing the DOM catch ( SAXParseException spe ) { System.err.println("Parse error: " + spe.getMessage() ); System.exit( 1 ); } catch ( SAXException se ) { se.printStackTrace(); } catch ( FileNotFoundException fne ) { System.err.println( "File \'" + file + "\' not found. " ); System.exit( 1 ); } catch ( Exception e ) { e.printStackTrace(); }

27 Dickson Chiu 2004CSIT600b 01-27 Traversing the DOM public void processNode( Node currentNode ){ switch ( currentNode.getNodeType() ) { case Node.DOCUMENT_NODE: // process a Document node Document doc = ( Document ) currentNode; System.out.println( "Document node: " + doc.getNodeName() + "\nRoot element: " + doc.getDocumentElement().getNodeName() ); processChildNodes( doc.getChildNodes() ); break; case Node.ELEMENT_NODE: // process an Element node System.out.println( "\nElement node: " + currentNode.getNodeName() ); NamedNodeMap attributeNodes = currentNode.getAttributes(); for ( int i = 0; i < attributeNodes.getLength(); i++){ Attr attribute = ( Attr ) attributeNodes.item( i ); System.out.println( "\tAttribute: " + attribute.getNodeName() + " ; Value = " + attribute.getNodeValue() ); } processChildNodes( currentNode.getChildNodes() ); break; switch statement determines Node type If document node, output document node and process child nodes If element node, output element’s attributes and process child nodes

28 Dickson Chiu 2004CSIT600b 01-28 Traversing the DOM case Node.CDATA_SECTION_NODE: // process text node / CDATA section case Node.TEXT_NODE: Text text = ( Text ) currentNode; if ( !text.getNodeValue().trim().equals( "" ) ) System.out.println( "\tText: " + text.getNodeValue() ); break; } public void processChildNodes( NodeList children ) { if ( children.getLength() != 0 ) for ( int i = 0; i < children.getLength(); i++) processNode( children.item( i ) ); } public static void main( String args[] ) { if ( args.length < 1 ) { System.err.println("Usage: java TraverseDOM " ); System.exit( 1 ); } TraverseDOM traverseDOM = new TraverseDOM( args[ 0 ] ); } If CDATA or text node, output node’s text content Method processChildNodes calls method processNode for each Node in NodeList

29 Dickson Chiu 2004CSIT600b 01-29 DOM with JavaScript (Reference) (MS XML Parser) (Deitel Sec 8.3) <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> A DOM Example var xmlDocument = new ActiveXObject( "Microsoft.XMLDOM" ); xmlDocument.load( "article.xml" ); // get the root element var element = xmlDocument.documentElement; document.writeln(" Here is the root node of the document:" ); document.writeln(" " + element.nodeName+" " ); document.writeln(" The following are its child elements:" ); document.writeln( " " );

30 Dickson Chiu 2004CSIT600b 01-30 DOM with JavaScript (2) // traverse all child nodes of root element for ( i = 0; i < element.childNodes.length; i++ ) { var curNode = element.childNodes.item( i ); // print node name of each child element document.writeln( " " + curNode.nodeName + " " ); } document.writeln( " " ); // get the first child node of root element var currentNode = element.firstChild; // firstChild = childNodes.item(0) document.writeln( " The first child of root node is:" ); document.writeln( " " + currentNode.nodeName+ " "); document.writeln( " whose next sibling is:" ); // get the next sibling of first child var nextSib = currentNode.nextSibling; document.writeln( " " + nextSib.nodeName+ "." ); document.writeln( " Value of " + nextSib.nodeName + " element is:" );

31 Dickson Chiu 2004CSIT600b 01-31 DOM with JavaScript (3) var value = nextSib.firstChild; // print the text value of the sibling document.writeln( " " + value.nodeValue + " " ); document.writeln( " Parent node of " ); document.writeln( " " + nextSib.nodeName+ " is:" ); document.writeln( " " + nextSib.parentNode.nodeName + ". " ); Simple XML December 6, 2000 Tem Nieto XML is pretty easy. Once you have mastered HTML, XML is easily learned. You must remember that XML is not for displaying information but for managing information.


Download ppt "1 Dickson K.W. Chiu PhD, SMIEEE Thanks to Prof. SC Cheung (HKUST), Prof. Francis Lau (HKU) Reference: XML How To Program, Deitel, Prentice Hall 2001 CSIT600b:"

Similar presentations


Ads by Google