-
Help with xml
I have a problem when I want to do searches in java. It returns some kind of exception that says "the root is empty" I have the same problem when I want to delete elements in a XML
Can you send me an example ? I don't know how can i do that.
Thank you guys.
-
This is strange.
Can you send the exact error ?
-
-
You could use xpath to search through an XML document.
Here is an example:
Code:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setIgnoringComments(true);
DocumentBuilder parser = dbf.newDocumentBuilder();
Document doc = parser.parse(yourfile.xml);
String text = "Text to search for";
// look for the text somewhere in a nested Element
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xpath.evaluate("//*[contains(text(),'" + text + "')]", doc, XPathConstants.NODESET);
for (int i = 0, n = nodes.getLength(); i < n; i++) {
Node node = nodes.item(i);
String path;
path = node.getNodeName() + '/' + node.getTextContent()
}
This should search through the XML and return any values that contain your search text.