Java Virtal Machine.net

[ News ] rss

February 01, 2011

Mark Wielaard: New GPG key. Finally created a new GPG key using gnupg. The old one was a DSA/1024 bits one and 8 years old. The new one is a RSA/2048 bits one. I will use the new one in the future to sign any release tarballs I might create. pub 2048R/57816A6A 2011-01-29 Key f...

More »

February 01, 2011

Andrew Hughes: [SECURITY] IcedTea6 1.7.8, 1.8.5, 1.9.5 Released!. We are pleased to announce a new set of security releases, IcedTea6 1.7.8, IcedTea6 1.8.5 and IcedTea6 1.9.5. This update contains the following security updates: The IcedTea project provides a harness to build the source code from OpenJDK6 u...

More »

April/2024
SunMonTueWedThuFriSat
 123456
78910111213
14151617181920
2122232425 2627
282930    

[ Archives News
for 'Java Technology' ]

home > news > java technology > transforming an xml tree with scala partial functions

Transforming an XML Tree with Scala Partial Functions

In my last blog, I outlined how I found the Scala XML library a pleasant solution for unpleasant XML format conversion jobs. In those jobs, I had to completely transform the document from one grammar to another. When you need to make small tweaks to a document, the library a bit more of a hassle. This page by Burak Emir, the author of the Scala XML library, states: “The Scala XML API takes a functional approach to representing data, eschewing imperative updates where possible. Since nodes as used by the library are immutable, updating an XML tree can a bit verbose, as the XML tree has to be copied.” A verbose example follows. Here is what I needed to do. Whenever I had a Filename.java, I had to replace it with the actual file name, with each line preceded by a line number. That part is simple: def getExample(node: Node) = {io.Source.fromFile(new File((node \ "p").toString)).getLines().map( w => {w} )} But how can you say “Do this for all , and leave the rest alone?” In a functional program, you need to copy the tree, so I figured I should write a universal transformer method. /** * Transforms all descendants matching a predicate. * n a node * pred the predicate to match * trans the transformation to apply to matching descendants */ def transformIf(n: Node, pred: (Node)=>Boolean, trans: (Node)=>Node): Node = if (pred(n)) trans(n) else n match { case e: Elem => if (e.de...


Date: May, 16 2010
Url: http://www.java.net/blog/cayhorstmann/archive/2010/05/16/transforming-xml-tree-scala-partial-functions


Others News

©2002-2019Java-Virtual-Machine.net de | Bootstrap Templates | bootstrap modal popup | bootstrap forms | css slider