skagedal

libxml2 xmlTextReaderReadAttributeValue example

Aug 22nd, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <libxml/xmlreader.h>
  5.  
  6. static int check (int rc)
  7. {
  8.     if (rc == -1) {
  9.         fprintf (stderr, "Error from libxml2, fix your code!\n");
  10.         exit (1);
  11.     }
  12. }
  13.  
  14. void main ()
  15. {
  16.     // This works well:
  17.     char *s = "<!DOCTYPE doc [<!ENTITY bar 'bar'>]><doc att='foo&bar;'/>";
  18.     // But for the following xml, att2 does not get printed
  19.     // char *s = "<!DOCTYPE doc [<!ENTITY bar 'bar'>]><doc att1='foo&bar;' att2='foo&bar;'/>";
  20.    
  21.     xmlTextReaderPtr xml = xmlReaderForMemory (s, strlen(s), NULL, NULL, 0);
  22.    
  23.     while (check (xmlTextReaderRead (xml))) {
  24.         printf ("%s\n", xmlTextReaderConstName (xml));
  25.         while (check (xmlTextReaderMoveToNextAttribute (xml))) {
  26.             const char *attname = xmlTextReaderConstName (xml);
  27.             while (check (xmlTextReaderReadAttributeValue (xml))) {
  28.                 printf (" - child of attribute %s with node type %d, name %s and value %s\n",
  29.                     attname,
  30.                     xmlTextReaderNodeType (xml),
  31.                     xmlTextReaderConstName (xml),
  32.                     xmlTextReaderConstValue (xml));
  33.             }
  34.         }
  35.     }
  36. }
  37.  
  38. /* Compile with gcc -o attributevalue3 attributevalue3.c `pkg-config --cflags --libs libxml-2.0` */
Add Comment
Please, Sign In to add comment