Java--xml file Read (DOM)

Source: Internet
Author: User

1, performance: a ". xml" file with an extension

2. Storage: Tree-shaped structure

3. XML parsing application:

Communication between different applications-booking software and payment software

Communication between different platforms--operating system

Sharing data between different platforms--websites and mobile phones

4.

The process of reading an XML file in a Java program is also known as parsing an XML file

Purpose of parsing: Get node name, node value, property name, property value

Parsing methods: DOM, SAX, dom4j, JDOM

5, pre-resolution preparation work:

 Packagecom.imooc_xml.dom.test;Importjava.io.IOException;ImportJavax.xml.parsers.DocumentBuilder;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.parsers.ParserConfigurationException;Importorg.w3c.dom.Document;ImportOrg.w3c.dom.NamedNodeMap;ImportOrg.w3c.dom.Node;Importorg.w3c.dom.NodeList;Importorg.xml.sax.SAXException; Public classDomtest { Public Static voidMain (string[] args) {//1. Create a Documentbuilderfactory objectDocumentbuilderfactory DBF =documentbuilderfactory.newinstance (); //2. Create a Documentbuilder object        Try{Documentbuilder db=Dbf.newdocumentbuilder (); //loading an XML file into the current project through the parser method of the Documentbuilder objectDocument document = Db.parse ("Xml/books.xml");             }                    } Catch(parserconfigurationexception e) {e.printstacktrace (); } Catch(saxexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }}

6. Parse the attribute name and attribute value of the XML file:

NodeList Booklist = document.getElementsByTagName ("book"); //traverse each book nodeSystem.out.println ("A total of" +booklist.getlength () + "book");  for(intI=0;i<booklist.getlength (); i++) {Node Book=Booklist.item (i); //traversing the properties of a bookNamedNodeMap attrs =book.getattributes (); System.out.println ("I+1" + "book in total" +attrs.getlength () + "Properties");  for(intJ=0;j<attrs.getlength (); j + +) {Node attr=Attrs.item (j); System.out.println ("Property Name:" +attr.getnodename ()); System.out.println ("Attribute value:" +Attr.getnodevalue ()); }                /*** If you already know that the book node has and can only have one ID attribute * Element book = Booklist.item (i);                 * String AttrValue = book.getattribute ("id"); */

7. Parse the node name and node value of the XML file:

NodeList childNodes =book.getchildnodes (); System.out.println ("First" + (i+1) + "book Total" +childnodes.getlength ()+ "Child Nodes");  for(intK = 0;k<childnodes.getlength (); k++){                    //The text type of node (with line breaks and spaces) and the element type of node                    if(Childnodes.item (k). Getnodetype () = =Node.element_node)                        {System.out.println (Childnodes.item (k). Getnodename ()); //Gets the node value of element type element type node//getfirstchild (). Getnodevalue (): considers the value to be a child of a child node//System.out.println (Childnodes.item (k). Getfirstchild (). Getnodevalue ()); //difference: <name><a>aa</a>java programming idea </name> one is null, one is Aajava programming thoughtSystem.out.println (Childnodes.item (k). Gettextcontent ()); }                }

8. Common node types:

Java--xml file Read (DOM)

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.