14. SAXExample : Input XML <?xml version="1.0" encoding="UTF-8"?> <dots> this is before the first dot and it continues on multiple lines <dot x="9" y="81" /> <dot x="11" y="121" /> <flip> flip is on <dot x="196" y="14" /> <dot x="169" y="13" /> </flip> flip is off <dot x="12" y="144" /> <extra> stuff </extra> <!-- a final comment --> </dots>
16. SAXExample : Input Output startDocument startElement: dots (0 attributes) characters: this is before the first dot and it continues on multiple lines startElement: dot (2 attributes) endElement: dot startElement: dot (2 attributes) endElement: dot startElement: flip (0 attributes) characters: flip is on startElement: dot (2 attributes) endElement: dot startElement: dot (2 attributes) endElement: dot endElement: flip characters: flip is off startElement: dot (2 attributes) endElement: dot startElement: extra (0 attributes) characters: stuff endElement: extra endElement: dots endDocument Finished parsing input. Got the following dots: [(9, 81), (11, 121), (14, 196), (13, 169), (12, 144)] <?xml version="1.0" encoding="UTF-8"?> <dots> this is before the first dot and it continues on multiple lines <dot x="9" y="81" /> <dot x="11" y="121" /> <flip> flip is on <dot x="196" y="14" /> <dot x="169" y="13" /> </flip> flip is off <dot x="12" y="144" /> <extra> stuff </extra> <!-- a final comment --> </dots>
19. Using DOM import javax.xml.parsers.* ; import org.w3c.dom.* ; // get a DocumentBuilder object DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch ( ParserConfigurationException e ) { e.printStackTrace(); } // invoke parser to get a Document Document doc = db.parse(inputStream); Document doc = db.parse(file); Document doc = db.parse(url); Here’s the basic recipe for getting started with DOM:
20. DOM Document access idioms // get the root of the Document tree Element root = doc.getDocumentElement(); // get nodes in subtree by tag name NodeList dots = root.getElementsByTagName( "dot" ); // get first dot element Element firstDot = ( Element ) dots.item(0); // get x attribute of first dot String x = firstDot.getAttribute( "x" ); OK, say we have a Document . How do we get at the pieces of it? Here are some common idioms:
21. More Document accessors Node access methods: String getNodeName () short getNodeType () Document getOwnerDocument () boolean hasChildNodes () NodeList getChildNodes () Node getFirstChild () Node getLastChild () Node getParentNode () Node getNextSibling () Node getPreviousSibling () boolean hasAttributes () ... and more ... Element extends Node and adds these access methods: String getTagName () boolean hasAttribute ( String name ) String getAttribute ( String name ) NodeList getElementsByTagName ( String name ) … and more … Document extends Node and adds these access methods: Element getDocumentElement () DocumentType getDoctype () ... plus the Element methods just mentioned ... ... and more ... e.g. DOCUMENT_NODE , ELEMENT_NODE , TEXT_NODE , COMMENT_NODE , etc.
22. Creating & manipulating Document s // get new empty Document from DocumentBuilder Document doc = db.newDocument(); // create a new <dots> Element and add to Document as root Element root = doc.createElement( "dots" ); doc.appendChild(root); // create a new <dot> Element and add as child of root Element dot = doc.createElement( "dot" ); dot.setAttribute( "x" , "9" ); dot.setAttribute( "y" , "81" ); root.appendChild(dot); The DOM API also includes lots of methods for creating and manipulating Document objects:
23. More Document manipulators Node manipulation methods: void setNodeValue ( String nodeValue ) Node appendChild ( Node newChild ) Node insertBefore ( Node newChild , Node refChild ) Node removeChild ( Node oldChild ) ... and more ... Element manipulation methods: void setAttribute ( String name , String value ) void removeAttribute ( String name ) … and more … Document manipulation methods: Text createTextNode ( String data ) Comment createCommentNode ( String data ) ... and more ...