I’m getting a strange error with rapidxml when parsing a xml file like
<?xml version="1.0" encoding="UTF-8"?>
<IMG align="left"
src="http://www.w3.org/Icons/WWW/w3c_home" />
It throws “expected >”.
Im using a code like the following to parse the data
std::fstream file("./test.xml");
std::istream_iterator<char> eos;
std::istream_iterator<char> iit (file);
std::vector<char> xml(iit, eos);
xml.push_back('\0');
xml_document<> doc;
doc.parse<0>(&xml[0]);
the “/” symbol in the IMG rag seems t be the problem. Is this a rapidxml bug or am I doing something wrong?
The way you load the XML data into vector is wrong. In C++ text mode streams have “skipws” flag set by default, which causes them to skip all whitespace in the input. You can verify this by examining the contents of your vector – it will have all spaces/endlines missing. This obviously causes the parser to complain.
Unset skipws flag on the stream to get the correct behaviour:
Alternatively, you can use file class from rapidxml_utils.hpp to load the file:
Sadly, loading text files with C++ streams is very tricky and full of traps.
As for sehe tests above, the “incorrectly accepted” cases are by design (I don’t have enough reputation to add comments to his answer). You need to use “parse_validate_closing_tags” parse flag to make the parser check whether end tag name matches starting tag name:
See parse_validate_closing_tags in rapidxml manual.
The rationale for this behaviour is performance – verifying end tags is time consuming and in most cases not needed.