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!