Re: Use ampersand character in setText

Rolf Lear <[email protected]>
Newsgroups gmane.comp.java.jdom.general
Message-ID <[email protected]>
Hi Andrea

As others have said, JDOM ensures that the output from the XMLOutputter 
is valid XML. The raw string '&test' is not valid XML.

You do have options. If you really want the raw string, then perhaps you 
want to enclose it in a CDATA section.

CDATA cdata = new CDATA("&test");
Element element = new Element("root");
element.addContent(cdata);
xmloutputter.output(stream,element);

<root><[CDATA[&test]]></root>

That may give you what you want, and it will also be valid XML.

If you want to skip the CDATA section then you can probably put a 
filtering system between JDOM's output stream and the final destination 
stream. That 'filter' can search/replace specific content with invalid 
XML content....

Otherwise you can create your own custom XMLOutputter instance that 
'breaks' the validation process for Text content.

Rolf

On 16/12/2011 6:58 AM, Andrea Pacini wrote:
>
>     We have necessity to write, in the final XML, the string &test and
>     not the string &amp;test
>     How can we do ?
>
>     2011/12/16 Grzegorz <[email protected]
>     <mailto:[email protected]>>
>
>             according to Jdom FAQ (
>             http://www.jdom.org/docs/faq.html#a0290 ) , I tried to
>             write the string
>
>             &test
>
>             using this statement:
>
>             root.setText("\u0026test");
>
>             where \u0026 is the Unicode code for ampersand character.
>
>             The output is:
>
>             &amp;test
>
>             Where is the error ?
>
>
>         There is no error. What is wrong? It is a special character in
>         XML and as such it must be escaped.
>
>         Regards,
>         Grzegorz
>
>
>

_______________________________________________
To control your jdom-interest membership:
http://www.jdom.org/mailman/options/jdom-interest/[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.