解析xml几个方法的简单封装

 

import java.io.File;

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

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
/**
 * 
 * @author D_xiao
 *
 */
public class AnalysisXmlByDom {
	/**
	 * 功能            :通过dom的方法解析文档,得到子节点内容
	 * 参数           :url:文档路径 ; rootElemnt:xml文件根节点; childElement:子节点
	 * return :子节点内容
	 * */
	public String dom(String url,String rootElement,String clildElement){
		String child = null;
		try{
			 File f=new File(url); 
			 DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 
			 DocumentBuilder builder=factory.newDocumentBuilder(); 
			 Document doc = builder.parse(f); 
			 NodeList nl = doc.getElementsByTagName(rootElement); 
			 for (int i=0;i<nl.getLength();i++){ 
				 child = doc.getElementsByTagName(clildElement).item(i).getFirstChild().getNodeValue(); 
			 } 
		 }catch(Exception e){ 
			 e.printStackTrace(); 
		 }
		return child;
	}
	public static void main(String[] args) {
		AnalysisXmlByJdom dom = new AnalysisXmlByJdom();
		String a = dom.dom("src/first.xml", "value", "no");
		System.out.println(a);
	}
}

import java.io.File;
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class AnalysisXmlByDom4j {
	/**
	 * 功能            :通过jdom的方法解析文档,得到子节点内容
	 * 参数           :url:文档路径 ; rootElemnt:xml文件根节点; childElement:子节点
	 * return :子节点内容
	 * */     
	public String dom4j(String url,String clildElement){
		String child = null;
		try { 
			//使用SAX方式解析xml
			SAXReader saxReader = new SAXReader();
			//读取first.xml资源
			Document doc = saxReader.read(new File(url));
			//取得根节点
			Element root = doc.getRootElement();
			Element foo; 
			//遍历根节点的所有孩子节点
			for (Iterator i = root.elementIterator(); i.hasNext();){ 
				foo = (Element) i.next(); 
				child = foo.elementText(clildElement); 
			}
		} catch (Exception e) { 
			e.printStackTrace(); 
		} 
		return child;
	}
	public static void main(String[] args) {
		AnalysisXmlByJdom dom4j = new AnalysisXmlByJdom();
		String a = jdom4j.jdom4j("src/first.xml", "value", "no");
		System.out.println(a);
	}
}

import java.io.File;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class AnalysisXmlByJdom {
	/**
	 * 功能            :通过jdom的方法解析文档,得到子节点内容
	 * 参数           :url:文档路径 ; rootElemnt:xml文件根节点; childElement:子节点
	 * return :子节点内容
	 * */
	public String jdom(String url,String rootElement,String childElement){
		String child = null;
		try { 
			//创建一个SAXBudilder对象
			SAXBuilder builder = new SAXBuilder();
			//读取first.xml资源
			Document doc = builder.build(new File(url));
			//获取根元素
			Element foo = doc.getRootElement(); 
			//获取根元素下面所有的子元素
			List<Element> allChildren = foo.getChildren(rootElement); 
			for(int i=0;i<allChildren.size();i++) { 
				child = (allChildren.get(i)).getChild(childElement).getText();
			} 
		} catch (Exception e) { 
			e.printStackTrace(); 
		}
		return child;
	}
	
	/**
	 * 测试
	 * @param args
	 */
	public static void main(String[] args) {
		AnalysisXmlByJdom jdom = new AnalysisXmlByJdom();
		String a = jdom.jdom("src/first.xml", "value", "no");
		System.out.println(a);
	}
}

你可能感兴趣的:(xml解析,dom4j解析xml,dom解析xml,xml解析方法封装,JDOM解析xml)