JAVA parses the DOM of XML, javaxmldom

Source: Internet
Author: User

JAVA parses the DOM of XML, javaxmldom
DOM method preparation for parsing XML in JAVA

Create a DocumentBuilderFactory object;

Create a DocumentBuilder object;

Load xml using the parse method of the DocumentBuilder object

Common Methods for parsing XML file attribute names and attribute values to parse XML file node names and node values are as follows:

GetElementsByTagName ();

GetLength ();

Item ();

GetNodeName ();

GetNodeValue ();

GetNodeType ();

* GetAttributes (); node

* GetAttribute ("name"); element

GetChildNodes ();

GetFirstChild ();

Common node types:

    

The following code is package pers. zww. xml; import java. io. IOException; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. parsers. parserConfigurationException; import org. w3c. dom. document; import org. w3c. dom. namedNodeMap; import org. w3c. dom. node; import org. w3c. dom. nodeList; import org. xml. sax. SAXException; public class Dom {public static void main (String [] args) {// create the DocumentBuilderFactory object DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance (); // create the DocumentBuilder object DocumentBuilder db; try {db = dbf. newDocumentBuilder (); // load xml Document document = db by using the parse method of the DocumentBuilder object. parse ("books. xml "); // get the book node set NodeList bookList = document. getElementsByTagName ("book"); // obtain the length of the System by using the getLength () method of noodlist. out. println (bookList. getLength (); // traverse the book node for (int I = 0; I <bookList. getLength (); I ++) {System. out. println ("=================="); // item (I) method To obtain Node book = (Node) bookList. item (I); // obtain all attributes of the book node set NamedNodeMap attrs = book. getAttributes (); // traverses the attributes of the book System. out. println ("Number" + (I + 1) + "total books" + attrs. getLength () + "attributes"); for (int j = 0; j <attrs. getLength (); j ++) {/** node TYPE * element 1 * attr 2 * text 3 * // ** if there is only one attribute, * forcibly convert the node TYPE of the book node * using the getAttribute ("id ") method * // item (I) method to obtain the attribute Node attr = attrs. item (j); // obtain the attribute name System. out. print ("attribute name:" + attr. getNodeName (); // obtain the attribute value System. out. println ("& attribute value:" + attr. getNodeValue (); // parse the subnode NodeList childNodes = book. getChildNodes (); // get the node name/value of each node System. out. println ("Total" + childNodes. getLength () + "subnode"); for (int k = 0; k <childNodes. getLength (); k ++) {// differentiate between the node of the text type and the node if (childNodes. item (k ). getNodeType () = Node. ELEMENT_NODE) {// obtain the node name System of the element node. out. print ("node name" + childNodes. item (k ). getNodeName (); // get the node value of an element node // Method 1: System. out. println ("& node value" + childNodes. item (k ). getFirstChild (). getNodeValue (); // Method 2: // all values of the subnode // System. out. println ("& node value" + childNodes. item (k ). getTextContent ();/** difference: * if you add another node under the name node, for example,  

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <bookstore> 3 <book id = "1"> 4 <name> When only one attribute is specified for a node, You can forcibly convert the node to an element and call its getAttribute ("name") method;

When the number of child nodes in the getLength () method is exceeded, the blank and line feed are also included;

References

MOOC JessicaJiang video tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.