Jump to content
  • Advertisement
Sign in to follow this  
ratha

XML Parsing using Tiny XML

This topic is 2553 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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


<?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>


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
Advertisement
I wrote the following 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();
}
}
}


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

Share this post


Link to post
Share on other sites

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
}

Share this post


Link to post
Share on other sites


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
}




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.


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>
}


and your xml has some errors which were pointed out.

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.


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>
}


and your xml has some errors which were pointed out.


This code crashes at the following line at tinyxml.cpp:

for ( node = firstChild; node; node = node->next )


when

TiXmlElement* props = element->FirstChildElement("properties");

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:


<?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>


It's just a guess though.

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:


<?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>


It's just a guess though.


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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!