XPath遍历输出XML所有叶子节点

XPath 是一门在 XML 文档中查找信息的语言。XPath 可用来在 XML 文档中对元素和属性进行遍历。

XPath 是 W3C XSLT 标准的主要元素,并且 XQuery 和 XPointer 都构建于 XPath 表达之上。

W3School 提供了详细的XPath教程。http://www.w3school.com.cn/xpath/


本文所展示的遍历算法参考自其他博客,由于不能确定原博主,故没有注明出处,特此说明。


本算法实现遍历输出XML所有叶子节点的path, value, attribute。

本文所用XML文档来自W3School示例文档books.xml。

<!--   Copyright w3school.com.cn  -->
<!--  W3School.com.cn bookstore example  -->
<bookstore>
    <book category="children">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="web" cover="paperback">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>
    <book category="web">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern, Per Bothner, Kurt Cagle</author>
        <year>2003</year>
        <price>49.99</price>
    </book>
</bookstore>

Java方法代码:

public class XmlUtil {

	/**
	 * 读取并解析XML文档
	 * 从文件读取XML,输入文件名,返回XML文档
	 * @param fileName
	 * @return XML文档
	 * @throws DocumentException
	 */
	public Document read(String fileName) throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File(fileName));
		return document;
	}
	
	
	/**
	 * 获取节点所有属性值
	 * @param element
	 * @return
	 */
	public String getNodeAttribute(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() + "]";
			xattribute += " " + e.getName() + "=" + "\"" + e.getText() + "\"" ;
		}
		return xattribute;
	}
	
	/**
	 * 递归遍历方法
	 * 
	 * @param element
	 * @return
	 */
	public ArrayList getElementList(Element element, ArrayList elemList) {
		//ArrayList<Leaf> elemList = new ArrayList<Leaf>();
		List elements = element.elements();
		if(elements.size() == 0) {
			//没有子元素
			String xpath = element.getPath();
			String value = element.getTextTrim();
			elemList.add(new Leaf(getNodeAttribute(element), xpath, value));
		} else {
			//有子元素
			Iterator it = elements.iterator();
			while (it.hasNext()) {
				Element elem = (Element) it.next();
				//递归遍历
				getElementList(elem, elemList);
				
			}
		}
		return elemList;
	}
	
	
}
测试代码:

public class testXmlUtil {

	/**
	 * @param args
	 */
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//存储xml元素信息的容器    
	    ArrayList<Leaf> elemList = new ArrayList<Leaf>();  
	    XmlUtil util = new XmlUtil();
	    try {
			Document document = util.read("xmlResource/books.xml");
			//测试遍历xml所有子节点
			//获取xml根元素
			Element root = document.getRootElement();
			elemList = util.getElementList(root, elemList);
			String x = getListString(elemList);
			//System.out.println(elemList.get(1).getXpath() + elemList.get(1).getValue());
			System.out.println("-----------原xml内容------------\n" + root.asXML());
			System.out.println("-----------解析结果------------\n" + x);
			
			//测试输出节点所有属性值
			Node node = document.selectSingleNode("/bookstore/book[1]");
			System.out.println("-----------节点所有属性值------------\n" + util.getNodeAttribute((Element) node));
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    	
	}
	
	//自定义输出方法
	public static String getListString(List elemList) {
		StringBuffer sb = new StringBuffer();
		for (Iterator it = elemList.iterator(); it.hasNext();) {
			Leaf leaf = (Leaf) 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(); 
	}

}

XML节点数据结构:

/**
 * xml节点数据结构
 * @author 
 *
 */
public class Leaf {

	//节点属性
	private String xattribute;
	//节点path
	private String xpath;
	//节点值
	private String value;
	public Leaf(String xattribute, String xpath, String value) {
		super();
		this.xattribute = xattribute;
		this.xpath = xpath;
		this.value = value;
	}
	public String getXattribute() {
		return xattribute;
	}
	public void setXattribute(String xattribute) {
		this.xattribute = xattribute;
	}
	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;
	}


输出结果:

-----------原xml内容------------
<bookstore>
    <book category="children">
        <title lang="en">Harry Potter</title>
        <author>J K. Rowling</author>
        <year>2005</year>
        <price>29.99</price>
    </book>
    <book category="cooking">
        <title lang="en">Everyday Italian</title>
        <author>Giada De Laurentiis</author>
        <year>2005</year>
        <price>30.00</price>
    </book>
    <book category="web" cover="paperback">
        <title lang="en">Learning XML</title>
        <author>Erik T. Ray</author>
        <year>2003</year>
        <price>39.95</price>
    </book>
    <book category="web">
        <title lang="en">XQuery Kick Start</title>
        <author>James McGovern, Per Bothner, Kurt Cagle</author>
        <year>2003</year>
        <price>49.99</price>
    </book>
</bookstore>
-----------解析结果------------
xpath:/bookstore/book/title, value:Harry Potter, Attribute:  lang="en"
xpath:/bookstore/book/author, value:J K. Rowling
xpath:/bookstore/book/year, value:2005
xpath:/bookstore/book/price, value:29.99
xpath:/bookstore/book/title, value:Everyday Italian, Attribute:  lang="en"
xpath:/bookstore/book/author, value:Giada De Laurentiis
xpath:/bookstore/book/year, value:2005
xpath:/bookstore/book/price, value:30.00
xpath:/bookstore/book/title, value:Learning XML, Attribute:  lang="en"
xpath:/bookstore/book/author, value:Erik T. Ray
xpath:/bookstore/book/year, value:2003
xpath:/bookstore/book/price, value:39.95
xpath:/bookstore/book/title, value:XQuery Kick Start, Attribute:  lang="en"
xpath:/bookstore/book/author, value:James McGovern, Per Bothner, Kurt Cagle
xpath:/bookstore/book/year, value:2003
xpath:/bookstore/book/price, value:49.99

-----------节点所有属性值------------
 category="children"



  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值