Java read parse XML file instance _java

Source: Internet
Author: User
Tags xquery

Read the local XML file, parsing through the DOM, Dom parsing is characterized by loading the entire XML file into memory, forming a DOM tree structure, the tree structure is convenient traversal and manipulation.

The characteristic of Dom parsing is to read the XML file into the DOM tree structure and traverse through the nodes.

This is the concept of a node in the consortium.

If the XML contains a large amount of data, the DOM is not suitable for XML parsing that contains large amounts of data because the DOM loads the XML in memory at once. When a large amount of XML is included, parsing with sax saves memory.

Here is an example of parsing an XML file using the DOM:

The XML file structure is as follows:

 <?xml version= "1.0" encoding= "iso-8859-1"?> <bookstore> <book category=
 "Cooking" > <title lang= "en" >everyday italian</title> <author>giada De laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category= "Children" > < Title lang= "en" >harry potter</title> <author>j K rowling</author> <year>2005</year > <price>29.99</price> </book> <book category= "Web" > <title lang= "en" >xquery Sta rt</title> <author>james mcgovern</author> <year>2003</year> <price>49.99</ price> </book> <book category= "Web" cover= "Paperback" > <title lang= "en" >learning xml</title&
 Gt <author>erik T. ray</author> <year>2003</year> <price>39.95</price> </book > </bookstore> 

The

Create the parsed XML class as follows:

Package xml.dom;
Import Java.io.File;
Import Javax.xml.parsers.DocumentBuilder;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import org.w3c.dom.Document;
Import org.w3c.dom.Element;
Import Org.w3c.dom.Node;

