Xml getValue() not working like example

Hi,
I am trying to read an xml file with getValue() method, but it returns… nothing !

Here is the example : http://openframeworks.cc/documentation/utils/ofXml.html

Here is my xml file :

<highscores>
    <highscore id="1">
      <date>oct</date>
      <heure>14</heure>
      <num>4</num>
      <score>1935</score>
    </highscore>
    <highscore id="2">
      <date>oct</date>
      <heure>14</heure>
      <num>4</num>
      <score>1935</score>
    </highscore>
</highscores> 

And, here is my code :

 m_file.open("xml/highscores.xml");
    m_buffer    = m_file.readToBuffer();
    m_xml.loadFromBuffer(m_buffer.getText());
    cout << ofToString(m_xml.getNumChildren()) << endl;
    cout << m_xml.getValue("highscores/highscore[0]/score") << endl;

The first cout gave me “2” > correct !
The second cout game me nothing

Is there any syntax error : “highscores/highscore[0]/score” ?

Hi there!

By default, you are already pointing to highscores (the root). So, you just need to do:
m_xml.getValue( “highscore[0]/score” );

Thanks, it’s working now !
I think there is a mistake with the example in the documentation…

On the example, because they first show you the function setToParent(), you will then need to set the full path. However, I get your point, it’s not very clear.

I’m now trying to save data in my xml file…

When I execute :

m_xml.addChild("highscore/test");
m_xml.save("xml/highscores.xml");

I get :

<highscore/>
    <test/> 

I expected :

<highscore>
    <test></test>
</highscore>

what’s wrong ?

The /> means that is empty. Most of these functions are relative to where you are on the file structure. So, based on you example file, if you do getNumChildren() when set to <highscores> you get 2. But if you do it set to <highscore id="0"> you get 4.

For example, if you wanted to add <test> to the file you loaded (this means having data, heure, num, score and test per score id) you could do something like:

for (int i = 0; i < m_xml.getNumChildren(); i++) {
   m_xml.setToChild(i);
   m_xml.addValue("test", i * 100);
   m_xml.setToParent();
}

What I did was: I was on the root <highscores> and for each child (each <highscore id="i">) I stepped in, added a value, and then went up and on to the next child.

Thanks again hubris

Most of these functions are relative to where you are on the file structure.

So true !