使用dom及dom4j进行xml解析

使用dom进行xml解析:

dom解析相关类是jdk自带的类,使用dom解析时无需引入其他jar包,直接使用即可:

以下是一个dom解析xml的实例,xml内容如下:



	
		张三
		[email protected]
	
	
		李四
		[email protected]
	

java解析程序如下:

package com.fcy.xml.dom;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class DOMRead {
	public static void main(String[] args) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		builder = factory.newDocumentBuilder();
		Document doc = null;
		doc = builder.parse("d://test//dom_demo.xml");
		NodeList nl = doc.getElementsByTagName("linkman");
		int length = nl.getLength();
		for(int x = 0; x < length; x++){
			Element e = (Element) nl.item(x);
			System.out.println("姓名:" + e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
			System.out.println("邮箱:" + e.getElementsByTagName("email").item(0).getFirstChild().getNodeValue());
		}
	}
}

同样地,dom也可以创建xml文件,示例代码如下:

package com.fcy.xml.dom;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;

public class DOMWrite {
	public static void main(String[] args) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.newDocument();
		Element addresslist = doc.createElement("addresslist");
		Element linkman = doc.createElement("linkman");
		Element name = doc.createElement("name");
		Element email = doc.createElement("email");
		name.appendChild(doc.createTextNode("张三"));
		email.appendChild(doc.createTextNode("[email protected]"));
		linkman.appendChild(name);
		linkman.appendChild(email);
		addresslist.appendChild(linkman);
		doc.appendChild(addresslist);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer t = tf.newTransformer();
		t.setOutputProperty(OutputKeys.ENCODING, "GBK");
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(new File("d://test/output.xml"));
		t.transform(source, result);
		
		
		
	}
}

接下来,看下dom4j是如何进行xml解析的:

要想使用dom4j功能,需要引入dom4j的jar包,相关maven库依赖如下:


	    dom4j
	    dom4j
	    1.6.1
 

引入dom4j的jar包,就可以编写xml解析程序了,首先看一个生成xml文件的示例:

package com.fcy.xml.dom;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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;

public class DOMWrite {
	public static void main(String[] args) throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document doc = builder.newDocument();
		Element addresslist = doc.createElement("addresslist");
		Element linkman = doc.createElement("linkman");
		Element name = doc.createElement("name");
		Element email = doc.createElement("email");
		name.appendChild(doc.createTextNode("张三"));
		email.appendChild(doc.createTextNode("[email protected]"));
		linkman.appendChild(name);
		linkman.appendChild(email);
		addresslist.appendChild(linkman);
		doc.appendChild(addresslist);
		TransformerFactory tf = TransformerFactory.newInstance();
		Transformer t = tf.newTransformer();
		t.setOutputProperty(OutputKeys.ENCODING, "GBK");
		DOMSource source = new DOMSource(doc);
		StreamResult result = new StreamResult(new File("d://test/output.xml"));
		t.transform(source, result);
		
		
		
	}
}

程序运行后,生成的xml文件如下:




  
    张三
    [email protected]
  
  
    李四
    [email protected]
  


接下来,利用dom4j的解析功能,对生成的xml文件进行解析:

package com.fcy.xml.dom;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class DOMRead {
	public static void main(String[] args) throws Exception{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		builder = factory.newDocumentBuilder();
		Document doc = null;
		doc = builder.parse("d://test//dom_demo.xml");
		NodeList nl = doc.getElementsByTagName("linkman");
		int length = nl.getLength();
		for(int x = 0; x < length; x++){
			Element e = (Element) nl.item(x);
			System.out.println("姓名:" + e.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
			System.out.println("邮箱:" + e.getElementsByTagName("email").item(0).getFirstChild().getNodeValue());
		}
	}
}

运行程序,得到的结果如下:

总结:虽然dom与dom4j都能进行xml解析,从性能上来看,dom4j要优于dom,在实际开发中建议使用dom4j.另外,一些开源框架,如spring,hibernate等都是使用dom4j的解析方式。




你可能感兴趣的:(java)