Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ XML
Okay, so I have the following code that I have modeled from online resources:
 
std::vector<std::string> VN::XDocument::getNode()
	 {
		 std::string xmld = document;
		rapidxml::xml_document<> doc;
 
		std::vector<char> xml_c(xmld.begin(), xmld.end());
		xml_c.push_back('\0');
		doc.parse<rapidxml::parse_declaration_node | rapidxml::parse_no_data_nodes>(&xml_c[0]);
 
		 std::vector<std::string> v;
		 rapidxml::xml_node<>* rn = doc.first_node();
 
		 v.push_back(rn->first_attribute()->value());
		 return v;
	 }
 
Please note that "document" is a variable storing an online XML file (I didn't post the actual URL for privacy reasons...) but anyway, I call the above code from the following:
 
VN::XDocument x(xmlStr);
std::vector<std::string> defs = x.getNode();
std::cout << defs[0];
 
It's a vector because I eventually plan to read off more of the XML file.. but for now I'm just calling the first node just to make sure it works. However, every time I debug I get the following error:
 
Microsoft C++ exception: rapidxml::parse_error at memory location 0x0018f180..
 
Here's the XML file:
<entry_list version="1.0">
<entry id="hello">
<ew>hello</ew>
<hw>hel*lo</hw>
<sound>
<wav>hello001.wav</wav>
<wpr>hu-!lO</wpr>
</sound>
<pr>hə-ˈlō, he-</pr>
<fl>noun</fl>
<in>
<il>plural</il>
<if>hellos</if>
</in>
<et>
alteration of
<it>hollo</it>
</et>
<def>
<date>1877</date>
<dt>
:an expression or gesture of greeting
<un>
used interjectionally in greeting, in answering the telephone, or to express surprise
</un>
</dt>
</def>
</entry>
<entry id="hullo">
<ew>hullo</ew>
<hw>hul*lo</hw>
<sound>
<wav>hullo001.wav</wav>
<wpr>(+)hu-!lO</wpr>
</sound>
<pr>(ˌ)hə-ˈlō</pr>
<cx>
<cl>chiefly British variant of</cl>
<ct>hello</ct>
</cx>
</entry>
</entry_list>
Anyone else ever get this? I don't know what to do! I tried everything! Please help. Thanks in advanced.
 
BTW, I'm a C++ newbie.
Posted 12-Mar-13 6:23am
Edited 12-Mar-13 7:02am
v2
Comments
Sergey Alexandrovich Kryukov at 12-Mar-13 11:24am
   
What's on input when you get this exception?
How do you know that XML is well-formed and valid?
—SA
RadXPictures at 12-Mar-13 11:55am
   
I would imagine the XML is valid because I originally had parsed it using Linq XDocument in C# and it worked fine. But for cross-platform purposes I switched to C++... Nothing outputs when the exception is thrown. It immediately returns to Visual Studio, opens up rapidxml.cpp (line 1409) and shows a message box displaying the error. I then have to manually stop the debugger and return to the coding window.
Sergey Alexandrovich Kryukov at 12-Mar-13 13:31pm
   
I would double-check. It sounds weird. Anyway, I cannot tell anything until I see some minimalistic XML sample which fails to parse.
 
For you information, C# is also cross-platform, with stricter level of standardization through ECMA and ISO standards for CLR, C# and C++/CLI. .NET applications written in a compatible way (and XML support is of course compatible part of FCL) actually work on a good number of systems without recompilation.
 
And there is a good number of other C++ XML parsers...
 
—SA
RadXPictures at 12-Mar-13 11:59am
   
Oh... and also, the debugger seems to say something about m_name and m_value in the xml_node being bad pointers (bad ptr)
Sergey Alexandrovich Kryukov at 12-Mar-13 13:32pm
   
Please, show the relevant XML sample causing the problem, try to minimize it as much as you can.
—SA
RadXPictures at 14-Mar-13 11:58am
   
I just posted it..
Sergey Alexandrovich Kryukov at 14-Mar-13 12:28pm
   
As I can see, your XML uses Unicode, so you need to add the prolog:
<?xml version="1.0" encoding="utf-8"?>
 
I hope that even without prolog, you initialize you XML reader (or whatever it is) to read with proper charset, so it should not cause this problem.
Other than that, XML is correct, thank you for showing it...
 
—SA
RadXPictures at 14-Mar-13 14:00pm
   
I'm sorry, but I'm fairly new to C++, but how exactly would I implement the prolog? What is a prolog?
Sergey Alexandrovich Kryukov at 14-Mar-13 14:49pm
   
Oh, I'm sorry, I missed the formatting in my comment above, so it was invisible to you. This is the line shown in red, above. Please see now.
Look, you need to take at least a quick look at XML standard, to get some basic ideas... and terminology...
—SA
Sergey Alexandrovich Kryukov at 14-Mar-13 14:52pm
   
I'm afraid this fix won't help you, but it's better to observe the encoding. Who knows...
 
After all, I don't know how good is the RapidXML parser, but it's open source, so you can dig out where the problem is, using the full source code and the debugger...
—SA
RadXPictures at 16-Mar-13 22:19pm
   
I think I know what the problem is... because my XML file is from an online source, it isn't reading it correctly, therefore never parsing, therefore throwing errors... I think I'm gonna try a more web-friendly language, like Python, simply because I can. Thanks for the help anyway though.
Sergey Alexandrovich Kryukov at 16-Mar-13 23:13pm
   
I'm not sure. It shouldn't make any difference...
A language cannot be Web-friendly or not.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 295
2 Afzaal Ahmad Zeeshan 224
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 160
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 12 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100