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!