Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: XML Whitespace Removal?

Anyone know of a good scala library to do whitespace removal/compaction from XML?

 <foo>   <bar>hello world</bar>   <baz>  xxx  </baz> </foo> 

to:

 <foo><bar>hello world</bar><baz>xxx</baz></foo> 
like image 943
harryh Avatar asked Nov 09 '09 21:11

harryh


People also ask

How do I remove white space between XML tags?

Attach a Filler node to the XML source node. Open the Filler node and use the field chooser to select the field with the unwanted spaces. Set Replace to Based on condition and set Condition to true.

Does XML ignore white space?

In XML documents, there are two types of whitespace: Significant whitespace is part of the document content and should be preserved. Insignificant whitespace is used when editing XML documents for readability. These whitespaces are typically not intended for inclusion in the delivery of the document.

How to preserve whitespace in XML?

When xml:space is used on an element with a value of preserve , the whitespace in that element's content must be preserved as is by the application that processes it. The whitespace is always passed on to the processing application, but xml:space provides the application with a hint regarding how to process it.

Is XML space sensitive?

As XML is case sensitive, xml:space must be declared using all lowercase letters. The value of xml:space applies to the element and any elements it contains unless overridden by the xml:space value of the contained elements.


2 Answers

scala.xml.Utility.trim() should do what you want:

scala> val x = <foo>      |   <bar>hello world</bar>      |   <baz>  xxx  </baz>      | </foo> x: scala.xml.Elem =  <foo>          <bar>hello world</bar>          <baz>  xxx  </baz>        </foo>  scala> scala.xml.Utility.trim(x) res0: scala.xml.Node = <foo><bar>hello world</bar><baz>xxx</baz></foo> 
like image 164
Walter Chang Avatar answered Oct 07 '22 17:10

Walter Chang


For whatever it's worth, this is what I've got going on now in the "roll my own" strategy:

 def compactXml(xml: Node): Node = {   (xml map {     case Elem(prefix, label, attributes, scope, children @ _*) => {       Elem(prefix, label, attributes, scope, children.map(compactXml(_)) :_*)     }     case Text(data) => Text(data.trim)      case x => x   }).first } 
like image 43
harryh Avatar answered Oct 07 '22 16:10

harryh