Modifying a part's XML in place

Coordinator
Jan 24, 2014 at 6:32 PM
At the risk of just talking to myself I'll post this anyway...

I want to open a part, parse it's XML into a DOM, modify a bit of the XML and write that XML back to the part.

From what I see in libopc this is a bit of a challenge. Here's how I imagine it has to work. Please correct me if I'm missing something or if you have a better approach:
  1. Find the part (p).
  2. Open an input stream (is) on the part. Stream the part into a buffer.
  3. Create an xmlReaderForMemory on the buffer.
  4. Get the reader's internal libxml2 reader.
  5. Get the libxml2 reader's current doc (xmlTextReaderCurrentDoc) as an xmlDocPtr
  6. Modify the xmlDoc
  7. Somehow write the xmlDoc back to a memory buffer. (don't know the api yet)
  8. Open an output stream for the part (p).
  9. Write the buffer to the output stream.
Whew! That's a lot of steps with libxml2 pieces involved but that seems to be what I need and this seems like it would be a common use case. Anyone have code to do a basic pass like this?

John
Coordinator
Mar 10, 2014 at 2:01 PM
To complete the echo chamber here's a follow-up for future visitors.

Yes. I ended up doing exactly the steps I spelled out above. The mystery API I didn't know in step 7 is xmlDocDumpMemory(). I created a bunch of C++ wrapper classes so I can get RAII handling of xml and opc pieces.

John