JDOM+Xpath解析XML
easyjava吧
全部回复
仅看楼主
level 8
噢丶Right 楼主
jdom 使用 xpath获取信息 package com.chen.xpath;import java.io.File;
import java.io.IOException;
import java.util.List;import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;public class TextXpath
{
private String pathString = "";
public TextXpath(String pathString){
this.pathString = pathString;
}
public void selectSome() throws JDOMException, IOException
{
File file = new File(pathString);
if (!file.exists())
{
System.out.println("文件不存在");
}
SAXBuilder sax = new SAXBuilder(false);
Document doc = sax.build(file);
Element rootElement = doc.getRootElement();
// List list = rootElement.getChildren();
// this.readXml(list);
// 查询绝对路径/metadata/MODULE/MODULE/OPERATE下的节点集合(包括根路径)
String xpathString = "/metadata/MODULE/MODULE/OPERATE";
List elist = XPath.selectNodes(rootElement, xpathString);
this.readXmlList(elist);
System.out.println("************************************************************"); // 查询所有节点名字为OPERATE的节点集合
String xpathStringA = "//OPERATE";
List elistA = XPath.selectNodes(rootElement, xpathStringA);
this.readXmlList(elistA);
System.out.println("************************A************************************");
// 查询所有节点名字为OPERATE中KEY="hr_chenYearAction_list"的节点集合
String xpathStringB = "//OPERATE[@KEY='hr_chenYearAction_list']";
List elistB = XPath.selectNodes(rootElement, xpathStringB);
this.readXmlList(elistB);
System.out.println("*************************B***********************************"); // 查询所有节点中KEY="hr_chenYearAction_list"的节点集合
String xpathStringC = "//*[@KEY='hr_chenYearAction_list']";
List elistC = XPath.selectNodes(rootElement, xpathStringC);
this.readXmlList(elistC);
System.out.println("*************************C***********************************"); // 使用 selectSingleNode 查询所有节点中 得到第一个节点 Element
String xpathStringD = "//MODULE/*";
Element element = (Element) XPath.selectSingleNode(rootElement, xpathStringD);
System.out.println(element.getAttributeValue("KEY").toString());
System.out.println(element.getTextNormalize());
// this.readXmlList(elistD);
System.out.println("***********************D*************************************");
/*
XPATH中一些有用的函数:
string concat (string, string, string*) 联接两个字符串
boolean starts-with (string, string) 判断某字符串是否以另一字符串开头
boolean contains (string, string) 判断某字符串是否包含另一字符串
string substring (string, number, number) 取子字符串
number string-length (string) 测字符串长度
number sum (node-set) 求和
number floor (number) 求小于此数的最大整数值
number ceiling (number) 求大于此数最小整数值
*
*/
// 查询所有节点中KEY以hr_chenYearAction开头的节点集合
String xpathStringE = "//*[starts-with(@KEY,'hr_chenYearAction')]";
List elistE = XPath.selectNodes(rootElement, xpathStringE);
this.readXmlList(elistE);
System.out.println("*************************E***********************************");
}
/**打印结果
*
* @author chen_weixian
* May 4, 2012 10:53:39 AM
* @param list
*/
public void readXmlList(List list)
{
for (int i = 0; i<list.size(); i++ )
{
Element el = (Element) list.get(i);
System.out.println(el.getAttributeValue("KEY"));
System.out.println(el.getAttributeValue("TITLE"));
}
}
public void readXml(List list)
{
for (int i = 0; i<list.size(); i++ )
{
Element el = (Element) list.get(i);
List listi = el.getChildren();
if (listi != null && listi.size() > 0)
{
readXml(listi);
}
else
{
System.out.println(el.getAttributeValue("KEY"));
System.out.println(el.getAttributeValue("TITLE"));
}
}
} /**
* @author chen_weixian
* May 4, 2012 9:06:12 AM
* @param args
*/
public static void main(String[] args)
{
String pathString = "F:/workspaceA/jzProject/config/resource_shop.xml";
TextXpath textXpath = new TextXpath(pathString);
try
{
textXpath.selectSome();
}
catch (JDOMException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
2013年05月31日 09点05分 1
level 9
2013年06月08日 07点06分 2
level 9
2013年06月11日 14点06分 4
level 9
2013年06月11日 14点06分 5
1