Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to add properly child node in given xml file, libxml2
    primarykey
    data
    text
    <p>What I am doing is reading a <strong>xml</strong> file and trying to add a child node to a given <strong>xml</strong> file. But the problem is, it is not showing properly in the file here is the code: </p> <pre><code>xmlDocPtr doc; xmlNodePtr nodeptr=NULL , node = NULL , node_child =NULL; doc = xmlParseFile("Mainfile.xml"); if (doc == NULL ) { fprintf(stderr,"Document not parsed successfully. \n"); return; } nodeptr = xmlDocGetRootElement(doc); if (nodeptr == NULL) { fprintf(stderr,"empty document\n"); xmlFreeDoc(doc); return; } if (xmlStrcmp(nodeptr-&gt;name, (const xmlChar *) "story")) { fprintf(stderr,"document of the wrong type, root node != story"); xmlFreeDoc(doc); return; } node = xmlNewNode( NULL, BAD_CAST "Account" ); xmlNewProp(node, BAD_CAST "id", BAD_CAST "A001"); xmlAddChild(nodeptr , node); node_child = xmlNewChild(node, NULL, BAD_CAST "Country",BAD_CAST "US"); xmlAddChild(node,node_child); xmlAddChild(nodeptr , node); node_child = xmlNewChild(node, NULL, BAD_CAST "City", BAD_CAST "ABC"); xmlAddChild(node,node_child); xmlAddChild(nodeptr , node); node_child = xmlNewChild(node, NULL, BAD_CAST "ZIP",BAD_CAST "34040"); xmlAddChild(node,node_child); xmlAddChild(nodeptr , node); xmlSaveFile("Mainfile.xml", doc); xmlFree(doc); </code></pre> <p>And the structure of given xml file is </p> <pre><code>&lt; ?xml version="1.0"? &gt; &lt; Project &gt; &lt; author &gt;John Fleck&lt; /author &gt; &lt; datewritten &gt;June 2, 2002&lt; /datewritten &gt; &lt; keyword &gt;example keyword&lt; /keyword &gt; &lt; Account id = "A000" &gt; &lt; Country &gt;UK&lt; /Country &gt; &lt; City &gt;XYZ&lt; /City &gt; &lt; Zip &gt;67688&lt; /Zip &gt; &lt; /Account &gt; &lt; /Project &gt; </code></pre> <p>and after using my code the xml showing the content in below format </p> <pre><code>&lt; ?xml version="1.0"? &gt; &lt; Project &gt; &lt; author &gt;John Fleck&lt; /author &gt; &lt; datewritten &gt;June 2, 2002&lt;/datewritten&gt; &lt; keyword &gt;example keyword&lt; /keyword &gt; &lt; Account id = "A000" &gt; &lt; Country &gt;UK&lt; /Country &gt; &lt; City &gt;XYZ&lt; /City &gt; &lt; Zip &gt;67688&lt; /Zip &gt; &lt; /Account &gt; &lt; Account id = "A001" &gt;&lt; Country &gt;US&lt; /Country &gt;&lt; City &gt;ABC&lt; /City &gt;&lt; Zip &gt;34040&lt; /Zip &gt;&lt; /Account &gt;&lt; /Project &gt; </code></pre> <p>The main problem is it is not adding the child node with proper indentation.</p> <p>Can anyone suggest me what I'm doing wrong? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload