azraelly / ticpp

Automatically exported from code.google.com/p/ticpp
0 stars 0 forks source link

Problems on getting started with ticpp #48

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hi,
I have big problems with parsing xml files with ticpp.
The problem is, I'm not very familiar with xml parsers and therefore I
don't now how to parse a xml file properly, since there are no ticpp tutorials.

How should I parse such a xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<Fruits>
    <Apple colour="green" price="1.1" amount="10" />
    <Banana colour="yellow" price="1.5" amount="10" />
    <Cherry colour="red" price="2.5" amount="1" />
</Fruits>

Here my attempt. The problem is that the values and names are not parsed
properly.

[code]
#include <iostream>
#include <ticpp.h>

void Read(const char* pFilename);

int main()
{
    try
    {
        Read("fruits.xml");
    }
    catch(ticpp::Exception& ex)
    {
        std::cout << ex.what();
    }

    return -1;
}

void Read(const char* pFilename)
{
    ticpp::Document doc(pFilename);
    doc.LoadFile();

    ticpp::Iterator<ticpp::Element> child;
    for(child = child.begin(doc.FirstChildElement()); child != child.end();
child++)
    {

        char acName[256];
        char acValue[256];
        child->GetValue(acName);
        std::cout << "Die Frucht: " << acName << std::endl;

        ticpp::Iterator< ticpp::Attribute > attribute;
        for(attribute = attribute.begin(child.Get()); attribute !=
attribute.end(); attribute++)
        {
            attribute->GetName(acName);
            attribute->GetValue(acValue);
            std::cout << acName << ": " << acValue << std::endl << std::endl;
        }
    }
}
[/code]

Original issue reported on code.google.com by a.schre...@gmx.net on 1 May 2009 at 8:16

GoogleCodeExporter commented 9 years ago
Hi, finally I got it. The problem was that I mixed up the char array with 
std::string.
Here a snipped how to parse through a xml file: Maybe you could make a tutorial 
out
of it ;)

XML-file to be parsed:
<?xml version="1.0" encoding="UTF-8" ?>
<Fruits>
    <Apple colour="green" price="1.1" amount="10" />
    <Banana colour="yellow" price="1.5" amount="10" />
    <Cherry colour="red" price="2.5" amount="1" />
</Fruits>

Approach how to achieve that:
[code]
#include <iostream>
#include <ticpp.h>

// simple function to parse a xml file
void Parse(const char* pcFilename);

int main()
{
    // whatch out for exceptions
    try
    {
        // parse the xml file with the name fruits.xml
        Parse("fruits.xml");
    }
    catch(ticpp::Exception& ex)
    {
        std::cout << ex.what();
    }

    return -1;
}

void Parse(const char* pcFilename)
{
    // first load the xml file
    ticpp::Document doc(pcFilename);
    doc.LoadFile();

    // parse through all fruit elements
    ticpp::Iterator<ticpp::Element> child;
    for(child = child.begin(doc.FirstChildElement()); child != child.end(); child++)
    {
        // The value of this child identifies the name of this element
        std::string strName;
        std::string strValue;
        child->GetValue(&strName);
        std::cout << "fruit: " << strName << std::endl;
        std::cout << "-------------" << std::endl;

        // now parse through all the attributes of this fruit
        ticpp::Iterator< ticpp::Attribute > attribute;
        for(attribute = attribute.begin(child.Get()); attribute != attribute.end();
attribute++)
        {
            attribute->GetName(&strName);
            attribute->GetValue(&strValue);
            std::cout << strName << ": " << strValue << std::endl;
        }
        std::cout << std::endl;
    }
}
[/code]

Original comment by a.schre...@gmx.net on 3 May 2009 at 9:18

GoogleCodeExporter commented 9 years ago
I added a tutorial section to the Wiki. Thanks.

Original comment by rpusz...@gmail.com on 3 May 2009 at 6:00