Re: [xml] Problem with XML output formatting



Petyo,

for starters, I assume you switched the close tags for options and general in your example, otherwise it's not even XML. the formatting of the xml document is done through text. The general node actually contains:

a text node ( "<0d>       " )
the value1 element node
a text node ( "<0d>       " )
the value2 element node
a text node ( "<0d>       " )
the value3 element node
a text node ( "<0d>   " )


(don't punish me for the exact number of spaces, it's just an indication).

When you remove the value2 element node, you end up with:
a text node ( "<0d>       " )
the value1 element node
a text node ( "<0d>       " )
a text node ( "<0d>       " )
the value3 element node
a text node ( "<0d>   " )

so that's why you get the (not really) empty line. to get rid of the empty line, you'll have to remove the node after the value2 node as well

when you then add the new value3 node in the end, you get:

a text node ( "<0d>       " )
the value1 element node
a text node ( "<0d>       " )
a text node ( "<0d>       " )
the value3 element node
a text node ( "<0d>   " )
the other value3 element node

and yes, when printed, the new node is placed right before the closing tag, after the text node that takes care of the indent.. You'll have to handle the indentation yourself by handling the text nodes in code. Hint: drop any text nodes consisting of just whitespace after loading, and use an algorithm based on the depth of the node to add the indentation and line feeds just before printing.




--Gait.


Petyo Milotinov wrote:

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

        Hello!
        I want to parse an existing XML document. Here is how the XML should
look like:

<options>
   <general>
       <value1>some text</value1>
       <value2>another text</value2>
       <value3>another text 2</value3>
   </options>
</general>

        I remove a node in this way (node is pointing to "general" node):

if ( !xmlStrcmp( node->name, (const xmlChar*)"value2") )
{
        xmlNodePtr node_to_del = node;
        node = node->next;
        
        xmlUnlinkNode( node_to_del );
        xmlFreeNode( node_to_del );
        
        continue;
}

        But when I save the file with xmlSaveFormatFile( CONF_FILE, doc, 1 ) a
blank line appears:

<options>
   <general>
       <value1>some text</value1>
        
       <value3>another text 2</value3>
   </options>
</general>

        I'm inserting a new node in this way (orig_node is same as node):

xmlNewChild( orig_node, NULL, (const xmlChar*)"value4", (const
xmlChar*)"Just another text" );

        And here is the result in saved file:

<options>
   <general>
       <value1>some text</value1>
        
       <value3>another text 2</value3>
   <value3>another text 2</value3></options>
</general>

        There is no problem to parse and edit the file in my program. But I'm
trying to make a config file and it is possible someone to edit it so I
want to make an ordered output.
        What is wrong with my code?
        Thanks in advance!

        Best regards,
        Petyo Milotinov
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDMY06H/3eKQaFKT0RAiOeAJ46IVAyOQLgY3v61KQGYI3TcdHLGwCfXSCB
V3P5XtI9Ka0oKE/SpsUxUvY=
=cSxM
-----END PGP SIGNATURE-----
_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml






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