简体   繁体   中英

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 objection. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM