java dom4j遍历xml_dom4j递归遍历XML的所有元素 | 学步园

package com.dom4j;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.dom4j.tree.DefaultAttribute;

public class dom4j {

//存储xml元素信息的容器

private static List elemList = new ArrayList();

/*

* 这个方法是通过xml路径读取xml文件,并将其DOM结构返回

*/

public Document parse2Document(String filePath){

Document document = null ;

//创建SAXReader读取器,专门用于读取xml

SAXReader saxread = new SAXReader();

//将xml转换为输入流

try {

InputStream inputStream = new FileInputStream(new File(filePath));

document = saxread.read(inputStream);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

//这个是抛出xml文件转化为输入流的异常

e.printStackTrace();

} catch (DocumentException e) {

// TODO Auto-generated catch block

//这个抛出SAXReader读取xml转换为document树的异常

e.printStackTrace();

}

return document ;

}

/**

* 获取根元素

*

* @return

* @throws DocumentException

*/

public Element getRootElement(String xmlfilePath) throws DocumentException {

Document document = parse2Document(xmlfilePath);

Element elem = document.getRootElement();

return elem;

}

/**

* 递归遍历方法

*

* @param element

* @see [类、类#方法、类#成员]

*/

public void getElementList(Element element)

{

List elements = element.elements();

// 没有子元素

if (elements.isEmpty())

{

String xpath = element.getPath();

String value = element.getTextTrim();

elemList.add(new dom_bean(getNoteAttribute(element), xpath, value));

}

else

{

// 有子元素

Iterator it = elements.iterator();

while (it.hasNext())

{

Element elem = (Element)it.next();

// 递归遍历

getElementList(elem);

}

}

}

/**

* 获取节点所有属性值

*

* @param element

* @return

* @see [类、类#方法、类#成员]

*/

public String getNoteAttribute(Element element)

{

String xattribute = "";

DefaultAttribute e = null;

List list = element.attributes();

for (int i = 0; i < list.size(); i++)

{

e = (DefaultAttribute)list.get(i);

xattribute += " [name = " + e.getName() + ", value = " + e.getText() + "]";

}

return xattribute;

}

public String getListString(List elemList)

{

StringBuffer sb = new StringBuffer();

for (Iterator it = elemList.iterator(); it.hasNext();)

{

dom_bean leaf = (dom_bean)it.next();

sb.append("xpath: " + leaf.getXpath()).append(", value: ").append(leaf.getValue());

if (!"".equals(leaf.getXattribute()))

{

sb.append(", Attribute: ").append(leaf.getXattribute());

}

sb.append("\n");

}

return sb.toString();

}

public static void main(String args[]){

dom4j dom = new dom4j();

Element root;

String x = "";

try {

root = dom.getRootElement("dog.xml");

dom.getElementList(root);

x = dom.getListString(elemList);

} catch (DocumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("-----------解析结果------------");

System.out.println(x);

}

}

package com.dom4j;

//这个类用来存储dom的数据结构

public class dom_bean {

// 节点属性

private String xattribute;

// 节点PATH

private String xpath;

// 节点值

private String value;

public dom_bean(String xattribute, String xpath, String value)

{

this.xattribute = xattribute;

this.xpath = xpath;

this.value = value;

}

public String getXpath()

{

return xpath;

}

public void setXpath(String xpath)

{

this.xpath = xpath;

}

public String getValue()

{

return value;

}

public void setValue(String value)

{

this.value = value;

}

public String getXattribute()

{

return xattribute;

}

public void setXattribute(String xattribute)

{

this.xattribute = xattribute;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值