0
<element1 type="type1" name="value1">
  <start play="no"/>
  <element2 aaa="AAA"/>
  <element2 bbb="BBB"/>
  <element3 ccc="CCC">
     <element4/><!-- play="no"/>-->
  </element3>
</element1>

And I use following code to parse it:

int parse( const char *xml) {

    printf("Entered\n");
    xmlDoc  *doc = NULL;
    doc = xmlReadFile(xml, NULL, 0);

    if (doc == NULL)
        printf("Could not parse file\n");
    else {
        printf("Success\n");
        xmlFreeDoc(doc);
    }

    xmlCleanupParser();
    return 0;
}

But I get following error:

Entered
I/O warning : failed to load external entity "<element1 type="type1" name="value1">
  <start play="no"/>
  <element2 aaa="AAA"/>
  <element2 bbb="BBB"/>
  <element3 ccc="CCC">
     <element4/><!-- play="no"/>-->
  </element3>
</element1>

"
Could not parse file
hari
  • 9,439
  • 27
  • 76
  • 110

1 Answers1

2

I believe the first argument to

xmlReadFile(xml, NULL, 0);

is the name of the file, not the XML string itself. The error message indicates that you passed the actual XML to the method. I believe there is an xmlReadMemory() method that will suit your needs.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • I think you are right. I have an intermediate function that converts the file to XML stream. What should I use to read the XML stream? – hari May 08 '12 at 05:03
  • 1
    Use `xmlReadMemory()`, as you could have easily found on the [libXml2 website](http://xmlsoft.org/examples/index.html) under Code Examples. – Jim Garrison May 08 '12 at 05:13