Import org.w3c.dom.NodeList; public class Readxmlfile {public static void main (string[] args) {try{File xmlFile = new File ("src/resource/
  
  Book.xml ");
  
  Documentbuilderfactory builderfactory = Documentbuilderfactory.newinstance ();
  
  Documentbuilder builder = Builderfactory.newdocumentbuilder ();
  
  Document doc = Builder.parse (xmlFile);
  
  Doc.getdocumentelement (). normalize ();
  
  System.out.println ("Root element:" +doc.getdocumentelement (). Getnodename ());
  
  NodeList nlist = Doc.getelementsbytagname ("book");
  
  for (int i = 0; I<nlist.getlength (); i++) {Node node = Nlist.item (i);
  System.out.println ("Node name:" + node.getnodename ());
  
  Element ele = (element) node;
  System.out.println ("----------------------------"); if (Node.getnodeType () = = Element.element_node) {System.out.println ("book Category:" + ele.getattribute ("category"));
  
  System.out.println ("title name:" + ele.getelementsbytagname ("title"). Item (0). Gettextcontent ());
  
  System.out.println ("Author Name:" +ele.getelementsbytagname ("Author"). Item (0). Gettextcontent ());
  
  System.out.println ("Year:" +ele.getelementsbytagname ("Year"). Item (0). Gettextcontent ());
  
  System.out.println ("Price:" +ele.getelementsbytagname ("Price"). Item (0). Gettextcontent ());
  
  
  System.out.println ("-------------------------");
  
 }
  
  
  }

Parse Result:

Root element:bookstore
Node name:book
----------------------------book
category:cooking
title Name:everyday Italian
author Name:giada De Laurentiis year:2005 price:30.00
---------------------- ---
Node name:book
----------------------------book
category:children
title Name:harry Potter
author Name:j K. Rowling
year:2005
price:29.99
-------------------------
Node name:book< c19/>----------------------------book
category:web
title name:xquery Kick Start
author Name:james McGovern
year:2003
price:49.99
-------------------------
Node name:book
-------------- --------------book
category:web
title name:learning XML
author Name:erik T. Ray
year:2003
price:39.95
-------------------------


The above is the corresponding value obtained by name.
The following is output using the Loop node:
The code for the Loop node output is as follows:

Package xml.dom;

Import Java.io.File;
Import Javax.xml.parsers.DocumentBuilder;

Import Javax.xml.parsers.DocumentBuilderFactory;
Import org.w3c.dom.Document;
Import Org.w3c.dom.NamedNodeMap;
Import Org.w3c.dom.Node;

Import org.w3c.dom.NodeList; public class ReadXmlFile2 {public static void main (string[] args) {try{File xmlFile = new File ("Src/resource/b
  
  Ook.xml ");
  
  Documentbuilderfactory builderfactory = Documentbuilderfactory.newinstance ();
  
  Documentbuilder builder = Builderfactory.newdocumentbuilder ();
  
  Document doc = Builder.parse (xmlFile);
  
  Doc.getdocumentelement (). normalize ();
  
  
  System.out.println ("Root element:" +doc.getdocumentelement (). Getnodename ());
  if (Doc.haschildnodes ()) {Printnode (Doc.getchildnodes ());
 
 }}catch (Exception e) {e.printstacktrace ();
 } public static void Printnode (NodeList nodelist) {System.out.println ("------------------------");
 System.out.println (Nodelist.getlength ()); for (int i = 0; i< Nodelist.getlength ();
 
 
 i++) {Node node = (node) nodelist.item (i);
 
 if (node.getnodetype () = = Node.element_node) {System.out.println ("node name:" +node.getnodename ());
 
 System.out.println ("Node value:" +node.gettextcontent ());
  
  if (Node.hasattributes ()) {NamedNodeMap Nodemap = node.getattributes ();
  
  for (int j = 0; J < Nodemap.getlength (); j + +) {Node nodenew = Nodemap.item (j);
  System.out.println ("Node name" +nodenew.getnodename ());
  System.out.println ("Node value" +nodenew.getnodevalue ());
 } if (Node.haschildnodes ()) {Printnode (Node.getchildnodes ());

 }
 }
 
 }
 
 }

}

The output results are as follows:

Root element:bookstore------------------------node name:bookstore node value:everyday Italian Giada De Laurenti is 30.00 Harry Potter J. Rowling 29.99 XQuery Kick Start James McGovern 2003 49.99 Lear Ning XML Erik T. Ray 2003 39.95------------------------node Name:book node value:everyday Italian Giada De La Urentiis 30.00 Node name category node value cooking------------------------node name:title node Value:everyd AY Italian node name lang node value en------------------------node name:author node Value:giada De laurentiis------- -----------------node name:year node value:2005------------------------node Name:price node value:30.00----------- -------------node Name:book node Value:harry Potter J K. Rowling, 29.99 node name category node value Childr En------------------------node name:title node value:harry Potter node name lang node value en----------------------- -Node Name:author node Value:j K. Rowling------------------------node name:year node value:2005------------------------node Name:price n Ode value:29.99------------------------node name:book node value:xquery Kick Start James McGovern 2003 49.99 N Ode name category node value Web------------------------node name:title node value:xquery Kick Start node name Lang No 
De value en------------------------node name:author node value:james McGovern------------------------node name:year Node value:2003------------------------node name:price node value:49.99------------------------node Name:book nod 
E value:learning XML Erik T. Ray 2003 39.95 Node name category node value Web node name cover node value paperback
------------------------node name:title node value:learning XML node name lang node value en------------------------ Node Name:author node Value:erik T. Ray------------------------node name:year node value:2003---------------------- --Node Name:price node value:39.95------------------------

 

Questions about nodes:

 <book category= "Cooking" >
 <title lang= "en" >everyday italian</title>
 <author>giada De laurentiis</author>
 <year>2005</year>
 <price>30.00</price>
 </ Book>

For book Application: Doc.getchildnodes () gets a nodelist in which the nodelist length is 9
The 9 nodes were as follows:
Title Node
Lang node
Everyday node
Author node
Giada De Laurentiis Node
Year node
2005 Node
Price node
30.00 Node

PS: Here again for you to provide several online tools on XML operations for your reference to use:

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

Online format xml/on-line compression of XML:
Http://tools.jb51.net/code/xmlformat

XML Online compression/formatting tool:
http://tools.jb51.net/code/xml_format_compress

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.