Having an XML node object (the parent one), how can I retrieve the text value of one of the children nodes in C++?
Asked
Active
Viewed 1,039 times
-3
-
3_"Having an XML node object"_ Are you using an XML library? Which one? – mvidelgauz Aug 18 '16 at 14:10
-
Indeed, it's a private Library. But I can use boost – Dave ddd Aug 18 '16 at 14:14
-
1Could you please provide a [mcve]? – Khalil Khalaf Aug 18 '16 at 14:14
-
_"Indeed, it's a private Library"_ So you have looked at that library API (or at least public headers) and didn't find any ways to enumerate XML node's children? May be it is time to switch to another well known XML library? There are plenty of them – mvidelgauz Aug 18 '16 at 14:17
-
@Daveddd: If you are using Boost then give a try of [Property Tree](http://www.boost.org/doc/libs/1_61_0/doc/html/property_tree.html). – karastojko Aug 18 '16 at 14:36
-
2***it's a private Library*** How can we possibly help when we don't have the library you use?? – drescherjm Aug 18 '16 at 14:43
1 Answers
1
First use the method on your parent that gives you the child node you want.
Then use the method on that object that lets you access its text.
If you happen to use RapidXML, which I like to recommend, there's an easy parsing example found here.
The essential part is:
root_node = doc.first_node("MyBeerJournal.xml");
reads an XML file called MyBeerJournal.xml
for (xml_node<> * brewery_node = root_node->first_node("Brewery"); brewery_node; brewery_node = brewery_node->next_sibling())
{
...
}
lets you iterate over the nodes, starting with the one called Brewery
.
auto beerName = brewery_node->first_attribute("name")->value();
finally lets you access the text value of the desired attribute, in this case name

Community
- 1
- 1

Jan Hohenheim
- 3,552
- 2
- 17
- 42
-
-
You last edit makes this "link only" answer, which is not welcomed here at SO, you should provide essential code snippet – mvidelgauz Aug 18 '16 at 14:23
-
-
No, I suggest you to wait until we know what library is actually used, otherwise you will "shoot at the dark" – mvidelgauz Aug 18 '16 at 14:29
-
@mvidelgauz the OPs comment `Indeed, it's a private Library. But I can use boost` makes me believe he's unsure on which library to use – Jan Hohenheim Aug 18 '16 at 14:35
-
IMHO, you are wasting your time guessing. But that's your call of course – mvidelgauz Aug 18 '16 at 14:36
-
@mvidelgauz eh, you're probably right. Guess the possibility of juicy rep blinded me – Jan Hohenheim Aug 18 '16 at 14:40
-
1