replaceAll Use

V

vunet.us

How can I use replaceAll function to remove XML declaration line:
<?xml version="1.0" encoding="US-ASCII"?>

This did not work:

String newXML = myXML.replaceAll("<?xml version=\"1.0\"
encoding=\"US-ASCII\"?>","")
It has something to do with regular expressions.... I guess.

PS: Yes, I do need to remove that XML declaration.
 
S

sara

How can I use replaceAll function to remove XML declaration line:
<?xml version="1.0" encoding="US-ASCII"?>

This did not work:

String newXML = myXML.replaceAll("<?xml version=\"1.0\"
encoding=\"US-ASCII\"?>","")
It has something to do with regular expressions.... I guess.

PS: Yes, I do need to remove that XML declaration.

You should use \? instead of ? in the regular expression
 
H

Hemal Pandya

sara said:
You should use \? instead of ? in the regular expression

Or use literal pattern:

Pattern p = Pattern.compiler(xmlDeclarationLine, Pattern.LITERAL);
p.matcher(xmlString).replaceAll("");
 
D

Daniel Pitts

\? shows error invalid escape character
You need to escape the \ for the Java compiler, so that your string
contains \?
"\\?"

However. Why are you using replace all for something that is only
supposed occure on the first line?
Why not:

if (myXml.startsWith("<?xml")) {
myXml.substring(myXml.indexOf("\n") + 1);
}
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

How can I use replaceAll function to remove XML declaration line:
<?xml version="1.0" encoding="US-ASCII"?>

This did not work:

String newXML = myXML.replaceAll("<?xml version=\"1.0\"
encoding=\"US-ASCII\"?>","")
It has something to do with regular expressions.... I guess.

I think so too.

If you are on Java 1.5 or newer then use:

String newXML = myXML.replace("<?xml version=\"1.0\"
encoding=\"US-ASCII\"?>","")

It does not use regex.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top