Re: [xml] addchild formatting in saved file?



On Sat, Mar 24, 2007 at 03:04:01PM -0400, Stefan Jeglinski wrote:
Another simple question.

I am parsing a formatted xml data file (by "formatted" I mean nicely 
indented etc).

If I change an element value and then save the file back out using

      xmlSaveFormatFile(filename, doc, 1)

It looks the same as the original.

But if I add a child, for example, and use the same 
xmlSaveFormatFile, the added child is correct but not formatted at 
all (no indents, no returns).

It's not clear to me from the documentation if the "1" argument in 
xmlSaveFormatFile is supposed to format it like I expect or not. [as 
per the docs I did call xmlKeepBlanksDefault(0) before the Save call; 
no change]

  See FAQ #4 which is somehow related.
In XML every blanks in content are significant. What you consider 
indentation could as well be significant data and the parser has
now way to guess that unless it uses a DTD. As a result libxml2 
is extremely cautious about adding or modifying text nodes even
if they have only spaces, tabs or line return.
The indenting algorithm is by definition a hack, and sometimes won't
behave as you expect because it care about data that you don't see.

Reason for pursuing this is because the xml data files are small and 
may very well be hand edited on occasion before being parsed again. I 
want them to "look nice" and be easy to edit if the parsing code has 
added a child in the interim.

The only reliable way to force an identation is to programmatically
add or modify the text nodes corresponding to indentation when you
modify an existing document.

Daniel

-- 
Red Hat Virtualization group http://redhat.com/virtualization/
Daniel Veillard      | virtualization library  http://libvirt.org/
veillard redhat com  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine  http://rpmfind.net/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]