XML解析——Java-DOM

参考了http://java.chinaitlab.com/XMLBeans/36082.html
还参考了某位的代码,找不到原文了,见谅
待解析的xml范例:
<?xml version="1.0" encoding="gb2312"?>
<books>
<book email="[email protected]">
    <name>Bible
</name>
    <price>60.0</price>
  </book>
  
  
    <book email="[email protected]">
    <name>TAOCP</name>
    <price>600.0
    </price>
  </book>
</books>

Java代码:
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;

public class TestDom {
	public static void main(String[] args) {
		//test2();
		getSpecTagVal("book.xml", "name");
	}
	/**
	 * getSpecTagVal直接提取某个tag的值,不理会层次结构
	 * */
	public static void getSpecTagVal(String filename, String tag){
		//String filename = "book.xml";
		// 获得一个XML文件的解析器
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			// 解析XML文件生成DOM文档的接口类,以便访问DOM
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.parse(new File(filename));
			// 获得根节点
			Element element = document.getDocumentElement();
			// 获得根节点的子节点列表
			NodeList books = element.getChildNodes();
			for(int i = 0; i < books.getLength(); i++){
				walkNode(books.item(i), tag);
			}
		} catch (Exception ie) {
			ie.printStackTrace();
		}
	}
	public static void test2(){
		String filename = "book.xml";
		// 获得一个XML文件的解析器
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			// 解析XML文件生成DOM文档的接口类,以便访问DOM
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.parse(new File(filename));
			// 获得根节点
			Element element = document.getDocumentElement();
			// 获得根节点的子节点列表
			NodeList books = element.getChildNodes();
			if(books != null){
				for(int i = 0; i < books.getLength(); i++){
					Node book = books.item(i);
					System.out.println("==========" + i + "============");
					//System.out.println(book.getAttributes().toString());
					if(book.getNodeType() == Node.ELEMENT_NODE){
						String email = book.getAttributes().getNamedItem("email").getNodeValue();
						System.out.println(email);
						for(Node n = book.getFirstChild(); n != null; n = n.getNextSibling()){
							if(n.getNodeType() == Node.ELEMENT_NODE){
								if(n.getNodeName().equals("name")){
									String name = n.getNodeValue();
									String name1 = n.getFirstChild().getNodeValue();
									System.out.println(name + "\n" + name1);
								}
								if(n.getNodeName().equals("price")){
									//String price = n.getNodeValue();
									String price1 = n.getFirstChild().getNodeValue();
									System.out.println(price1);
								}
							}
						}
					}
				}
			}
		} catch (Exception ie) {			
			ie.printStackTrace();
		}
	}
	public static void test() {
		String filename = "book.xml";
		// 获得一个XML文件的解析器
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		try {
			// 解析XML文件生成DOM文档的接口类,以便访问DOM
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document document = db.parse(new File(filename));
			// 获得根节点
			Element element = document.getDocumentElement();
			// 获得根节点的子节点列表
			NodeList list = element.getChildNodes();
			for (int i = 0; i < list.getLength(); i++) {
				Node node = list.item(i);
				walkNode(node);
			}

		} catch (Exception ie) {
			
			ie.printStackTrace();
		}

	}

	private static void walkNode(Node anode, String tag) {		
		if(anode.getNodeType() == Node.ELEMENT_NODE && anode.getNodeName() == tag){
			System.out.println(anode.getNodeName() + "\t" + anode.getFirstChild().getNodeValue());
		}
		if(anode.hasChildNodes()){
			NodeList child = anode.getChildNodes();
			for (int i = 0; i < child.getLength(); i++) {								
				walkNode(child.item(i), tag);				
			}
		}
	}
	private static void walkNode(Node anode) {		
		if(anode.getNodeType() == Node.ELEMENT_NODE){
			System.out.println(anode.getNodeName() + "\t" + anode.getFirstChild().getNodeValue());
		}
		if(anode.hasChildNodes()){
			NodeList child = anode.getChildNodes();
			for (int i = 0; i < child.getLength(); i++) {								
				walkNode(child.item(i));				
			}
		}
	}


  这个代码是可以跑的,鉴于网上很多代码(我看到的)都产生莫名其妙的输出,我就把自己修修改改过的代码贴出来,主要方便新手学习吧!代码注释写的不多,看JDK的API来理解每个方法的作用吧!
目前我对xml解析也是刚刚接触,了解很浅,欢迎交流、批评指正

你可能感兴趣的:(java,xml)