欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java使用xpath解析xml示例分享

程序员文章站 2024-02-24 18:24:34
xpath即为xml路径语言(xml path language),它是一种用来确定xml文档中某部分位置的语言。xpath基于xml的树状结构,提供在数据结构树中找寻节点...

xpath即为xml路径语言(xml path language),它是一种用来确定xml文档中某部分位置的语言。xpath基于xml的树状结构,提供在数据结构树中找寻节点的能力。起初 xpath 的提出的初衷是将其作为一个通用的、介于xpointer与xsl间的语法模型。但是 xpath 很快的被开发者采用来当作小型查询语言。

xpathtest.java

复制代码 代码如下:

package com.hongyuan.test;

import java.io.file;
import java.io.ioexception;

import javax.xml.parsers.documentbuilder;
import javax.xml.parsers.documentbuilderfactory;
import javax.xml.parsers.parserconfigurationexception;
import javax.xml.xpath.xpath;
import javax.xml.xpath.xpathconstants;
import javax.xml.xpath.xpathexpressionexception;
import javax.xml.xpath.xpathfactory;

import org.w3c.dom.document;
import org.w3c.dom.node;
import org.w3c.dom.nodelist;
import org.xml.sax.saxexception;

public class xpathtest {

 public static void main(string[] args) throws parserconfigurationexception,
   saxexception, ioexception, xpathexpressionexception {

  // 解析文件,生成document对象
  documentbuilder builder = documentbuilderfactory.newinstance()
    .newdocumentbuilder();
  document document = builder.parse(new file("bookstore.xml"));

  // 生成xpath对象
  xpath xpath = xpathfactory.newinstance().newxpath();

  // 获取节点值
  string webtitle = (string) xpath.evaluate(
    "/bookstore/book[@category='web']/title/text()", document,
    xpathconstants.string);
  system.out.println(webtitle);

  system.out.println("===========================================================");

  // 获取节点属性值
  string webtitlelang = (string) xpath.evaluate(
    "/bookstore/book[@category='web']/title/@lang", document,
    xpathconstants.string);
  system.out.println(webtitlelang);

  system.out.println("===========================================================");

  // 获取节点对象
  node bookweb = (node) xpath.evaluate(
    "/bookstore/book[@category='web']", document,
    xpathconstants.node);
  system.out.println(bookweb.getnodename());

  system.out.println("===========================================================");

  // 获取节点集合
  nodelist books = (nodelist) xpath.evaluate("/bookstore/book", document,
    xpathconstants.nodeset);
  for (int i = 0; i < books.getlength(); i++) {
   node book = books.item(i);
   system.out.println(xpath.evaluate("@category", book,
     xpathconstants.string));
  }

  system.out.println("===========================================================");
 }

}

bookstore.xml

复制代码 代码如下:

<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
 <book category="cooking">
   <title lang="en">everyday italian</title>
   <author>giada de laurentiis</author>
   <year>2005</year>
   <price>30.00</price>
 </book>
 <book category="children">
   <title lang="en">harry potter</title>
   <author>j k. rowling</author>
   <year>2005</year>
   <price>29.99</price>
 </book>
 <book category="web">
   <title lang="en">learning xml</title>
   <author>erik t. ray</author>
   <year>2003</year>
   <price>39.95</price>
 </book>
</bookstore>


运行效果

java使用xpath解析xml示例分享