JXITC
JXITC

Reputation: 1178

tinyXML lib cannot read ‘&’ properly

I have an XML file with some lines like the following:

<rule pat="&&&&&&&&&&&&&&&(?<B>B) ?(?<AND>&) ?(?<E>E)">

I use TinyXML lib in C++ to parse this XML file, but when I try to get the 'pat' attribute of such lines, the TinyXML turns out just ignore any occurrence of character &. That is, the result read by TinyXML turns to be like:

(?<B>B) ?(?<AND>) ?(?<E>E)

with all & missing!

This char is a part of my regular expression pattern, so this will lead to further error in my program.

Do anyone have any idea why this character & is so SPECIAL and TinyXML just cannot read? even a stand alone & will be dismissed?

Upvotes: 0

Views: 688

Answers (3)

dkamins
dkamins

Reputation: 21948

That's not well formed XML. If you want an & character, you need to put &amp;.

Upvotes: 2

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143319

In xml, & is represented as &amp;

Upvotes: 1

Nicol Bolas
Nicol Bolas

Reputation: 474476

That's because that is not a valid XML file. You can't just stick an & character anywhere in XML. You have to escape it with entities:

&amp;

TinyXML will only read valid XML files (or at least mostly valid ones).

Similarly, you need to escape the < and > characters too, with &lt; and &gt;.

Upvotes: 7

Related Questions