create XML nodes based on XPath in java
I have a configuration file may or may not contain a certain element whose XPath is:
/configuration/server/address
when I write the configuration I have to create the node if it doesn't exist.
Node n = (Node)xp.evaluate("/configuration/server/address", configDocument, XPathConstants.NODE);
but, no surprise, node is null if the node doesn't exist in the real file.
QUESTION
Ok. My idea is have something like File: i can define a path that doesn't exist:
File f = new File("myInexistentDir/myInexistentSubdir");
then, i call f.mkdirs() and the path is replicated in the real world. Is it possible with java implementation of XPath?
Possible objectio开发者_运维问答n. It's obvious that not all XPath expressions are "creatable nodes". Where create the "//anywhere" element? I would say that "//anywhere" expression doesn't is a "path" in a strict sense, it's more similar to a query.
Nothing like this exists that I've ever seen. A quick glance at the JavaDocs of some of the alternative parsers didn't find anything either. XOM returns a Nodes
object from it's XPath engine, which does allow inserts. That might get you close to what you need.
精彩评论