title: Read contents from xml file and store in an array
body: <p>I'm working with xml for the first time and I have some problems in storing  the contents of the xml file in an array. I'm using libxml2 for parsing the xml file and I'm able to get the data and able to print it. The code is given below:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;libxml/xmlmemory.h&gt;
  #include &lt;libxml/parser.h&gt;
  #include &lt;wchar.h&gt;
  
  wchar_t buffer[7][50]={"\0"};
  
  static void parseDoc(const char *docname) 
  {
  
      xmlDocPtr doc;
      xmlNodePtr cur;
      xmlChar *key;
      int i=0;
      doc = xmlParseFile(docname);
  
      if (doc == NULL ) {
  
      fprintf(stderr,"Document not parsed successfully. \n");
       return;
      }
  
      cur = xmlDocGetRootElement(doc);
  
      if (cur == NULL) 
      {
        fprintf(stderr,"empty document\n");
        xmlFreeDoc(doc);
        return;
      }
  
      cur = cur-&gt;xmlChildrenNode;
  
      while (cur != NULL) 
      {
          key = xmlNodeListGetString(doc, cur-&gt;xmlChildrenNode, 1);
          wmemcpy(buffer[i],(wchar_t*)(key),size(key));   /*segmentation fault at this stage*/        
          printf("Content : %s\n", key);
          xmlFree(key);
          i++;
          cur = cur-&gt;next;
      }
      xmlFreeDoc(doc);
      return;
  }
  
  int main(void) 
  {
     const char *docname="/home/workspace/TestProject/Text.xml;
     parseDoc (docname);
     return (1);
   }
  </code></pre>
  
  <p>The sample xml file is provided below</p>
  
  <pre><code> &lt;?xml version="1.0"?&gt;
   &lt;story&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;headline&gt;This is the headline&lt;/headline&gt;
    &lt;para&gt;This is the body text.&lt;/para&gt;
   &lt;/story&gt;
  </code></pre>
  
  <p>The output of the file contents when printed on the screen were as below </p>
  
  <p><code>Content : null</code></p>
  
  <p><code>Content : John Fleck</code></p>
  
  <p><code>Content : null</code></p>
  
  <p><code>Content : June 2, 2002</code></p>
  
  <p><code>Content : null</code></p>
  
  <p><code>Content : example keyword</code></p>
  
  <p><code>Content : null</code></p>
  
  <p><code>Content : This is the headline</code></p>
  
  <p><code>Content : null</code></p>
  
  <p><code>Content : This is the body text.</code></p>
  
  <p>I feel that the content of the file being null in few places is causing the problem in copy and hence generating the segmentation fault. Please let me know how to fix the problem and is there an better way to get the thing done. I had  done a similar xml file read using MSXML parser and this is my first time with Linux API's.</p>
  
  <p><strong>EDIT</strong> The copying part is performed as below but the contents of the wchart array are garbled. Further help would be appreciated.</p>
  
  <pre><code>while (cur != NULL) {
  
      key = xmlNodeListGetString(doc, cur-&gt;xmlChildrenNode, 1);
      if(key!=NULL)
      {
          wmemcpy(DiscRead[i],(const wchar_t *)key,sizeof(key));
          i++;
      }
  
      printf("keyword: %s\n", key);
      xmlFree(key);
  
      cur = cur-&gt;next;
  }
  </code></pre>