• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 354
  • Last Modified:

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!
0
Inward_Spiral
Asked:
Inward_Spiral
  • 2
  • 2
1 Solution
 
HonorGodCommented:
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
 
Inward_SpiralAuthor Commented:
Thanks Bill, that was exactly what I needed.
0
 
wnrossCommented:
No problem, thanks for the points

Cheers,
-Bill
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now