I had same problem some month ago. I both asked for it in meta and sent a mail to moderators.
The moderator answer was that the correct way was indeed to ask them for the answer text ... but it came several days after a 10k+ copied my deleted answer in meta.
So here is your answer :
Almost any content can be used as text of an element. Any XML library should provide a way to set the text of the element and by doing that, the library will escape all of the five special characters (>
, <
, &
, "
and '
). There is no need for CDATA.
In your case, the library would escape the text to
<reference>A;&94-230</reference>
which is perfectly fine XML. If the destination of the XML also uses a XML library, the library will convert the text back to
A;&94-230
There's not need to perform a validity before setting the text.
Edit
Setting the text
string_contains_CDATA-section-close_delimiter ]]>
on a tagelement
will result in
<tag>string_contains_CDATA-section-close_delimiter ]]></tag>
which is again valid XML.
Edit 2
Find this unit test which uses JDom
@Test
public void XMLSpecialCharacterTest() throws Exception
{
try
{
// Set up everything
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder dombuilder= factory.newDocumentBuilder();
InputStream is = new ByteArrayInputStream( "<reference/>".getBytes("UTF-8"));
org.w3c.dom.Document w3cDocument = dombuilder.parse(is);
DOMBuilder jdomBuilder = new DOMBuilder();
Document jdomDocument = jdomBuilder.build(w3cDocument);
Element rootElement = jdomDocument.getRootElement();
// Do the actual tests
rootElement.setText("A;&94-230");
System.out.println(new XMLOutputter().outputString(jdomDocument));
rootElement.setText("Some text with CDATA ]]>");
System.out.println(new XMLOutputter().outputString(jdomDocument));
} catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}