I have a document which uses an XML namespace for which I want to increase /group/house/dogs
by one: (the file is called houses.xml
)
Round-tripping, unfortunately, isn't a trivial problem. With XML, it's generally not possible to preserve the original document unless you use a special parser (like DecentXML but that's for Java).
Depending on your needs, you have the following options:
If you control the source and you can secure your code with unit tests, you can write your own, simple parser. This parser doesn't accept XML but only a limited subset. You can, for example, read the whole document as a string and then use Python's string operations to locate
and replace anything up to the next <
. Hack? Yes.
You can filter the output. XML allows the string <
and then the same with
You can write your own, simple XML parser. Read the input as a string and then create Elements for each pair of <>
plus their positions in the input. That allows you to take the input apart quickly but only works for small inputs.