InterviewSolution
| 1. |
Why Does Set Text (" ;") Not Do What I Want? |
|
Answer» When you pass a STRING into a method like set Text () JDOM assumes it's just that, a string, not a fragment of XML. For example, when you call: element.setText ("&#160 ;") JDOM assumes you want to set the content to the string containing the six characters & # 1 6 0. It does not parse it to attempt to understand it as XML first. Thus when you output the text using XMLOutputter it will naturally escape the special ampersand character and output & #160. The SOLUTION is to pass regular Unicode characters to the set Text () method or, if you have text DATA that you want to be interpreted as XML, pass it through an XML parser before it GOES into JDOM. This is what the SAX Builder and DOM Builder classes do. When you pass a string into a method like set Text () JDOM assumes it's just that, a string, not a fragment of XML. For example, when you call: element.setText ("  ;") JDOM assumes you want to set the content to the string containing the six characters & # 1 6 0. It does not parse it to attempt to understand it as XML first. Thus when you output the text using XMLOutputter it will naturally escape the special ampersand character and output & #160. The solution is to pass regular Unicode characters to the set Text () method or, if you have text data that you want to be interpreted as XML, pass it through an XML parser before it goes into JDOM. This is what the SAX Builder and DOM Builder classes do. |
|