import java.io.StringReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;public class Evaluation {
public static String extractString(String xmlStr, String xpath)
throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath xp = factory.newXPath();
XPathExpression xpe = xp.compile(xpath);
InputSource is = new InputSource(new StringReader(xmlStr));
String result = xpe.evaluate(is);
return result;
}public static NodeList extractNodeList(String xmlStr, String xpath)
throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath xp = factory.newXPath();
XPathExpression xpe = xp.compile(xpath);
InputSource is = new InputSource(new StringReader(xmlStr));
NodeList result = (NodeList) xpe.evaluate(is, XPathConstants.NODESET);
return result;
}public static String extractString(Node node, String xpath)
throws XPathExpressionException {
XPathFactory factory = XPathFactory.newInstance();
XPath xp = factory.newXPath();
XPathExpression xpe = xp.compile(xpath);
return xpe.evaluate(node);
}
}
2009年2月23日星期一
使用XPATH从XML/XHTML中提取数据
订阅:
博文评论 (Atom)
没有评论:
发表评论