Jump to content
  • Advertisement
Sign in to follow this  
Affected1

Multiple items in XML

This topic is 1235 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

I'm making a small project as a learning experience with XML parsing.

 

I have the following XML file:

<data>
	<item>
		<type>User</type>
		<name>TestUser1</name>
		<permission>Admin</permission>
	</item>
	
	<item>
		<type>User</type>
		<name>TestUser2</name>
		<permission>Normal</permission>
	</item>
</data>

and the following code to parse the XML:

XMLDocument doc;
doc.LoadFile("data.xml");
XMLElement* dataNode = doc.FirstChildElement("data");
XMLElement* itemNode = dataNode->FirstChildElement("item");

const char* type = itemNode->FirstChildElement("type")->GetText();
const char* name = itemNode->FirstChildElement("name")->GetText();
const char* permission = itemNode->FirstChildElement("permission")->GetText();

printf("--- ITEM ---");
printf("Type: %s\n", type);
printf("Name: %s\n", name);
printf("Permissions: %s\n", permission);

This works well for one item but how could i iterate through the whole XML and print out every item and their properties?

I'm fairly new to XML so this might be something very simple that i just overlooked.

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Once you get a child element with FirstChildElement() there should be something like a Next() or NextChildElement() call you can make to retrieve the subsequent sibling node from the document.

Share this post


Link to post
Share on other sites

Using NextSiblingElement() did the trick, thanks smile.png .

Is there a way to count how many items there are in the main <data> node so i could use an for loop to print all of them?

Or do i need to make another node for that like <itemcount> and then update that everytime i add a new item to the XML?

Share this post


Link to post
Share on other sites


Is there a way to count how many items there are in the main node so i could use an for loop to print all of them?

 

Unless you specifically need the count beforhands, a while-loop should do:

XMLElement* dataNode = doc.FirstChildElement("data");

while(dataNode)
{
    dataNode = doc.NextSiblingElement(dataNode);
}

I don't know the specific syntax & how tinyxml works, but I assume NextSiblingElement will return nullptr once there is are no more elements.

 

Doesn't tinyxml have a documentation, on another note? Those questions are not something that really require external assistance, you should be able to extract this information from the libaries documentation and/or samples themselves (which would be a good exercise regardless).

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!