1

I am re-wording my question because the 'parsed entity' thing has nothing to do with the problem at hand.

XML 1.1 versus 1.0

Is an xml 1.1 library is to escape illegal characters before serializing/deserializing them? Or is the library is to forbid them outright? Which is the correct way to set Text on an xml element?

if Element e = new Element("foo")

Should I do this:

e.setText(sanitized_text_illegal_characters_removed_or_escaped) ?

or

e.setText(any_text)

1
  • 1
    The XML 1.1 spec doesn't restrict the APIs of XML libraries, so the answer to your question could differ for different XML 1.1 libraries; the only way to find out is to look at the documentation for the library in question (or, failing that, test its behavior). Sep 9, 2012 at 3:25

2 Answers 2

1

A parsed entity is something you don't really need to worry about unless you're writing an XML parser. It's things like < and &. You can define your own in the document DTD, but it's a rarely used feature. An external parsed entity is one whose contents reside in another file or network resource or somewhere like that.

As to your main question:

Which is the correct way to set Text on an xml element?

if Element e = new Element("foo")

Should I do this:

e.setText(string_of_sanitized_data_with_illegal_characters_escaped) ?

or

e.setText(any_text)

You should set the text as you would like it to come out the other end, when the document is deserialized. This normally means you should not escape the data, and the XML library will do this for you.

e.g.:

  1. You insert the text "bed & breakfast".
  2. The XML library converts this to "bed &amp; breakfast" or "<![CDATA[bed & breakfast]]>" or some other representation, it doesn't really matter.
  3. You send the document somewhere else.
  4. The other parser reads the document and converts the text back.
  5. The end software retrieves the string "bed & breakfast".
5
  • Then what if I use xml 1.1. Are there characters I cannot use when I insert the text. Or is that also taken care of by the XML Library to make sure it all comes out corect atthe other end? Sep 7, 2012 at 15:47
  • There are some characters which are always invalid in an XML document, even when escaped. For example, the NULL character. If you want to store binary data in XML you should first convert it to some other textual format such as Base64, and decode from Base64 at the other end.
    – porges
    Sep 7, 2012 at 21:27
  • NULL and a host of others ... vertical tab, etc. [#x1-#x8] | [#xB-#xC] | [#xE-#x1F] | [#x7F-#x84] | [#x86-#x9F] Sep 7, 2012 at 21:36
  • Those are the restricted characters, not banned characters. In XML 1.1, restricted characters are allowed if they are escaped (and an XML library will do this automatically). The banned characters are those which do not match: [#x1-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF].
    – porges
    Sep 7, 2012 at 22:18
  • Thanks. That clarifies things. Sep 7, 2012 at 23:59
1

If you're writing XML programmatically, then you almost certainly don't want to use parsed entities.

There are two kinds of parsed entities: internal and external. An internal parsed entity is defined by a DTD declaration like this:

<!ENTITY me "Mike">

or

<!ENTITY me "<name>Mike</name>">

An external parsed entity is defined by a DTD declaration like this:

<!ENTITY me SYSTEM "me.xml">

Whether the entity is internal or external, it can be referenced by an entity reference like this:

&me;

which can appear within the content of an element or attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.