需要在pom.xml增加这个jar
<dependency>
<groupId>jdom</groupId>
<artifactId>jdom</artifactId>
<version>1.0</version>
</dependency>
package jnhx;
import java.io.File;
import java.io.StringReader;
import java.util.List;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import org.xml.sax.InputSource;
public class XpathOperater {
protected Logger log = Logger.getLogger(XpathOperater.class);
private Document document;
private String xPath;
public XpathOperater(String xml) {
String trimxml = xml.trim();
StringReader rd = new StringReader(trimxml);
InputSource is = new InputSource(rd);
SAXBuilder sb = new SAXBuilder();
try {
document = sb.build(is);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public XpathOperater(File file) {
SAXBuilder sb = new SAXBuilder();
try {
document = sb.build(file);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
public String getXPath() {
return xPath;
}
public void setXPath(String path) {
this.xPath = path;
}
public String getNodeValue() {
try {
XPath xp = XPath.newInstance(this.xPath);
Element ee = (Element) xp.selectSingleNode(document
.getRootElement());
return ee.getTextTrim();
} catch (Exception e) {
log.info("没有" + this.xPath + "节点");
return "";
}
}
@SuppressWarnings("unchecked")
public String[] getNodeValuesByString() {
try {
XPath xp = XPath.newInstance(xPath);
List<Element> list = (List<Element>) xp.selectNodes(document
.getRootElement());
String[] values = new String[list.size()];
int i = 0;
for (Element e : list) {
values[i] = e.getTextTrim();
i++;
}
return values;
} catch (JDOMException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public Double[] getNodeValuesByDouble() {
try {
XPath xp = XPath.newInstance(xPath);
List<Element> list = (List<Element>) xp.selectNodes(document
.getRootElement());
Double[] values = new Double[list.size()];
int i = 0;
for (Element e : list) {
values[i] = Double.parseDouble(e.getTextTrim());
i++;
}
return values;
} catch (JDOMException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public Integer[] getNodeValuesByInt() {
try {
XPath xp = XPath.newInstance(xPath);
List<Element> list = (List<Element>) xp.selectNodes(document
.getRootElement());
Integer[] values = new Integer[list.size()];
int i = 0;
for (Element e : list) {
values[i] = Integer.parseInt(e.getTextTrim());
i++;
}
return values;
} catch (JDOMException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public List<Element> getNodes() {
try {
XPath xp = XPath.newInstance(xPath);
List<Element> list = (List<Element>) xp.selectNodes(document
.getRootElement());
return list;
} catch (JDOMException e) {
throw new RuntimeException(e);
}
}
public Element getNode() {
try {
XPath xp = XPath.newInstance(xPath);
Element el = (Element) xp.selectSingleNode(document
.getRootElement());
return el;
} catch (JDOMException e) {
throw new RuntimeException(e);
}
}
}
测试方法:
package com.skysz.text;
public class TextXml {
public static void main(String[] args) {
//获取单个对象方法
XpathOperater xml = new XpathOperater("<response><head><tradeTime>2017-01-12</tradeTime></head><body><map><name>大鹏</name><age>20</age><sex>男</sex></map></body></response>");
xml.setXPath("/response/head/tradeTime");
String name = xml.getNodeValue();
System.out.println(name);
//获取list对象方法
XpathOperater xp = new XpathOperater("<response><head><tradeTime>2017-01-12</tradeTime></head><body><List><map><name>大鹏</name><age>20</age><sex>男</sex></map><map><name>大鹏2</name><age>202</age><sex>男2</sex></map></List></body></response>");
xp.setXPath("/response/body/List/map");
List<Element> els = xp.getNodes();
for (Element txel : els) {
String name_ = txel.getChildTextTrim("name");// 用户名
String age_ = txel.getChildTextTrim("age");// 年龄
String sex_ = txel.getChildTextTrim("sex");// 性别
System.out.println("用户名:" + name_);
System.out.println("年龄:" + age_);
System.out.println("性别:" + sex_);
}
}