我需要检查一堆 .xml
文件中的特定权限,这是一个属性。如果存在这样的权限属性,我必须找出该属性具有哪个值。
这是我的代码,它产生NullPointerExceptions
:
public static void checkXmlPermissions(String path)
{
FileInputStream file;
try
{
file = new FileInputStream(new File(path));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
//XPath initialisieren;
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
String expression ="//*[@permission]"; //Expression;
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++)
{
String match = nodeList.item(i).getFirstChild().getNodeValue();
System.out.println(match);
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}
我想我的错误是NodeList
,但我自己找不到解决方案。
最佳答案
更改:
String match = nodeList.item(i).getFirstChild().getNodeValue();
至:
String match = nodeList.item(i).getTextContent();
关于java - XPath 检查属性并显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31177560/