[jdom-interest] & problems

Dirk Schumacher greyshine at gmx.net
Wed Sep 7 02:29:59 PDT 2005


Hello,

well I was scanning through the archives but I was not successful
finding what I need.

I have Element with text which need to have a pure '&' outputted.
Well, we know, '&' will automatically converted to the 5 letters:
"&".

This is not always what I want. And I does not make sense to me.
Why is this problem so often ignored in so many articles/tutorials?

Just imagine. Having user input into a textarea which targets to be
displayed on a html page.

I am having a xml skeleton to put the input between the body tag.
The program automatically converts i.e. " to "
adding it then to an Element.

Ok, whenever jdom approches an '&' it will change it to &
for sure.

What is the workaround for having Text(parts) not to be changed, i.e.:
p.setText(" little indented text.");
outputting
" little indented text."

Which classes in jdom are responsible for changing this String's
'&'s to "&"s. I tried to overwrite the XmlOutputter but the
methods seem to be never called except in the main by program:
outputter.output((Element)e, (OutputStream)System.out);

Is probably possible to put it through xslt to resolve entities.
I would not want to do that. It would be lots of overhead and runtime
cost. Which is actually not needed for all other data than the &.

If you have further questions just mail I appreciate all hints I can
get.

The code:
--------------------------------------------------------------
import java.io.IOException;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class Joint6TestMain {

    public static void main(String[] args) throws IOException {

        Element e = new Element("body");
        e.addContent("When I was young I loved to watch Simon & Simon");
        e.addContent(new Element("br"));
        e.addContent("seems to work correct!");
        Element p = new Element("p");
        e.addContent(p);
        p.setText(" little indented text.");

        e.addContent("makes me crazy!");
        e.addContent(new Element("br"));
        e.addContent("\u0026 is ok - & bothers me");

        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        outputter.output(e, System.out);
        }
}
--------------------------------------------------------------
The Output:
--------------------------------------------------------------
<body>
  When I was young I loved to watch Simon &amp; Simon
  <br />
  seems to work correct!
  <p>&amp;nbsp;little indented text.</p>
  makes me crazy!
  <br />
  &amp; is ok - &amp;#38; bothers me
</body>
--------------------------------------------------------------

What I want:
--------------------------------------------------------------
<body>
  When I was young I loved to watch Simon &amp; Simon
  <br />
  seems to work correct!
  <p>&nbsp;little indented text.</p>
  makes me crazy!
  <br />
  &amp; is ok - & bothers me
</body>




More information about the jdom-interest mailing list