Sample code for parsing XML files in Java

Source: Internet
Author: User
Code snippets, code sharing, PHP code sharing, Java code sharing, Ruby code sharing, Python code sharing, HTML code sharing, CSS code sharing, SQL code sharing, javaScript code sharing parses XML documents using Java. There are two most common methods: Simple API for XML) the Document Object Module, known as SAX and the Document Object Model based on trees and nodes, is called DOM.

Sun provides the Java API for XML Parsing (JAXP) interface to use SAX and DOM. through JAXP, we can use any XML parser compatible with JAXP. The JAXP interface contains three packages:

The interface recommended by org. w3c. dom for the XML standard Planning Document Object Model.

Org. xml. sax event-driven XML simple API (SAX) used for syntax analysis on XML)

The javax. xml. parsers factory tool allows programmers to obtain and configure special syntax analyzers.

Java XML parser

Import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. IOException; import java. io. inputStream; 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. node; import org. w3c. dom. nodeList; import org. xml. sax . SAXException; public class DomParse {public DomParse () {DocumentBuilderFactory domfac = DocumentBuilderFactory. newInstance (); try {DocumentBuilder dombuilder = domfac. newDocumentBuilder (); InputStream is = new FileInputStream ("WebRoot/WEB-INF/hell. xml "); Document doc = dombuilder. parse (is); Element root = doc. getDocumentElement (); NodeList books = root. getChildNodes (); if (books! = Null) {for (int I = 0; I
 
  

The above is the details of the sample code for parsing XML files using Java. 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.