-1

In my sample xml I've got a tag like this.

<ISAPPLICABLE>&#4; Applicable</ISAPPLICABLE>

While I was trying to create an XML in java, I tried to hardcode this.

public void addElement(Element parent, String elementName, String textContent){
  Element el = document.createElement("ISAPPLICABLE");
  el.setTextContent("&#4; Applicable");
  parent.appendChild(el);
}

But I get the result something like this.

<ISAPPLICABLE>&amp;#4; Applicable</ISAPPLICABLE>

How can I get the desired output in xml?

Pranjal Choladhara
  • 845
  • 2
  • 14
  • 35
  • You seem to have forgotten to mention _which XML/DOM libraries_ you're using, which is pretty important when it comes to verifying you're using the correct methods. Also I don't know you pasted that code in your post, but every code editor would have yelled that there's a `"` missing, so remember to [show your actual code](/help/how-to-ask), don't write some new code on the spot for your post. As for "what does it mean", it's an XML entity. The actual value is defined in the DTD that goes with that XML. – Mike 'Pomax' Kamermans Aug 13 '23 at 15:52
  • @Kayaman your edit changed the output that this person claims they're getting. It's fairly clear that they just typed out some new code for their post without actually ever running it and copy-pasting both code and output, but rather than fixing errors for them, they should be asked to show a [mcve]. – Mike 'Pomax' Kamermans Aug 13 '23 at 15:59
  • @Mike'Pomax'Kamermans: numeric entities like this `` do not need to be declared in a dtd. See for more info: https://en.wikipedia.org/wiki/Numeric_character_reference – Siebe Jongebloed Aug 13 '23 at 16:23
  • Could this answer be helpful: https://stackoverflow.com/a/29204979/3710053 ? – Siebe Jongebloed Aug 13 '23 at 16:30
  • means eot, see https://en.wikipedia.org/wiki/End-of-Transmission_character – Siebe Jongebloed Aug 13 '23 at 16:31
  • Yeah, I did some cowboy editing. Yeehaw! Giddy up horsies. – Kayaman Aug 13 '23 at 17:00
  • 2
    It's also worth pointing out that the numeric character reference `` is allowed in XML 1.1 but not in XML 1.0. Many XML parsers only support version 1.0. – Michael Kay Aug 13 '23 at 19:17
  • It is not only the numeric character reference representation that is disallowed in XML 1.0. That code point is illegal in XML 1.0 no matter how you try to express it; see https://stackoverflow.com/a/28152666/139985. (In XML 1.1, `` is allowed ... but there are other code points that are illegal. For example `` == ASCII `NUL` is not allowed in XML 1.1.) – Stephen C Aug 14 '23 at 00:48

1 Answers1

0
  • In XML you use &#4; to insert a character with code 4 into your text.
  • The same in Java inserts the characters &, #, 4 and ; into your string. When converting this to an XML string as the & is a reserved character it will be escaped as &amp;

If you want a character with code 4 in your string literal you have to escape it e.g. "\u0004 Applicable".

Karsten
  • 195
  • 5