How to parse an XML file in C, having a hard time with code example

This is a discussion on How to parse an XML file in C, having a hard time with code example within the C Programming forums, part of the General Programming Boards category; Hi, first post here . I'd like to parse an XML file in C. I have C/C++ experience, but have ...

  1. #1
    ysg
    ysg is offline
    Registered User
    Join Date
    Feb 2013
    Posts
    27

    How to parse an XML file in C, having a hard time with code example

    Hi, first post here .

    I'd like to parse an XML file in C. I have C/C++ experience, but have not done this for a while.

    This is my code example:
    Code:
    #include <stdio.h>#include <libxml2/parser.h>
    #include <libxml2/tree.h>
    
    
    static const char * document = "<doc/>";
    
    
    static void example3Func(const char * content, int length);
    
    
    int main(int argc, char * argv[])
    {
      LIBXML_TEST_VERSION
    
    
      // read our document (which is a dummy in this case).
      example3Func(document, 6);
    
    
      // cleanup function for the XML library.
      xmlCleanupParser();
    
    
      // this is to debug memory for regression tests.
      xmlMemoryDump();
    
    
      return 0;
    }
    
    
    static void example3Func(const char * content, int length)
    {
      xmlDocPtr doc;
    
    
      doc = xmlReadMemory(content, length, "noname.xml", NULL, 0);
    
    
      if (NULL == doc)
      {
        fprintf(stderr, "ERROR: Failed to parse document!\n\n");
    
    
        return;
      }
      else
      {
        printf("SUCCESS: Document parsed succesfully!\n\n");
      }
    
    
      xmlFreeDoc(doc);
    }
    When I compile, this is what I get:
    Code:
    $ gcc -o parse-xml parse-xml.c
    parse-xml.c:5:28: fatal error: libxml2/parser.h: No such file or directory
    compilation terminated.
    I'm doing this in cygwin.

    When I run the whereis command, libxml seems to be installed:
    Code:
    $ whereis libxml
    libxml: /lib/libxml2.la /usr/lib/libxml2.la /usr/share/man/man3/libxml.3.gz
    What am I doing wrong?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    You need to also install the development files as well, not just run-time support for libxml.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    ysg
    ysg is offline
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Quote Originally Posted by Salem View Post
    You need to also install the development files as well, not just run-time support for libxml.
    Right, the header files, got it. Configuring as I type.

  4. #4
    ysg
    ysg is offline
    Registered User
    Join Date
    Feb 2013
    Posts
    27
    Quote Originally Posted by Salem View Post
    You need to also install the development files as well, not just run-time support for libxml.
    Ok, I got another question. I compiled libxml (configure + make + make install), but when I compile my app, the compiler can't find libxml/parser.h and libxml/tree.h. This is what I get:
    Code:
    $ gcc -o parse-xml parse-xml.cparse-xml.c:5:27: fatal error: libxml/parser.h: No such file or directory
    compilation terminated.
    Any idea on how to fix this problem? Did I not compile/install the right thing again?

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    Well if it didn't install in the default location, you need something like
    gcc -I/path/to/header/files -o parse-xml parse-xml.c

    Where the -I parameter is the directory containing libxml/parser.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    981
    With gcc you may also set the environment variable CPATH to a list of directories. It is analogous to your platform's PATH variable except it applies to the paths where include files may be found.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    See also the Developer Corner/Point 1 in the libxml2-FAQ, if you want to find out the correct paths for your system.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-08-2012, 05:12 PM
  2. Having a hard time interpreting this code
    By BLG in forum C Programming
    Replies: 13
    Last Post: 09-29-2009, 05:19 PM
  3. I'm having a hard time making a .exe from a .cpp
    By manugarciac in forum C++ Programming
    Replies: 10
    Last Post: 05-13-2009, 04:40 PM
  4. I am having a hard time understanding this...
    By EvilPickles in forum C++ Programming
    Replies: 2
    Last Post: 07-10-2006, 05:26 AM
  5. Filling a vector from file... having a hard time...
    By criticalerror in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2003, 06:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21