java 解析dom4j

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ParseXmlD4j {
	
	public Document getDocument(String file) throws DocumentException{
		SAXReader sr = new SAXReader();
		InputStream is  = ReaderFileUtils.getFile(file);
		Document doc = sr.read(is);
		return doc;
	}
	
	public Element getRootElement(String file) throws DocumentException{
		Document doc = getDocument(file);
		Element rootEle= doc.getRootElement();
		return rootEle;
	}
	
	public String getEncoding(String file) throws DocumentException{
		Document doc = getDocument(file);
		String encoding = doc.getXMLEncoding();
		return encoding;
	}
	
	public List<Element> getListEle(String str) throws DocumentException{
		List<Element> ls =new ArrayList<Element>();
		Element rootEle = getRootElement(str);
		Iterator it = rootEle.elementIterator();
		while(it.hasNext()){
			Element ele = (Element) it.next();
			ls.add(ele);
		}
		return ls;
	}
	
	public int getAttributesCounter(Element ele) throws DocumentException{
		return ele.attributeCount();
	}
	
	public List<String> getAttributesName(Element ele,String attr,String str) throws DocumentException{
		return getAttributesName(ele,null);
	}
	
	public List<String> getAttributesName(Element ele,String attr) throws DocumentException{
		List<String> str = new ArrayList<String>();
		List<Attribute> ls = ele.attributes();
		for(Attribute att : ls){
			str.add(att.getName());
		}
		return str;
	}
	
	public List<String> getAttributesValue(Element ele,String attr,String str) throws DocumentException{
		return getAttributesValue(ele,null);
	}
	
	public List<String> getAttributesValue(Element ele,String attr) throws DocumentException{
		List<String> str = new ArrayList<String>();
		List<Attribute> ls = ele.attributes();
		for(Attribute att : ls){
			str.add(att.getValue());
		}
		return str;
	}
	
	public String getAttributesValues(Element ele,String attr) throws DocumentException{
		return ele.attributeValue(attr);
	}
	
	public String getElesValue(Element ele) throws DocumentException{
		String text = null;
		text = ele.getTextTrim();
		System.out.println(text);
		return text;
	}
	
	public String getElesName(Element ele) throws DocumentException{
		String name = null;
		name = ele.getName();
		return name;
	}
	
	public void getAllNode(String str) throws DocumentException{
		List<Element> ele = getListEle(str);
		List<Element> e = new ArrayList<Element>();
		for(Element el : ele){
			for(Iterator i = el.elementIterator();i.hasNext();){
				System.out.println(((Element)i.next()).getName());
				e.add(((Element)i.next()));
			}
		}
	}
	
	public void getAllNode(Element ele) throws DocumentException{
			for(Iterator i = ele.elementIterator();i.hasNext();){
			Element et = (Element)i.next();
			getAllNode(et);
		}
	}
	
	public void getAllNodes(Element ele) throws DocumentException{
		for(Iterator i = ele.elementIterator();i.hasNext();){
		Element et = (Element)i.next();
		System.out.println(et.getName());
		for(Iterator j = et.elementIterator();j.hasNext();){
			Element ee = (Element)j.next();
		}
	}
}
	
	public void getObjectList(String str) throws DocumentException{
		List<Element> ele = getListEle(str);
		for(Element el : ele){
			getAllNode(el);
		}
	}
	
	public void get(String str) throws DocumentException{
		List<Element> ele = getListEle(str);
		for(Element el : ele){
			getAllNodes(el);
		}
	}
	
	public void getElementXpath(String file,String xpath) throws DocumentException{
		List<Element> ls = getDocument(file).selectNodes(xpath);
		for(Element el : ls){
			System.out.println(el.getTextTrim());
		}
	}
	
	public void getElementXpaths(String file,String xpath,String str) throws DocumentException{
		List<Element> ls = getDocument(file).selectNodes(xpath, str);
		for(Element el : ls){
			System.out.println(el.getTextTrim());
		}
	}
	
	public void getAttributeXpath(String file,String xpath) throws DocumentException{
		List<Attribute> ls = getDocument(file).selectNodes(xpath);
		for(Attribute el : ls){
			System.out.println(el.getValue());
		}
	}
	
	public static void main(String[] st) throws DocumentException{
		//new ParseXmlD4j().getListEle("interface.xml");
		 //new ParseXmlD4j().get("interface.xml");
		new ParseXmlD4j().getElementXpaths("interface.xml","/FrontInvest/Deals/Members/MemberName","1");
	}
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值