Download presentation
Presentation is loading. Please wait.
Published byJosephine Simmons Modified over 8 years ago
1
USING ANDROID WITH THE DOM
2
Slide 2 Lecture Summary DOM concepts SAX vs DOM parsers Parsing HTTP results The Android DOM implementation
3
Slide 3 SAX vs DOM Parsers SAX parsers read an XML document sequentially They are fast DOM parsers read an entire document into a tree-based structure They are slower because of this We will use a DOM parser here
4
Slide 4 Origins of the DOM classes The XML related classes we use are derived from different sources Java Android W3c
5
Slide 5 Classes We Need DocumentBuilderFactory connects applications to parsers DocumentBuilder is the parser that builds the DOM tree Document contains a reference to the in- memory document
6
Slide 6 Classes We Need InputSource is the SAX parser we will use to read the XML document This SAX parser is needed by the DOM parser to read the document StringReader reads the string as a stream NodeList contains a list of DOM nodes Node is a DOM node
7
Slide 7 Creating an XML Parser In summary, we have a string that needs to be converted to an in-memory XML DOM object tree XML and Java take a “layered” approach to do this Create a parser Connect the parser to an input stream Parse the document
8
Slide 8 Creating the XML Parser Create a DocumentBuilder via the DocumentBuilderFactory Call the parse method on an input stream to read and parse the document Using the Document object, navigate and work with the in-memory XML document Its really the same object as the JavaScript document object
9
Slide 9 The DocumentBuilderFactory Class It lets applications get a parser that produces XML object trees from XML documents This is pretty much the same DOM with which you are familiar (JavaScript) As the name implies, we use a factory class and newInstance method to create the DocumenntBuilder
10
Slide 10 The DocumentBuilderFactory Class Example to create the factory DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
11
Slide 11 The DocumentBuilder Class It’s the DocumentBuilder that has the APIs used to parse the XML into a Document
12
Slide 12 The Document Class It’s very similar to the JavaScript document class. After all, the DOM is the DOM It contains the parsed document Call the parse method to parse the document
13
Slide 13 The Document Class (Methods) getDocumentElement() returns the root node (Node type) getElementsByTagName() gets a NodeList of elements having the same element name getElementByID() get a Node having the corresponding ID
14
Slide 14 The NodeList Class It’s just a simple list getLength() gets the number of nodes in the list Item(int index) returns a specific node We just enumerate with a loop
15
Slide 15 Node Types Every node has a type and node of different types behave differently Element Attribute Text
16
Slide 16 The Node Class getNodeName() gets the name of the element if this is an element node and the name of the attribute if this is an attribute node getNodeType() gets the type of node (element, text, attribute, etc…) getNodeValue() Returns null for elements Returns the text content for text nodes Returns the attribute value for attribute nodes
17
Slide 17 The Node Class hasAttributes() returns true for element nodes if the node has attributes hasChildren() returns true for element nodes if the node has child nodes getAttributes() returns a NamedNodeMap of attributes
18
Slide 18 The Node class Methods to navigate the tree getChildNodes() gets a NodeList of all children getFirstChild() get a Node of first child or null getLastChild() gets a Node of last child or null getNextSibling() gets the next sibling Node or null
19
Slide 19 Attributes Call getAttributes() on a node to return a NamedNodeMap Call getNamedItem() to get the value corresponding to the attribute name
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.