Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to read an XML file using c++ in eclipse. I am using 'rapidXML' library to achieve this. But once I try to run the program, it terminates saying,
terminate called after throwing an instance of 'rapidxml::parse_error'<br />
  what():  expected ' or "


The code:
C++
xml_document<> doc;
xml_node<> * rootNode;

//read the xml file into vector
ifstream xmlFile("Exchange.xml");
xmlFile.unsetf(ios::skipws);
vector<char> buffer ((istreambuf_iterator<char>(xmlFile)), istreambuf_iterator<char>());
buffer.push_back('\0');

//Parse the buffer
doc.parse<0>(&buffer[0]);

//getting the root node
rootNode = doc.first_node("Exchange");

//Iterating through the file
xml_node<> * sourceId;

for(sourceId = rootNode->first_node("sourceId"); sourceId; sourceId = sourceId->next_sibling()){
    cout << sourceId->value() << " : " << sourceId->first_attribute("interval")->value() << endl;
}


The XML file:
HTML
<pre><?xml version="1.0" encoding="UTF-8"?>
<Exchange>
	<sourceId interval = 03>ADSM<sourceId/>
	<sourceId interval = 06>ASE<sourceId/>
	<sourceId interval = 09>BSE<sourceId/>
	<sourceId interval = 12>CSAE<sourceId/>
	<sourceId interval = 15>CSE<sourceId/>
	<sourceId interval = 18>TDWL<sourceId/>
</Exchange>


NOTE: The above code is in a single method which calls in the main method. I just want to print the attribute value and the child value of the nodes. How can I solve this? Thank you!


What I have tried:

I tried using file<> method in rapidxml_util

C++
//read the xml file into vector
file<> xmlFile("Exchange.xml");


//Parse the buffer

doc.parse<0>(xmlFile.data());
Posted
Updated 26-Jan-20 22:26pm

No surprise, actually. Your XML is invalid.
Quote:
<Exchange>
<sourceId interval = 03>ADSM<sourceId/>
...

There should be quotes around the attribute names.
 
Share this answer
 
Comments
Manujaya Premathilaka 27-Jan-20 4:22am    
Oh thank you! It solves that error. Still, I couldn't achieve my target. Now it says 'what(): unexpected end of data'. How can I overcome this? Thanks again!
Your / should be in front of the end tag.
<Exchange>
	<sourceId interval="03">ADSM</sourceId>
 
Share this answer
 
Comments
Manujaya Premathilaka 27-Jan-20 4:31am    
thank you thank you soo much! now it works fine!
CPallini 27-Jan-20 6:26am    
5.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900