Sidebar

How to remove whitespace from XML?

0 votes
1.9K views
asked Oct 16, 2015 by rich-c-2789 (16,180 points)

I have some xml like the following:

<ClinicalDocument>
   <recordTarget>
      <patientRole>
         <id root="123" />
         <id root="456" />
         <addr>   999 Hollow Drive   </addr>
      </patientRole>
   </recordTarget>
</ClinicalDocument>

I need to remove one of the ids.  I do that with this line of code:

message.removeAllNodes("/ClinicalDocument/recordTarget/patientRole/id[@root='456']");

This results in this:

<ClinicalDocument>
   <recordTarget>
      <patientRole>
         <id root="123" />
         
         <addr>   999 Hollow Drive   </addr>
      </patientRole>
   </recordTarget>
</ClinicalDocument>

This leaves a blank line that the receiving system can not handle.  I tried using this code to remove the whitespace but it would not work:

message.setNode('/ClinicalDocument/recordTarget/patientRole', StringUtils.deleteWhiteSpace(message.getNode('/ClinicalDocument/recordTarget/patientRole')));

but it ends up creating bad XML:

<ClinicalDocument>
   <recordTarget>
      <patientRole>&lt;patientRole&gt;&lt;idroot=&quot;123&quot;/&gt;&lt;addr&gt;999HollowDrive&lt;/addr&gt;&lt;/patientRole&gt;
         <id root="123" />
         
         <addr>   999 Hollow Drive   </addr>
      </patientRole>
   </recordTarget>
</ClinicalDocument>

In addition duplicating and converting the "<" and ">" to "&lt;" and "&gt;", it remove the space that is needed between the "id" element and the "root" attribute.

I see the removeWhitespace on message but I can't figure out the syntax to use it.  Will this help?

 

 

1 Answer

0 votes

Yes.  message.removeWhiteSpace is what you need.  Starting with the same source message above:

<ClinicalDocument>
   <recordTarget>
      <patientRole>
         <id root="123" />
         <id root="456" />
         <addr>   999 Hollow Drive   </addr>
      </patientRole>
   </recordTarget>
</ClinicalDocument>

this code:

message.removeAllNodes("/ClinicalDocument/recordTarget/patientRole/id[@root='456']");
 
var patientRoleXmlString = message.getNode('/ClinicalDocument/recordTarget/patientRole');
 
var patientRoleMessageModel = qie.parseXMLString(patientRoleXmlString);
 
var patientRoleWithOutWhiteSpace = patientRoleMessageModel.removeWhitespace(true);
 
message.setNode('/ClinicalDocument/recordTarget/patientRole', patientRoleWithOutWhiteSpace);

Produces this result:

<ClinicalDocument>
   <recordTarget>
      <patientRole><id root="123"/><addr>999 Hollow Drive</addr></patientRole>
   </recordTarget>
</ClinicalDocument>
The trick to using removeWhiteSpace is to remeber that this is a method on the XMLMessageModel.  So, we need to convert from a string to a message model then call removeWhitespace on the message model.  After we make remove the whitespace we can then put it back in the message with the setNode call.
 
If you change the removeWhiteSpace(true) to removeWhiteSpace(false) the result looks like this:
 
<ClinicalDocument>
   <recordTarget>
      <patientRole>
<id root="123"/>
 
<addr>   999 Hollow Drive   </addr>
</patientRole>
   </recordTarget>
</ClinicalDocument>
Note that in this case it did not remove carriage returns.  It did however remove white space before and after the tags as well as removed the unnesscary space in the id tag between "123" and />.
answered Oct 16, 2015 by rich-c-2789 (16,180 points)
...