tags:

views:

52

answers:

2

Hello. Is it possible to do in place edit of XML document using xpath ? I'd prefer any python solution but Java would be fine too.

+3  A: 

XPath is not intended to edit document in place, as far as I know. It is intended to only select nodes of the document. XSLT relies on XPath and can transform documents.

Regarding Python, see answer to this question: how to use xpath in python. It mentions also libraries which can do XSLT transformations.

jetxee
A: 

Using XML to store data is probably not optimal, as you experience here. Editing XML is extremely costly.

One way of doing the editing is parsing the xml into a tree, and then inserting stuff into that three, and then rebuilding the xml file.

Editing an xml file in place is also possible, but then you need some kind of search mechanism that finds the location you need to edit or insert into, and then write to the file from that point. Remember to also read the remaining data, because it will be overwritten. This is fine for inserting new tags or data, but editing existing data makes it even more complicated.

My own rule is to not use XML for storage, but to present data. So the storage facility, or some kind of middle man, needs to form xml files from the data it has.

Tor Valamo
Thank you. I'm not using XML for storage it's a configuration file.
erb
Then it's probably feasible and acceptable to parse it into a list tree and edit it that way. :)
Tor Valamo