Re: [xml] DTD validation & whitespace removal



John,
Try parsing the document using:
xmlReadFile(URI, encoding, options)
with options set to XML_PARSE_NOBLANKS (in addition to anything else you want to use)

Here's what I mean:
#include <stdio.h>
#include <stdlib.h>

#include <libxml/parser.h>
#include <libxml/tree.h>

int
main(int argc, char **argv)
{
  if (argc != 2)
    {
      fprintf(stderr, "Usage: %s URI\n", argv[0]);
      return EXIT_FAILURE;
    }

  xmlDocPtr pDoc = xmlReadFile(argv[1], //filename
                               NULL,    //encoding
XML_PARSE_NOBLANKS); //don't allow blank nodes

  if (!pDoc)
    {
      fprintf(stderr, "Failed to parse %s\n", argv[1]);
      return EXIT_FAILURE;
    }

  xmlNodePtr pRoot = xmlDocGetRootElement(pDoc);

  if (!pRoot)
    {
      fprintf(stderr, "Failed to get root of document\n");
      return EXIT_FAILURE;
    }

  xmlNodePtr pCur = pRoot->children;

  while (pCur)
    {
      fprintf(stdout, "Node type: %d, Node name: %s\n",
              pCur->type, pCur->name);

      pCur = pCur->next;
    }
  return EXIT_SUCCESS;
}

Using the above to parse this:
<people_list>
  <person/>
  <person/>
</people_list>

I get this:
Node type: 1, Node name: person
Node type: 1, Node name: person

I hope that's what you're looking to achieve.

All the best!
Piotr



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