Sign in to follow this  
ratha

XML Parsing using Tiny XML

Recommended Posts

Hi!
I've an XML file whose contents are as following:

[code]
<?xml version="1.0" ?>
<response action="registration">
<element>
<properties name="username">cst11</properties>
<properties name="playerhandle">238768881697142018728993387950411141</properties>
<properties name="skinId">RCA001</properties>
<properties name="errcode" >loginnotAllowed</errcode>
</element>
</response>
[/code]

I've to get the values ("cst11","238768881697142018728993387950411141","RCA001" and "loginnotAllowed") using TinyXML. How to do this? Can any body post some sample code for this?

Share this post


Link to post
Share on other sites
I wrote the following code:

[code] std::string temp = objGen.WStringToString(res).c_str();

OutputDebugString(objGen.StringToWString(temp).c_str());

TiXmlDocument doc;
doc.Parse(temp.c_str() , 0, TIXML_ENCODING_UTF8);
doc.SaveFile("response.xml");
string errorcode;
if(doc.LoadFile())
{
TiXmlElement *res = doc.FirstChildElement("response");

if(res)
{
TiXmlElement * ele = res->FirstChildElement("element");
if(ele)
{
TiXmlElement *prop = ele->NextSiblingElement()->NextSiblingElement()->NextSiblingElement()->NextSiblingElement();
errorcode = prop->GetText();
}
}
}[/code]

But not returning the inner XML values(cst11,238768881697142018728993387950411141,RCA001and loginnotAllowed). What to do?

Share this post


Link to post
Share on other sites
[code]
TiXmlNode* parent = ...;

TiXmlNode* child = parent->FirstChild(); // parent == properties
if(child && child->Type() == TiXmlNode::TEXT) {
const char* text = child->Value(); // text is the string version of what you want, convert it if need be
}

[/code]

Share this post


Link to post
Share on other sites
[quote name='NumberXaero' timestamp='1311567738' post='4839845']
[code]
TiXmlNode* parent = ...;

TiXmlNode* child = parent->FirstChild(); // parent == properties
if(child && child->Type() == TiXmlNode::TEXT) {
const char* text = child->Value(); // text is the string version of what you want, convert it if need be
}

[/code]
[/quote]

Can you please clearly write how to get parent value? Sorry. I haven't done XML Parsing before.

Share this post


Link to post
Share on other sites
Two obvious things. element doesn't have any sibblings and the xml is malformed by closing a properties-tag with /errcode

Share this post


Link to post
Share on other sites
doc has a RootElement() call, which will be response, response->FirstChildElement("element"), element can then be looped.

[code]
TiXmlElement* props = element->FirstChildElement("properties");
while(props)
{
// props would be parent in my previous post

props = props->NextSiblingElement(); // ok as long as elements children are all <properties>
}
[/code]

and your xml has some errors which were pointed out.

Share this post


Link to post
Share on other sites
[quote name='NumberXaero' timestamp='1311570871' post='4839858']
doc has a RootElement() call, which will be response, response->FirstChildElement("element"), element can then be looped.

[code]
TiXmlElement* props = element->FirstChildElement("properties");
while(props)
{
// props would be parent in my previous post

props = props->NextSiblingElement(); // ok as long as elements children are all <properties>
}
[/code]

and your xml has some errors which were pointed out.
[/quote]

This code crashes at the following line at tinyxml.cpp:
[code]
for ( node = firstChild; node; node = node->next )[/code]

when

[code]TiXmlElement* props = element->FirstChildElement("properties");[/code]

is executed. What to do?

Share this post


Link to post
Share on other sites
Hello!

I haven't used TinyXML before but, I looked at your XML file and:

[code]
<?xml version="1.0" ?>
<response action="registration">
<element>
<properties name="username">cst11</properties>
<properties name="playerhandle">238768881697142018728993387950411141</properties>
<properties name="skinId">RCA001</properties>
<properties name="errcode" >loginnotAllowed</errcode> // Shouldn't this line terminate with the closing tag </properties> instead of </errcode> ?
</element>
</response>

[/code]
It's just a guess though.

Share this post


Link to post
Share on other sites
[quote name='ArthY303' timestamp='1311572254' post='4839864']
Hello!

I haven't used TinyXML before but, I looked at your XML file and:

[code]
<?xml version="1.0" ?>
<response action="registration">
<element>
<properties name="username">cst11</properties>
<properties name="playerhandle">238768881697142018728993387950411141</properties>
<properties name="skinId">RCA001</properties>
<properties name="errcode" >loginnotAllowed</errcode> // Shouldn't this line terminate with the closing tag </properties> instead of </errcode> ?
</element>
</response>

[/code]
It's just a guess though.
[/quote]

I've the correct one in my test environment. It's a mistake while posting. What else to do to get the inner Text?

Share this post


Link to post
Share on other sites
[quote name='ratha' timestamp='1311572880' post='4839865']
[quote name='ArthY303' timestamp='1311572254' post='4839864']
Hello!

I haven't used TinyXML before but, I looked at your XML file and:

[code]
<?xml version="1.0" ?>
<response action="registration">
<element>
<properties name="username">cst11</properties>
<properties name="playerhandle">238768881697142018728993387950411141</properties>
<properties name="skinId">RCA001</properties>
<properties name="errcode" >loginnotAllowed</errcode> // Shouldn't this line terminate with the closing tag </properties> instead of </errcode> ?
</element>
</response>

[/code]
It's just a guess though.
[/quote]

I've the correct one in my test environment. It's a mistake while posting. What else to do to get the inner Text?
[/quote]


Hi!
I've written the following code. It loops every element.

[code]
TiXmlElement* child = parent->FirstChildElement();
TiXmlElement* prop = child->FirstChildElement();

while(prop)
{
if(prop && prop->Attribute("errcode"))
{
errorcode = prop->GetText();
break;
}
else
prop = prop->NextSiblingElement();
}
}[/code]

It loops every element fine. After that I need to check for the username,playerhandle,skinId
and errcode and if it's errcode, I've to extract the "loginnotAllowed".
I don't know how to check this. I need some explanation or some code snippet
for the if block(what should be replaced with the existing to meet my requirements?).

Share this post


Link to post
Share on other sites
Something like this should do it:


[code]
TiXmlElement* child = parent->FirstChildElement();
TiXmlElement* prop = child->FirstChildElement();
const char *att = NULL;
std::string attribute;
while(prop)
{
att = prop->Attribute("name");
if(att == NULL)
{
// iterate and continue, or fail - no attribute called name
prop = prop->NextSiblingElement();
continue;
}
attribute = att;
if(attribute.compare("errcode") == 0)
{
// We have an errorcode, do something with it
}
else if(attribute.compare("username") == 0)
{
// We have a username...
}
else if(attribute.compare("playerhandle") == 0)
{
// We have a playerhandle...
}
else if(attribute.compare("skinId") == 0)
{
// We have a skinId...
}

prop = prop->NextSiblingElement();
}
[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this