I'm trying to merge two xml files using libxml2 and c++.
These xml files have same schema but have different content, and i need to merge some elements into main xml file.
And these xml files have different indentation. (One has space but another has tab)
Here is my pseudo code:
xmlDocPtr doc1 = xmlParseFile(...);
xmlDocPtr doc2 = xmlParseFile(...);
xmlNodePtr node_from_doc1;
for (node; ...) {
...
xmlAddNextSibling(node_from_doc1, node);
...
}
xmlSaveFormatFile("merged.xml", doc1, 1);
And the merged xml file is not indented well.
<root_elem attr1="attr">
...
<child>child_text</child>
<child_merged>child_merged</child_merged>
</root_elem>
The 'child' element and 'child_merged' element should have same indentation.
How can I indent merged file well? Thanks in advance.
The documentation says: "Note that format = 1
provide node indenting only if xmlIndentTreeOutput = 1
or xmlKeepBlanksDefault(0)
was called."
Your pseudo code example does not contain any of these, so that might be the problem.
Also you can use tools like xmlstarlet format
or xmllint --format
to format / re-indent the xml file afterwards.
UPDATE: I did some more research and it seems like what you are trying to do is not possible with libxml2. To cite the FAQ:
Libxml2 will not invent spaces in the content of a document since all spaces in the content of a document are significant. If you build a tree from the API and want indentation:
- the correct way is to generate those yourself too.
- the dangerous way is to ask libxml2 to add those blanks to your content …
That being said, I did manage to get correctly indented output by using xmlKeepBlanksDefault (0)
. But I did not merge two documents, I just loaded one and inserted some nodes into it. So if it don't work for you, it might be because the nodes you are merging contains white spaces. So you could try trimming before inserting.
It's hard to give advice when you don't post a complete example of what you are trying to do.