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

前端之家收集整理的这篇文章主要介绍了解析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","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 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","no");
		System.out.println(a);
	}
}
原文链接:https://www.f2er.com/xml/298678.html

猜你在找的XML相关文章