XML Parsing

I'm sure this has been asked before, but I can't find a thread similar to this one.

Say I have an XML file with:

1
2
3
<config name="Website">
    <address>http://www.example.com/main/</address>
</config>


I want the console to print Website: http://www.example.com/main/ , but how do I get the console to recognize the tags and output the text in between the tags?
Last edited on
closed account (Dy7SLyTq)
put it into a c-style string and tokenize it or look for sub strings. actually wait, i think i saw a xml-parsing library let me look
...steps away for a second...
http://code.google.com/p/ticpp/
Is there a simple way without using a library? I've updated the code in the OP.
You can certainly do it without using a library; but it's a lot more work than you might think. Things like ticpp and rapidxml are compliant to an XML standard, so it would be much more efficient to use them instead of writing your own parser, which, again, will take ages.

@Thumper

But what if I have a set template? This is my XML code in full:

1
2
3
4
5
6
7
8
9
10
11
12
<card type="MONSTER">
				<name>Exodius the Ultimate Forbidden Lord</name>
				<attr>DARK</attr>
				<lvl>10</lvl>
				<rar>SECRET</rar>
				<ed>1ST</ed>
				<set>CBAT-EN027</set>
				<type>Spellcaster/Effect</type>
				<desc>This card cannot be Normal Summoned or Set. This card cannot be Special Summoned except by returning all monsters from your Graveyard to your Deck. When this card declares an attack, send 1 monster from your hand or Deck to the Graveyard. This card gains 1000 ATK for each Normal Monster in your Graveyard. When this card is removed from the field, remove it from play. If there are 5 different "Forbidden One" cards in your Graveyard that were sent there by this card's effect, you win the Duel.</desc>
				<atk>?</atk>
				<def>0</def>
			</card> 


Every <card> tag is like this, so would it still take a long time to make my own even though the set tags are constant?
Maybe, maybe not. I remember trying to implement something like this in my early years of programming and it ended up being a disaster. There's a lot of textual scenarios to cover in the programming. It would by far be easier to use something like rapidxml.
But if you're set on writing your own code to do it, by all means, it would be a worthy exercise.

The basic idea is, you want to go through the text line by line. Split it into key parts (sometimes called tokenizing), and evaluate those parts. Look at the std::string reference. There are some neat member functions like find and substr that will be really helpful to you.
Well since it seems so hard to parse XML, what would be a better language to store this data in (and be able to output it in the console?):

1
2
3
4
5
6
7
8
9
10
11
Monster
Exodius the Ultimate forbidden Lord
DARK
10
SECRET
1ST
CBAT-EN027
Spellcaster/Effect
This card cannot be Normal Summoned or Set. This card cannot be Special Summoned except by returning all monsters from your Graveyard to your Deck. When this card declares an attack, send 1 monster from your hand or Deck to the Graveyard. This card gains 1000 ATK for each Normal Monster in your Graveyard. When this card is removed from the field, remove it from play. If there are 5 different "Forbidden One" cards in your Graveyard that were sent there by this card's effect, you win the Duel.</desc>
?
0 


Would it be logical to store them in a text file in the same format for each, then use a terminating character at the end of each section?
Topic archived. No new replies allowed.