XML Parsing using Tiny XML

Started by
10 comments, last by Nanoha 12 years, 9 months ago
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?
Advertisement
What have you tried?
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?

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
}



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.
Two obvious things. element doesn't have any sibblings and the xml is malformed by closing a properties-tag with /errcode
f@dzhttp://festini.device-zero.de
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.

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

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?

This topic is closed to new replies.

Advertisement