ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > irrXML
forum topic indicator parsing always returns the last element
person icon
Peter
Guest
Quote
2008-12-13 01:44:32

Hi,

I've searched in all the forums for some clues to help me but i have not found something.

I'm a little bit lost with the xml parser.

Here's a little xml test file i've wrote :


<?xml version="1.0" encoding="UTF-8">

<chapter1>
     <character name="Bonzo"/>
     <dialog>Hi Peter</dialog>
     <dialog>How are you today?</dialog>
</chapter1>


Here's the code to read the file :


// Test XML
    irr::io::IrrXMLReader *ReadPtr = irr::io::createIrrXMLReader("../../media/test.xml");

    if(ReadPtr == 0)
    {
        std::cout<<"Failed to open the file"<<std::endl;

        return 1;
    }

    // The variable which held the result of this simple parse
    std::string characterName;
    std::string dialog1;

    while(ReadPtr->read())
    {
        switch(ReadPtr->getNodeType())
        {
            case(irr::io::EXN_ELEMENT) :
            {
                if(!strcmp("character", ReadPtr->getNodeName()) )
                {
                    characterName = ReadPtr->getAttributeValue("name");
                }
                break;
            }

            case(irr::io::EXN_TEXT) :
            {
                // I'm lost here
                dialog1 = ReadPtr->getNodeData();
                break;
            }

            default : // NOTHING
        }
    }

    delete ReadPtr;
    ReadPtr = 0;

    std::cout<<characterName<<std::endl;
    std::cout<<dialog1<<std::endl;

    return 0;
}


The result is :
Bonzo
How are you today?


As you can see, it reads only the last "dialog" element.

I'm doing it that way because i want to collect each line in an array.

So, can i retrieve each line ?

Thank you in advance for your patience, and sorry for faults.

Byebye,

person icon
niko
Moderator
Quote
2008-12-14 18:14:33

Because that's what you are doing in the code :) To print out each line, print it or store it for example where you call getNodeData.

person icon
Peter
Guest
Quote
2008-12-15 00:00:52

Hello,

Thank you for your answer !

I've changed my code, and i've tried to follow your recommendation.

Something is sure, i still don't understand how it works  :D .

Here's the code :


// IrrXml Test
    irr::io::IrrXMLReader *ReadPtr = irr::io::createIrrXMLReader("../../media/test.xml");

    if(ReadPtr == 0)
    {
        std::cout<<"Failed to open the file"<<std::endl;

        return 1;
    }

    // array which held the result of this simple parse
    std::string dialog[2];

    while(ReadPtr->read())
    {
        switch(ReadPtr->getNodeType())
        {
            case(irr::io::EXN_TEXT) :
            {
                irr::u16 i = 0;

                if(ReadPtr->getNodeData(), i < 2)
                {
                    dialog[i] = ReadPtr->getNodeData();
                    ++i;
                }

                break;
            }

            default : break;
        }
    }

    delete ReadPtr;
    ReadPtr = 0;

    for(irr::u16 i = 0 ; i < 2 ; ++i)
    {
        std::cout<<"dialog["<< i <<"] : "<< dialog[i] <<std::endl;
    }



Here's the result :

dialog[0] : How are you today?
dialog[1] :


Let me know, i'm doing the same mistake. I'm sorry, i'm certainly hopeless lol.

In any case, thank you for your time and for Irrlicht  :B.

Bye,

person icon
niko
Moderator
Quote
2008-12-15 18:32:20

Maybe it would help using instead of this line
 if(ReadPtr->getNodeData(), i < 2)

this one:
 if(ReadPtr->getNodeData() && i < 2)

But it's just a guess :)

person icon
Peter
Guest
Quote
2008-12-15 23:49:53

Hello,

This doesn't change anything.

I'll remake an investigation later.

Thank you for your answer and your time  ;)

Cheers !

Bye,

person icon
Grigor
Guest
Quote
2009-01-04 15:40:51


            case(irr::io::EXN_TEXT) :
            {
                irr::u16 i = 0;

                if(ReadPtr->getNodeData(), i < 2)
                {
                    dialog[i] = ReadPtr->getNodeData();
                    ++i;
                }

                break;
            }


You are resetting i to 0 so it keeps overwriting dialog[0].



person icon
Peter
Guest
Quote
2009-01-21 13:04:37

Ok, i see  ::) thank you !


Create reply:










 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






Copyright© Ambiera e.U. all rights reserved.
Contact | Imprint | Products | Privacy Policy | Terms and Conditions |