DocumentBuilder builder=null;
XPath xpath=null;
XPathFactory xpathfactory = XPathFactory.newInstance();
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
try {
xpath = xpathfactory.newXPath();
builder = builderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new ServletException("can't init DocumentBuilder.." + e.getMessage() );
}
//将xml字符串 转换成流 ,此处也可以直接读取 .xml文件 转换成 inputStream
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
//将stream转换成文档
Document document = builder.parse(is);
// 对深层次结构的操作具有指向性
String status = getDocumentValue(document,"/根节点标签名/ 属性名或节点名"); //@ 的代表属性 如果要获取属性名 则 需要在 属性名前加 @
xpath 简单事例
最新推荐文章于 2022-01-14 17:54:20 发布