[jdom-interest] Problems with "<" and ">" in element.addContent()

Manos Laliotis codemonkey at mail.com
Wed Jan 2 14:15:01 PST 2002


The solution I found for this problem, was to subclass XMLOutputter and
override the method responsible for the escaping. See the attached file.

I had a similar use for this when I needed to add text that contained
html elements (like <b>, <i>, ...) in random parts of the string.
Constructing an Element object for each such html tag wouldn't be
convenient (-it'd require parsing of that string from my side, to find
where those tags are) and adding the string in a CDATA element was not a
good choice for my specific case. The CDATA element would have been the
"correct" approach though -I guess...

The default JDOM behavior is the correct one, so you should only use
this subclass when you know that it will not introduce any problems when
applied to your data. In my case, there was a guarantee that only
well-formed html tags (-that always had a closing tag) would be 
encountered. But this is a guarantee you cannot easily get in general.

-M



Ann Björklund wrote:

>  Hi
> 
>  
> 
> I have a problem when I try to add content included the signs "<" and 
> ">" to an element.
> 
> I write:
> 
> /element.addContent("<HTML><HEAD></HEAD><BODY>");/
> 
> and then use a XMLOutputter to write it out to a file. In my file I got 
> the result
> 
> &lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;
> 
> I want to have "<" instead of "&lt;" and ">" instead of "&gt;".
> Does anyone know what I'm doing wrong?
> 
> Thanks a million
> Ann
> 
> 
>  
> 
>  
> 
> 
> ------------------------------------------------------------------------
> Send and receive Hotmail on your mobile device: Click Here 
> <http://go.msn.com/bql/hmtag2_etl_EN.asp>
> _______________________________________________ To control your 
> jdom-interest membership: 
> http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhost.com 
> 

-------------- next part --------------
import org.jdom.output.XMLOutputter;

public class NonEscapingOutputter extends XMLOutputter
{
    public NonEscapingOutputter() {
        super();
    }

    public NonEscapingOutputter(String indent) {
		super(indent);
    }

    public NonEscapingOutputter(String indent, boolean newlines) {
		super(indent, newlines);
    }

    public NonEscapingOutputter(String indent, boolean newlines, String encoding) {
		super(indent, newlines, encoding);
    }

    public NonEscapingOutputter(NonEscapingOutputter that) {
		super(that);
    }

	/**
	 * this method serves the whole purpose of this subclass -to prevent escaping.
	 * Use with care.
	 */
    protected String escapeElementEntities(String st) {
        return st;
    }
}


More information about the jdom-interest mailing list