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!
Inward_SpiralAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HonorGodSoftware EngineerCommented:
In what language?

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

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

Java:
  str = new String( str.replaceAll( "\s+", '' );
0
Inward_SpiralAuthor Commented:
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.
0
wnrossCommented:
Try

re = /\s+/g;
data = data.replace(re,"");

This is just a first pass, a better regex would be
re = />\s+</g;
data = x.replace(re,"><");

So that only whitespace between tags will be caught

Cheers,
-Bill
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Inward_SpiralAuthor Commented:
Thanks Bill, that was exactly what I needed.
0
wnrossCommented:
No problem, thanks for the points

Cheers,
-Bill
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.