如何使用以下xpath /xs:schema[1]/xs:complexType[1]/xs:sequence[1]/xs:element[15]
我已尝试过此代码,但xpath无法找到该位置
try {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(fileName);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("/xs:schema[1]/xs:complexType[1]/xs:sequence[1]/xs:element[15]");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
Text a = doc.createTextNode("value");
Element p = doc.createElement("newNode");
p.appendChild(a);
for (int i = 0; i < nodes.getLength(); i++) {
//nodes.item(i).insertBefore(p, nodes.item(i));
nodes.item(i).getParentNode().insertBefore(p, nodes.item(i));
}
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(fileName));
} catch (Exception e) {
e.printStackTrace();
}
请帮我解决我犯错误的原因以及为什么我的代码无法找到给定的xpath。