Example of java reading and parsing xml files

Source: Internet
Author: User
This article mainly introduces the example of reading and parsing xml files in java. This article creates an XML parsing class and explains the circular node output mode. For more information, see read local xml files, DOM parsing is used to load the entire xml file into the memory to form a DOM tree structure. the tree structure is convenient for traversing and manipulating.

The feature of DOM parsing is to read xml files and convert them into a dom tree structure, which can be traversed by nodes.

This is W3c concept of nodes.

If xml contains a large amount of data, dom is not suitable for xml parsing that contains a large amount of data because dom loads xml into the memory at a time. When a large amount of xml is contained, it is easier to use SAX for parsing to save memory.

The following is an example of using DOM to parse xml files:

The xml file structure is as follows:

 
  
   
   Everyday Italian Giada De Laurentiis 
   
    2005
    
   
    30.00
    
   
   
   Harry Potter J K. Rowling 
   
    2005
    
   
    29.99
    
   
   
   XQuery Kick Start James McGovern 
   
    2003
    
   
    49.99
    
   
   
   Learning XML Erik T. Ray 
   
    2003
    
   
    39.95
    
  
 

The xml parsing class is created 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
 
  

Resolution result:

Root element: bookstoreNode name: book----------------------------book category: cookingtitle name: Everyday Italianauthor name: Giada De Laurentiisyear :2005price : 30.00-------------------------Node name: book----------------------------book category: childrentitle name: Harry Potterauthor name: J K. Rowlingyear :2005price : 29.99-------------------------Node name: book----------------------------book category: webtitle name: XQuery Kick Startauthor name: James McGovernyear :2003price : 49.99-------------------------Node name: book----------------------------book category: webtitle name: Learning XMLauthor name: Erik T. Rayyear :2003price : 39.95-------------------------

The above is the corresponding value obtained through name,
The following output uses the cyclic node method:
The code for the cyclic node output method 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/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 (); 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
   
    

The output result is as follows:

Root element: bookstore------------------------node name: bookstorenode value:    Everyday Italian Giada De Laurentiis 2005 30.00     Harry Potter J K. Rowling 2005 29.99     XQuery Kick Start James McGovern 2003 49.99     Learning XML Erik T. Ray 2003 39.95   ------------------------node name: booknode value:  Everyday Italian Giada De Laurentiis 2005 30.00  node name categorynode value cooking------------------------node name: titlenode value: Everyday Italiannode name langnode value en------------------------node name: authornode value: Giada De Laurentiis------------------------node name: yearnode value: 2005------------------------node name: pricenode value: 30.00------------------------node name: booknode value:  Harry Potter J K. Rowling 2005 29.99  node name categorynode value children------------------------node name: titlenode value: Harry Potternode name langnode value en------------------------node name: authornode value: J K. Rowling------------------------node name: yearnode value: 2005------------------------node name: pricenode value: 29.99------------------------node name: booknode value:  XQuery Kick Start James McGovern 2003 49.99  node name categorynode value web------------------------node name: titlenode value: XQuery Kick Startnode name langnode value en------------------------node name: authornode value: James McGovern------------------------node name: yearnode value: 2003------------------------node name: pricenode value: 49.99------------------------node name: booknode value:  Learning XML Erik T. Ray 2003 39.95  node name categorynode value webnode name covernode value paperback------------------------node name: titlenode value: Learning XMLnode name langnode value en------------------------node name: authornode value: Erik T. Ray------------------------node name: yearnode value: 2003------------------------node name: pricenode value: 39.95------------------------

Node problems:

     
      Everyday ItalianGiada De Laurentiis
      
       2005
      
      
       30.00
      
     

For the book application: doc. getChildNodes (), get a NodeList, where the length of NodeList is 9.
The nine nodes are as follows:
Title node
Lang node
Everyday node
Author node
Giada De Laurentiis node
Year node
2005 nodes
Price node
30.00 nodes

For more articles about how to read and parse xml file instances in java, refer to PHP!

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.