import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.isoftstone.fwk.util.XmlTool;
/**
* 功能:
* @author yaos email: [email protected]
* @date 2008 11 25
*/
public class TestParseDOM {
/**
* 功能:
* @param args
* @throws TransformerException
*/
public static void main(String[] args) throws Exception {
//writeSchoolXml();
readSchoolXml();
}
private static void readSchoolXml() throws Exception{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream is = new FileInputStream(new File("src/writeXML.xml"));
Document doc = db.parse(is);
Element element = doc.getDocumentElement();
NodeList rootNodeList = element.getChildNodes();
//analysisNode(rootNodeList);
/****************************************************************************************************/
if(rootNodeList!=null){
for(int i = 0 ; i < rootNodeList.getLength() ; i ++){
Node node1 = rootNodeList.item(i);
if(node1.getNodeType()==Node.ELEMENT_NODE){
String name1 = node1.getAttributes().item(0).getNodeName();
String value1 = node1.getAttributes().item(0).getNodeValue();
System.out.println(name1+"-->"+value1);
NodeList nodelist2 = node1.getChildNodes();
if(nodelist2!=null){
for(int j = 0 ; j < nodelist2.getLength() ; j++){
Node node2 = nodelist2.item(j);
if(node2.getNodeType()==Node.ELEMENT_NODE){
String name2 = node2.getAttributes().item(0).getNodeName();
String value2 = node2.getAttributes().item(0).getNodeValue();
System.out.println(name2+"-->"+value2);
NodeList nodelist3 = node2.getChildNodes();
if(nodelist3!=null){
for(int k = 0 ; k < nodelist3.getLength() ; k++){
Node node3 = nodelist3.item(k);
if(node3.getNodeType()==Node.ELEMENT_NODE){
String childName = node3.getNodeName();
String childValue = node3.getFirstChild().getNodeValue();
System.out.println(childName+"-->"+childValue);
}
}
}
}
System.out.println("------------");
}
}
}
System.out.println("------------");
}
}
/****************************************************************************************************/
}
public static void analysisNode(NodeList nodelist){
if(nodelist!=null){
for(int k = 0 ; k < nodelist.getLength() ; k++){
Node node = nodelist.item(k);
if(node.getNodeType()==Node.ELEMENT_NODE){
if(node.getAttributes()!=null&&node.getAttributes().item(0)!=null){
String name2 = node.getAttributes().item(0).getNodeName();
String value2 = node.getAttributes().item(0).getTextContent();
System.out.println(name2+"-->"+value2);
}else{
String childName = node.getNodeName();
String childValue = node.getFirstChild().getTextContent();
System.out.println(childName+"-->"+childValue);
}
analysisNode(node.getChildNodes());
}
}
}
}
}
也可以将/***************/之间的去掉,调用递规函数analysisNode(NodeList nodelist)去解吸
方法中使用的xml的数据是 本BLOG中使用的 生成的school的xml数据及其结构
控制台应该输出:
1 . 调用递规函数输出为:
level-->1
name-->1
teacher-->teacher2
monitor-->stu2
number-->2
name-->2
teacher-->teacher3
monitor-->stu3
number-->4
level-->2
name-->1
teacher-->teacher3
monitor-->stu3
number-->4
name-->2
teacher-->teacher4
monitor-->stu4
number-->8
level-->3
name-->1
teacher-->teacher4
monitor-->stu4
number-->6
name-->2
teacher-->teacher5
monitor-->stu5
number-->12
2 . 调用非递规函数输出为:
------------
level-->1
------------
name-->1
teacher-->teacher2
monitor-->stu2
number-->2
------------
------------
name-->2
teacher-->teacher3
monitor-->stu3
number-->4
------------
------------
------------
------------
level-->2
------------
name-->1
teacher-->teacher3
monitor-->stu3
number-->4
------------
------------
name-->2
teacher-->teacher4
monitor-->stu4
number-->8
------------
------------
------------
------------
level-->3
------------
name-->1
teacher-->teacher4
monitor-->stu4
number-->6
------------
------------
name-->2
teacher-->teacher5
monitor-->stu5
number-->12
------------
------------
------------
------------