Sample code for reading XML files using the DOM method

Source: Internet
Author: User
This article describes the sample code for reading XML files using the SAX method. if you need it, you can refer to the XML content (put it into the project)

 
 
  
   
    
Song of Ice and Fire
   George Martin
   
    
2014
   
   
    
89
   
  
  
   
    
Andersen fairy tale
   
   
    
2004
   
   
    
77
   
   
    
English
   
  
 

Code implementation

Import java. awt. print. book; 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. element; import org. w3c. dom. namedNodeMap; import org. w3c. dom. node; import org. w3c. dom. nodeList; import org. xml. sax. SAXException; public class XmlDOM {public static void main (String [] args) {DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance (); try {DocumentBuilder builder = dbf. newDocumentBuilder (); Document document = builder. parse ("xml/001.xml"); // nodeList gets the set of all nodes NodeList nodeList = document. getElementsByTagName ("book"); // facilitates each book node for (int I = 0; I
 
  
---------------------------------------- J 1st Book Information name = Song of Ice and Fire author = George Martin year = 2014 price = 89 Jun j 2nd book information name = Andersen fairy tale year = 2004 price = 77 language = English
 

The above is the detailed content of the sample code read using the DOM method in the XML file. For more information, see other related articles in the first PHP community!

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.