views:

44

answers:

3

I am having a lot of trouble working with the libxml2 library to parse an xml file.

I have weeded out a previous, similar problem, but have run into another.

Here is the problem code:

class SSystem{
public:
    //Constructors
    SSystem(){};
    //Make SSystem from XML Definition. Pass ptr to node
    SSystem(xmlNodePtr Nptr, xmlDocPtr Dptr){
        name = wxString((char *)xmlGetProp(Nptr, (xmlChar*)"name"), wxConvUTF8);
        //Move to next level down, the <general> element
        Nptr = Nptr->xmlChildrenNode;
        //Move one more level down to the <radius> element
        Nptr = Nptr->xmlChildrenNode;
        //Get Radius value
        if (!xmlStrcmp(Nptr->name, (const xmlChar *)"radius")) {
            char* contents = (char*)xmlNodeGetContent(Nptr);
            std::string test1 = std::string(contents);
            radius = wxString(contents, wxConvUTF8);
        }
    }

Both an xmlNodePtr and an xmlDocPtr are passed to the constructor, which works fine taking just a property ("name"), but is now choking on further parsing.

Here is a piece of the xml file in question:

<?xml version="1.0" encoding="UTF-8"?>
<Systems>
 <ssys name="Acheron">
  <general>
   <radius>3500.000000</radius> <-- I am trying to get this value (3500).
   <stars>300</stars>
   <asteroids>0</asteroids>
   <interference>0.000000</interference>
   <nebula volatility="0.000000">0.000000</nebula>
  </general>

It compiles fine, but crashes when the constructor is loaded (I know because, if I comment out the if conditional and the char* contents = (char*)xmlNodeGetContent(Nptr->xmlChildrenNode), it runs fine.

I've tried so many different things (removed one of the Nptr->xmlChildrenNode), but nothing works.

What is wrong?

+1  A: 

This:

char* contents = (char*)xmlNodeGetContent(Nptr->xmlChildrenNode)

Should probably be this:

char* contents = (char*)xmlNodeGetContent(Nptr)
JoshD
I tried that, still crashes.
Biosci3c
A: 

Okay, I am going to use a different XML parsing library, as Libxml is a bit too complicated for me.

I am looking into using MiniXML (http://www.minixml.org/).

Biosci3c
Oh yeah, this is way easier for me to understand.
Biosci3c
A: 

@Biosci3c: The method you are calling returns some fake value. You should not call the method

char*)xmlNodeGetContent(Nptr->xmlChildrenNode)

instead you have to get the data corresponding to radius in cdata callback method below here.

void cdataBlock (void * ctx, const xmlChar * value, int len)

Check out in libxml library documentation for reference...

iSight