The87Boy
The87Boy

Reputation: 887

Parse XML ampersand in Java

I download an XML-file, I generate using PHP, that looks similar to this

<?xml version="1.0" encoding="utf-8" ?> 
<customersXML> 
   ...
   <customer id="12" name="Me+%26+My+Brother" swid="1" /> 
   ...
</customersXML> 

Now I need to parse it in Java, but before that I use URL-Decode, so the XML become this

<?xml version="1.0" encoding="utf-8" ?> 
<customersXML> 
   ...
   <customer id="12" name="Me & My Brother" swid="1" /> 
   ...
</customersXML> 

But when I parse the XML-file using SAX, I get a problem with "&". How can I get around this?

Upvotes: 1

Views: 2345

Answers (4)

StaxMan
StaxMan

Reputation: 116512

You should FIRST parse, THEN url decode.

Upvotes: 0

Michael Kay
Michael Kay

Reputation: 163322

Never process XML as a string without parsing it, or you are liable to end up with something that is no longer XML. As you have discovered.

Upvotes: 2

RHSeeger
RHSeeger

Reputation: 16262

If the XML in question isn't urlencoded in the first place (which it doesn't look like it is), then you shouldn't be urldecoding it. Breaking the xml and then "unbreaking" it really doesn't seem like the best way to go about it. Just use the original xml and parse that.

Upvotes: 2

Femaref
Femaref

Reputation: 61437

The ampersand is a special character in xml (O'reilly Xml: Entities: Handling Special Content) and needs to be encoded. Replace it with &amp; before sending it.

Upvotes: 6

Related Questions