in XML. What does it mean? How can I write it in XML?

77 Views Asked by At

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?

1

There are 1 best solutions below

0
Karsten On
  • 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".