Xml getValue() not working like example

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 :

    <highscore id="1">
    <highscore id="2">

And, here is my code :

    m_buffer    = m_file.readToBuffer();
    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 :


I get :


I expected :


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.addValue("test", i * 100);

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 !