How can I write ampersand character (&)- addTextElement


#1

Hi All,
I am using JUCE 1.39. My requirement is to create an xml file which will contains supported characters in my program. Xml elements Start and End will tell the characters region. So for start element I need to give decimal value for Space(value is “& # 32;”) and end element I need to give decimal value for d(value is “& #100;”).
I have return the following code to achieve this,
code:-

juce::XmlElement *pStartElement = new juce::XmlElement(T(“Start”));
pStartElement->addTextElement(juce::String(" "));

juce::XmlElement *pEndElement = new juce::XmlElement(T(“End”));
pEndElement->addTextElement(juce::String(“d”));

but in output i am getting ‘&’ instead of ‘&’.

output:-
 
d

Could you please tell me How can I give decimal value(for space and character d) for xmlElement? How can I write only ‘&’ instead of ‘&’.

Thanks in advance.

regards
Moka.


#2

the & character is not allowed as such in an XML file. It must be replaced by “&” (the same is true for HTML files)
see for instance http://cnx.org/content/m9002/latest/

So the fact that “&” is replaced by “&” is a good thing.
Normally, when you’ll read back the XML file you made with an XML parser, the “&” will be converted back to simply &, so everything will be ok.

But I am little curious. Why do you insist to prefix your numbers by “&#” ? This is not the preferred way to enter number in XML.


#3

Ok, well first of all, I’d say that you really shouldn’t be putting a character into the text section of an xml element, because the text section isn’t guaranteed to actually return the exact same string that went in (e.g because excess spaces might be removed, new-lines might be added).

If you have a string where you need it to come out in exactly the same form that it went in, put it in an attribute.

But TBH if it was me, I’d just store it as an integer attribute, not a character at all. That way it’s foolproof.

But I’m a little concerned that you say you got that output from the code that you posted here - is that correct? It looks like something has added escape characters twice, but I can’t see how my xml code would make a mistake like that.