[xml] getting an error with xmlTextReader ...




Hi,

I am trying to parse some simple xml code in a buffer and am running into problems:

a. This is my xml part (without the single quotes. I added the single quotes in my printf just to see that I did not have extra spaces at the end or something).

'<response status="success">
    <result>
 <address>
         <entry name="my-any">
        <ip-netmask>0.0.0.0/0</ip-netmask>
         </entry>
         <entry name="Corporate Email Server">
        <ip-netmask>192.168.80.150/32</ip-netmask>
         </entry>
         <entry name="Internal Executive Network">
        <ip-netmask>192.168.73.0/24</ip-netmask>
         </entry>
         <entry name="External Network">
        <ip-netmask>1:2:3:4:5:6:7:8/64</ip-netmask>
         </entry>
         <group name="root">
        <member>my-any</member>
        <member>Corporate Email Server</member>
        <member>Internal Executive Network</member>
        <member>External Network</member>
         </group>
     </address>
    </result>
</response>'

b. I am using the following code (pretty much the same as in the example  http://xmlsoft.org/examples/reader1.c)

static void
processNode(EspRequest *ep, xmlTextReaderPtr reader) {
    const xmlChar *name, *value;

    name = xmlTextReaderConstName(reader);
    if (name == NULL)
 name = BAD_CAST "--";

    value = xmlTextReaderConstValue(reader);

    espWriteFmt(ep, "depth: %d, nodeType: %d, name: %s, isEmptyElement:%d, hasValue: %d,",
     xmlTextReaderDepth(reader),
     xmlTextReaderNodeType(reader),
     name,
     xmlTextReaderIsEmptyElement(reader),
     xmlTextReaderHasValue(reader));
    if (value == NULL)
  espWriteFmt(ep, "value: NULL");
    else
     espWriteFmt(ep, "value: '%s'", value);

 espWriteFmt(ep, "<br>\n");
}

static void printElementsByReader(EspRequest *ep, xmlTextReaderPtr reader) {
 int ret;
 xmlChar *nodeName, *str;
 int nodeType;
 int rowNo = 0;
    pan_char_t textNodeName[32];

  ret = xmlTextReaderRead(reader);
        while (ret == 1) {
            processNode(ep, reader);
            ret = xmlTextReaderRead(reader);
        }
        if (ret != 0) {
            espWriteFmt(ep, "failed to parse, ret value %d\n", ret);
        }

}

main() {

    // I have retrieved the xml from another source into respBuffer

    // This prints out the xml shown above so I know I am getting what I expect.

  if (respBuffer) {
     espWriteFmt(ep, "'%s'\n", respBuffer);
     espWriteFmt(ep, "<p><br><hr><br>\n");
    }

 if (respBuffer) {
  xmlInitParser();

  // we don't want blank spaces to bother us
  xmlKeepBlanksDefault(0);


     /* Using XML Reader */
  xmlTextReader = xmlReaderForMemory(respBuffer, respLen, "noname.xml", NULL, 0);
  if (xmlTextReader == NULL) {
   espWriteFmt(ep, "Could not get xmlTextReader<p>\n");

   retval = 1;
   goto cleanup;
  }
 
  printElementsByReader(ep, xmlTextReader);

cleanup:

... has clean up code ...

}

c. The problem I am facing is that I am getting the following error:

noname.xml:20: parser error : Extra content at the end of the document

     </address></result></response>

 

d. Also in the output I am getting the following:


depth: 0, nodeType: 1, name: response, isEmptyElement:0, hasValue: 0,value: NULL
depth: 1, nodeType: 1, name: result, isEmptyElement:0, hasValue: 0,value: NULL
depth: 2, nodeType: 1, name: address, isEmptyElement:0, hasValue: 0,value: NULL
depth: 3, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 3, nodeType: 1, name: entry, isEmptyElement:0, hasValue: 0,value: NULL
depth: 4, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 4, nodeType: 1, name: ip-netmask, isEmptyElement:0, hasValue: 0,value: NULL
depth: 5, nodeType: 3, name: #text, isEmptyElement:0, hasValue: 1,value: '0.0.0.0/0'
depth: 4, nodeType: 15, name: ip-netmask, isEmptyElement:0, hasValue: 0,value: NULL
depth: 4, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 3, nodeType: 15, name: entry, isEmptyElement:0, hasValue: 0,value: NULL
depth: 3, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 3, nodeType: 1, name: entry, isEmptyElement:0, hasValue: 0,value: NULL
depth: 4, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 4, nodeType: 1, name: ip-netmask, isEmptyElement:0, hasValue: 0,value: NULL
depth: 5, nodeType: 3, name: #text, isEmptyElement:0, hasValue: 1,value: '192.168.80.150/32'
depth: 4, nodeType: 15, name: ip-netmask, isEmptyElement:0, hasValue: 0,value: NULL
depth: 4, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 3, nodeType: 15, name: entry, isEmptyElement:0, hasValue: 0,value: NULL
depth: 3, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 3, nodeType: 1, name: entry, isEmptyElement:0, hasValue: 0,value: NULL
depth: 4, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 4, nodeType: 1, name: ip-netmask, isEmptyElement:0, hasValue: 0,value: NULL
depth: 5, nodeType: 3, name: #text, isEmptyElement:0, hasValue: 1,value: '192.168.73.0/24'
depth: 4, nodeType: 15, name: ip-netmask, isEmptyElement:0, hasValue: 0,value: NULL
depth: 4, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 3, nodeType: 15, name: entry, isEmptyElement:0, hasValue: 0,value: NULL
depth: 3, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 3, nodeType: 1, name: entry, isEmptyElement:0, hasValue: 0,value: NULL
depth: 4, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 4, nodeType: 1, name: ip-netmask, isEmptyElement:0, hasValue: 0,value: NULL
depth: 5, nodeType: 3, name: #text, isEmptyElement:0, hasValue: 1,value: '1:2:3:4:5:6:7:8/64'
depth: 4, nodeType: 15, name: ip-netmask, isEmptyElement:0, hasValue: 0,value: NULL
depth: 4, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 3, nodeType: 15, name: entry, isEmptyElement:0, hasValue: 0,value: NULL
depth: 3, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 3, nodeType: 1, name: group, isEmptyElement:0, hasValue: 0,value: NULL
depth: 4, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 4, nodeType: 1, name: member, isEmptyElement:0, hasValue: 0,value: NULL
depth: 5, nodeType: 3, name: #text, isEmptyElement:0, hasValue: 1,value: 'my-any'
depth: 4, nodeType: 15, name: member, isEmptyElement:0, hasValue: 0,value: NULL
depth: 4, nodeType: 14, name: #text, isEmptyElement:0, hasValue: 1,value: ' '
depth: 4, nodeType: 1, name: member, isEmptyElement:0, hasValue: 0,value: NULL
depth: 5, nodeType: 3, name: #text, isEmptyElement:0, hasValue: 1,value: 'Corporate Email Server'
failed to parse, ret value -1

There are two more nodes "Internal Executive Network" and "External Network". Somehow after "Corporate Email Server" we don't get to the next two nodes.

This is my first programming attempt with xml and I may be doing something really silly. Any advice will be greatly appreciated.

thanks,

kunal.




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