gpt4 book ai didi

java - 使用 Xpath 读取节点列表

转载 作者:行者123 更新时间:2023-11-29 05:49:15 24 4
gpt4 key购买 nike

这是我的 XML 文件:

<sitemesh>
<mapping path="/editor/tempPage/**" exclude="true"/>

<mapping decorator="/WEB-INF/views/decorators/detailstheme.jsp"
path="/*" exclude="false" />

</sitemesh>

我想要映射节点及其属性值的列表。这应该使用 Xpath 完成。

我的 xpath 表达式是:

expr = xpath.compile("/sitemesh/mapping");

但我在节点列表中得到的是空值。

这是我的代码:

Map<String,String> map=new HashMap<String, String>();

// reading xml file
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document doc = null;
XPathExpression expr = null;
try {
builder = factory.newDocumentBuilder();
// creating input stream
doc = builder.parse(file);
XPathFactory xpf = XPathFactory.newInstance();
XPath xpath = xpf.newXPath();
expr = xpath.compile("//mapping");
} catch (Exception e) {
LOG.error("some exception message", e);
}


//------------------------------------------------------------------------

NodeList attributeElements = null;
try {
attributeElements =(NodeList)expr.evaluate(doc, XPathConstants.NODE);

} catch (XPathExpressionException e) {
LOG.error("some exception message", e);
}
System.out.println("lenght:"+attributeElements.getLength());
for (int i = 0; i < attributeElements.getLength(); i++) {
Node node=attributeElements.item(i);
System.out.println("node:"+node.getNodeValue());
NamedNodeMap attrs = node.getAttributes();
for(i = 0 ; i<attrs.getLength() ; i++) {
Attr attribute = (Attr)attrs.item(i);
System.out.println("Node Attributes : " + attribute.getName()+" = "+attribute.getValue());
}
}


//-------------------------------------------------------------------------

// writing xml file
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer;
try {
transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);// creating output
// stream
transformer.transform(source, result);
} catch (Exception e) {
LOG.error("some exception message", e);
}

return map;

i am getting null for attributeElements

我想在 JSP 页面上显示路径、装饰器和排除的值。但是我无法通过 xpath 表达式获取节点列表。

我想要在 Xpath 中读取 mapping 节点元素的解决方案。

最佳答案

[edit]/sitemesh/mapping 也可以。

这里的问题是您评估 XPathConstants.NODE 的表达式,而 nodeList 映射到 XPathConstants.NODESET .请引用以下链接。

http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/xpath/XPathConstants.html#NODESET

添加示例代码仅用于说明目的:

public void testXpathExpr(){

String testXML = "<sitemesh><mapping path=\"/editor/tempPage/**\" exclude=\"true\"/><mapping decorator=\"/WEB-INF/views/decorators/detailstheme.jsp\" path=\"/*\" exclude=\"false\" /></sitemesh>";

NodeList nodeList = getNodeList(testXML);
}

private NodeList getNodeList(String xml) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = docFactory.newDocumentBuilder();

document = builder.parse(new ByteArrayInputStream( xml.getBytes() ) );
XPathExpression exprPath = xpath.compile(xpathExpr);
NodeList nodeList = (NodeList) exprPath.evaluate(document, XPathConstants.NODESET);;
return nodeList;
}

希望这对您有所帮助!

关于java - 使用 Xpath 读取节点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14620185/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com