Java DOM解析器 - 查詢XML文檔

演示示例

這是需要我們查詢的輸入XML文件:

Ferarri 101 Ferarri 201 Ferarri 301 Lamborgini 001 Lamborgini 002 Lamborgini 003 Benteley 1 Benteley 2 Benteley 3

演示示例:

QueryXmlFileDemo.java

package com.yiibai.xml; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; public class QueryXmlFileDemo { public static void main(String argv[]) { try { File inputFile = new File("input.txt"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); System.out.print("Root element: "); System.out.println(doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("supercars"); System.out.println("----------------------------"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\nCurrent Element :"); System.out.print(nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.print("company : "); System.out.println(eElement.getAttribute("company")); NodeList carNameList = eElement.getElementsByTagName("carname"); for (int count = 0; count < carNameList.getLength(); count++) { Node node1 = carNameList.item(count); if (node1.getNodeType() == node1.ELEMENT_NODE) { Element car = (Element) node1; System.out.print("car name : "); System.out.println(car.getTextContent()); System.out.print("car type : "); System.out.println(car.getAttribute("type")); } } } } } catch (Exception e) { e.printStackTrace(); } } }

這將產生以下結果:

Root element :cars

Current Element :supercars
company : Ferrari
car name : Ferarri 101
car type : formula one
car name : Ferarri 201
car type : sports car
car name : Ferarri 301
car type : sports car

Current Element :supercars
company : Lamborgini
car name : Lamborgini 001
car type :
car name : Lamborgini 002
car type :
car name : Lamborgini 003
car type :