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");
}
}
java 解析dom4j
最新推荐文章于 2022-11-01 17:31:23 发布