140 likes | 158 Views
WaysInJavaToParseXML. Prof: Dr. Shu-Ching Chen TA: Sheng Guan. Parse XML with Java DOM API. How to do this ? The Java DOM API for XML parsing is intended for working with XML as an object graph in memory - a "Document Object Model (DOM)".
E N D
WaysInJavaToParseXML Prof: Dr. Shu-Ching Chen TA: Sheng Guan
Parse XML with Java DOM API • How to do this ? • The Java DOM API for XML parsing is intended for working with XML as an object graph in memory - a "Document Object Model (DOM)". • The parser traverses the XML file and creates the corresponding DOM objects. • DOM objects are linked together in a tree structure.
XML file and corresponding DOM structure • <book> • <title>Fun Software</title> • <author>Jakob Jenkov</author> • <ISBN>0123456789</ISBN> • </book>
DOM - 3 pieces of XML • 1. Elements (sometimes called tags) • 2. Attributes • 3. The data (also called values) that the elements and attributes describe
Step 1:Creating a Java DOM XML parser • Creating a Java DOM XML parser is done using the javax.xml.parsers.DocumentBuilderFactory class. Here is an example: • DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); • DocumentBuilder builder = null; • try { • builder = builderFactory.newDocumentBuilder(); • } catch (ParserConfigurationException e) { • e.printStackTrace(); • }
Step 2:Parsing an XML file into a DOM tree • try { • Document document = builder.parse( new FileInputStream("/path/to/your/file.xml")); • } catch (SAXException e) { • e.printStackTrace(); • } catch (IOException e) { • e.printStackTrace(); • }
Step 3:Java DOM: Get the Document Object • You are now ready to traverse the Document instance you have received from the DocumentBuilder; • The DOM Document object represents an XML document. When you parse an XML file using a Java DOM parser, you get back a Document object.
The DOM Document Element • The two most commonly used features of DOM are: 1.Accessing Child Elements of an Element 2.Accessing Attributes of an Element • A DOM object contains a lot of different nodes connected in a tree-like structure. At the top is the Document object. • The Document object has a single root element, which is returned by calling getDocumentElement(): • Element rootElement = document.getDocumentElement();
DOM Elements, Child Elements, and the Node Interface • The root element has children. You get the children of an element like this: • NodeList nodes = element.getChildNodes(); • for(int i=0; i<nodes.getLength(); i++){ • Node node = nodes.item(i); • if(node instanceof Element){ • //a child element to process • Element child = (Element) node; • String attribute = child.getAttribute("width"); • } • }
DOM Element Attributes • The getChildNodes() method returns a NodeList object, which is a list of Node elements. • The Node interface is a superinterface for all of the different node types in DOM • The Element interface extends Node and you can access the attributes of an element via the Element interface: String attrValue = element.getAttribute("attrName");
Examine sub-elements //returns a list of subelements of specified name getElementsByTagName("subelementName"); //returns a list of all child nodes getChildNodes(); A whole complete example is in the below link: https://www.tutorialspoint.com/java_xml/java_dom_parse_document.htm
Reference • https://sanaulla.info/2013/05/23/parsing-xml-using-dom-sax-and-stax-parser-in-java/ • https://docs.oracle.com/cd/B28359_01/appdev.111/b28394/adx_j_parser.html • http://tutorials.jenkov.com/java-xml/dom.html • https://www.tutorialspoint.com/java_xml/java_dom_parse_document.htm