2

I'm reading XML data using Java and DOM. When I print a variable to the console I notice it prints on two different lines.

Output:

Hello How are
you today?

When I go to the attribute I'm trying to print of the underlying XML document, I notice the following:

<element attribute = "Hello How are&#xD;&#xA;you today?"></element>

How do I remove the characters &#xD;&#xA; from the attribute value in Java?

If the data from the attribute is stored in a Java String variable called myVar, I tried the following unsuccessfully:

if(myVar.contains("&#xD;&#xA;")){

    myVar = myVar.replaceAll("&#xD;&#xA;", " ");

}
1
  • Try replacing \r and \n. Commented Sep 15, 2015 at 17:33

2 Answers 2

2

&#xD;&#xA; this is a line break embedded in XML, which is probably converted into characters 0xD 0xA (13 10) in java. So for the pattern, either use "\n\r", or use "\s+" -> " "

Sign up to request clarification or add additional context in comments.

2 Comments

If he's using replaceAll, he should use "\\s+" since it takes a regular expression as its first argument. Using "s+" will just replace instances of 1 or more s characters.
Should probably be "\r\n" instead of "\n\r".
1

replaceAll("\\s+", " ") worked but so did replaceAll("\r\n", " "). On the other hand, "\n\r" as first argument to replaceAll did not work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.