将以上xml文件命名为file.xml并放入相关包中就可以了package com.ray.tests.readXML;import java.io.File; 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.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException;public class ReadXML { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new File( "src/com/ray/tests/readXML/file.xml")); // normalize text representation doc.getDocumentElement().normalize(); // System.out.println ("Root element of the doc is " + // doc.getDocumentElement().getNodeName()); NodeList listOfIfResult = doc.getElementsByTagName("IfResult"); int totallistOfIfResult = listOfIfResult.getLength(); //System.out.println("Total no of people : " + totallistOfIfResult); for (int i = 0; i < totallistOfIfResult; i++) { Node ifResultNode = listOfIfResult.item(i); System.out.println(ifResultNode.getChildNodes().item(0).getNodeValue()); }
NodeList listOfIfResultInfo = doc.getElementsByTagName("IfResultInfo"); int totallistOfIfResultInfo = listOfIfResultInfo.getLength(); //System.out.println("Total no of people : " + totallistOfIfResult); for (int i = 0; i < totallistOfIfResultInfo; i++) { Node ifResultInfoNode = listOfIfResultInfo.item(i); System.out.println(ifResultInfoNode.getChildNodes().item(0).getNodeValue()); } } }
具体请参考
http://www.it.com.cn/f/edu/053/27/93819.htm
http://topic.csdn.net/u/20071217/18/5bdc30c8-481c-4147-8d63-783c674007ed.html
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.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;public class ReadXML {
public static void main(String[] args) throws ParserConfigurationException,
SAXException, IOException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File(
"src/com/ray/tests/readXML/file.xml")); // normalize text representation
doc.getDocumentElement().normalize();
// System.out.println ("Root element of the doc is " +
// doc.getDocumentElement().getNodeName()); NodeList listOfIfResult = doc.getElementsByTagName("IfResult");
int totallistOfIfResult = listOfIfResult.getLength();
//System.out.println("Total no of people : " + totallistOfIfResult); for (int i = 0; i < totallistOfIfResult; i++) {
Node ifResultNode = listOfIfResult.item(i);
System.out.println(ifResultNode.getChildNodes().item(0).getNodeValue());
}
NodeList listOfIfResultInfo = doc.getElementsByTagName("IfResultInfo");
int totallistOfIfResultInfo = listOfIfResultInfo.getLength();
//System.out.println("Total no of people : " + totallistOfIfResult); for (int i = 0; i < totallistOfIfResultInfo; i++) {
Node ifResultInfoNode = listOfIfResultInfo.item(i);
System.out.println(ifResultInfoNode.getChildNodes().item(0).getNodeValue());
}
}
}