Link to home
Start Free TrialLog in
Avatar of Inward_Spiral
Inward_Spiral

asked on

Can I use regex to strip out xml whitespace?

Hello all,

I'm doing a lot of work with XML, but there are some cases where it would be faster to just deal with the XML data as a string.
Changing it to a string isn't a problem, but there's some whitespace that gets added in between the nodes that's throwing me off.

Can I structure a regular expression that can strip out the whitespace in between the nodes of the string?

I've done simple expressions with 'g' for replace-all commands, but nothing quite like this.

Any assistance would definitely be appreciated.

Thanks!
Avatar of HonorGod
HonorGod
Flag of United States of America image

In what language?

Perl:
  $data =~ s/\s+//g;

Python:
  data = data.replace( ' ', '');

Java:
  str = new String( str.replaceAll( "\s+", '' );
Avatar of Inward_Spiral
Inward_Spiral

ASKER

JavaScript, actually.

I'm pulling in some XML text from the server via AJAX, which sometimes has whitespace (spaces, carriage returns, etc.), and can come in like this sometimes:
<RootNode>
  <ParentNode>
    <ChildNode>Text</ChildNode>
  </ParentNode>
</RootNode>

I'd like to strip the string of all spacing between the "><" brackets:
<RootNode><ParentNode><ChildNode>Text</ChildNode></ParentNode></RootNode>

Does that help?
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of wnross
wnross

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Bill, that was exactly what I needed.
No problem, thanks for the points

Cheers,
-Bill