0
我有一个servlet查询数据库并将结果作为XML返回。我在servlet中设置变量使用在xpath中获取会话变量?
session.setAttribute("xml", xmlString);
如何甚至有可能使用XPath检索该属性。我需要得到它,解析它,然后将这些值写入网页。我对XPath相当陌生。
我有一个servlet查询数据库并将结果作为XML返回。我在servlet中设置变量使用在xpath中获取会话变量?
session.setAttribute("xml", xmlString);
如何甚至有可能使用XPath检索该属性。我需要得到它,解析它,然后将这些值写入网页。我对XPath相当陌生。
这是使用XPath的一个非常简单的例子。
您可以了解更多的:https://developer.mozilla.org/en-US/docs/Using_XPath
import java.io.StringReader;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.xml.sax.InputSource;
public class XPathSample {
public static void main(String[] args) {
String simpleXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><person><name>Bob</name></person>";
InputSource inputSource = new InputSource(new StringReader(simpleXML));
XPath xpath = XPathFactory.newInstance().newXPath();
try {
String name = xpath.evaluate("//name", inputSource);
System.out.println(name);
} catch (XPathExpressionException e) {
System.out.println(e.getMessage());
}
}
}