Blankman
Blankman

Reputation: 267140

Trying to compare String values, but seems to be converting text

My xml has text inside an element that looks like:

<node>#&lt;abc123&gt;</node>

So in my junit test I am doing an assertEqual it fails because the value I get from the xml looks like:

<#abc123>

Does calling toString on a Object decode it? Or going from a StringBuffer to a String?

Upvotes: 1

Views: 102

Answers (4)

Perception
Perception

Reputation: 80623

If you are doing an assert on the raw XMl string #&lt;abc123&gt; it is going to fail, because the following sequences are XML specific encodings that get translated away by your parser:

  • &lt ; - this is an encoding for the '<' character
  • &gt ; - this is an encoding for the '>' character

The reason for the encodings in the first place is pretty obvious - the '<' and '>' characters have special meaning in XML, so to include them literally in your document would cause problems for the parser.

What you need to do is assert on the String that would be outputted by your XML parser, which is '<#abc123>'.

Upvotes: 4

light_303
light_303

Reputation: 2111

my guess would be that your xml framework unescapes the text in the moment you call getText() on the node. Maybe you have access the node content in a different way to get the raw content.

Upvotes: 0

helpermethod
helpermethod

Reputation: 62234

This depends on the implementation of the toString method. toString gives a string representation of the object, in your case it converts &lt; to <.

Upvotes: 0

mack
mack

Reputation: 1828

this might help you,

look for escapeHtml in

StringEscapeUtils

http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringEscapeUtils.html

Upvotes: 0

Related Questions