Note that there are some explanatory texts on larger screens.

plurals
  1. POxmlNewChild not always writing the value of the element
    text
    copied!<p>I have a very weird error with the xmlNewChild function using libxml2. I tried to keep the post as short as possible so I removed some insignificant logic. A boolean variable is defined for each variable to be written and when setting the variable value, the boolean is set to TRUE indicating if the tag is present, and if it is, it's written, otherwise is skipped</p> <p>I have defined a structure as follows:</p> <pre><code> tyedef struct _xml_parse_t { const char *name; void *variable; int type; _xml_parse_t *childs; } xml_parse_t; </code></pre> <p>Another function parses this structure and writes it to a char*. the function is defined as follows:</p> <pre><code>int generate_xml(xml_parse_t *p, char *out, const char*root) { xmlChar *s = NULL; int size = 0; xmlDocPtr doc = NULL; xmlNodePtr root_node = NULL; doc = xmlNewDoc(NULL); if(NULL == doc) return -1; root_node = xmlNewNode(NULL, BAD_CAST root); if(NULL == root_node) return -1; else xmlDocSetRootElement(doc, root_node); if(-1 == writeXmlToDoc(p, doc, root_node)) return -1; xmlDocDumpFormatMemoryEnc(doc, &amp;s, &amp;size, "UTF-8", 1); if(NULL == s) return -1; if(SUCCEED == ret) strcpy(out, (char *)s); if(NULL != s) xmlFree(s); xmlFreeDoc(doc); xmlCleanupParser(); xmlMemoryDump(); return 0; } </code></pre> <p>The writeXmlToDoc is defined as below</p> <pre><code>int writeXmlToDoc(xml_parse_t *parse, xmlDocPtr doc, xmlNodePtr root_node) { xml_parse_t *p = parse; xmlNodePtr node; while(NULL != p-&gt;name) { if( NODE_TYPE_PARENT == p-&gt;type &amp;&amp; NULL != p-&gt;childs) { node = xmlNewChild(root_node, NULL, BAD_CAST p-&gt;name, NULL); if(-1 == writeXmlToDoc(p-&gt;childs, doc, node)) return -1; } else { printf("Current value being written [%s]", (char *)cvtToXmlChar(p-&gt;variable, p-&gt;type)); node = xmlNewChild(root_node, NULL, BAD_CAST p-&gt;name, BAD_CAST cvtToXmlChar(p-&gt;variable, p-&gt;type)); } p++; } return 0; } </code></pre> <p>To keep this post as short as possible, the cvtToXmlChar checks the type of the variable and casts it to xmlChar and the printf before the xmlNewChild is showing correct value of the variable.</p> <pre><code>xml_parse_t child_parse[] = { /* Name var Type Childs*/ {"ChildInt", &amp;test_int1, NODE_TYPE_INT, NULL }, {"ChildLong", &amp;test_long1, NODE_TYPE_LONG, NULL }, {"ChildChar", test_char1, NODE_TYPE_STRING, NULL }, {"ChildShort", &amp;test_short1, NODE_TYPE_SHORT, NULL }, {"ChildDouble", &amp;test_double1,NODE_TYPE_DOUBLE, NULL }, { NULL } }; xml_parse_t parent_parse[] = { /* Name var Type Childs*/ {"ParentInt", &amp;test_int, NODE_TYPE_INT, NULL }, {"ParentLong", &amp;test_long, NODE_TYPE_LONG, NULL }, {"ParentChar", test_char, NODE_TYPE_STRING, NULL }, {"ParentShort", &amp;test_short, NODE_TYPE_SHORT, NULL }, {"ParentDouble", &amp;test_double, NODE_TYPE_DOUBLE, NULL }, {"ParentParent", NULL, NODE_TYPE_PARENT, child_parse }, { NULL } }; </code></pre> <p>and the variables set as follows:</p> <pre><code> test_int = 1; test_int1 = 2; test_long = 1; test_long1 = 2; sprintf(test_char, "TestParentCharTag"); sprintf(test_char1, "TestChildCharTag"); test_short = 1; test_short1 = 2; test_double = 1; test_double1 = 2; </code></pre> <p>The problem is, the output string has 2 missing values. Following is the output of running the test</p> <pre><code> &lt;ROOTELEMENT&gt; &lt;ParentInt&gt;1&lt;/ParentInt&gt; &lt;ParentLong&gt;&lt;/ParentLong&gt; &lt;ParentChar&gt;TestParentCharTag&lt;/ParentChar&gt; &lt;ParentShort&gt;1&lt;/ParentShort&gt; &lt;ParentDouble&gt;1.000000&lt;/ParentDouble&gt; &lt;ParentParent&gt; &lt;ChildInt&gt;&lt;/ChildInt&gt; &lt;ChildLong&gt;2&lt;/ChildLong&gt; &lt;ChildChar&gt;TestChildCharTag&lt;/ChildChar&gt; &lt;ChildShort&gt;2&lt;/ChildShort&gt; &lt;ChildDouble&gt;2.000000&lt;/ChildDouble&gt; &lt;/ParentParent&gt; &lt;/ROOTELEMENT&gt; </code></pre>
 

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