Adding XML element with Scala's XML Support -


i got pretty basic question don't seem find elegant scala way of doing this.

i want insert programatically generated xml tags existing xml structure read in file using

xml.xml.loadfile(...) 

in how create xml root node in scala without literal element name? found approach creating tags.

def textelem(name: string, text: string) =  elem(null, name, null, topscope, text(text)) 

having xml tree

<root>   <set start="christmas">     <data>       <val>abc</val>       ...     </data>     <custom>        <entry>def</entry>        <!-- append here -->      </custom>   </set>   <set start="halloween">      ...   </set>  </root> 

how select custom section christmas set, append programatically generated xml tags , save whole xml tree file?

thanks help!

q. how select custom section christmas set ?
a. in scala, can use def \\(that: string): nodeseq

val custom = christmas \\ "custom" println("-- print custom element --") println(custom) println("-- end --") 

output:

-- print custom element -- <custom>    <entry>def</entry>   <!-- append here --> </custom> -- end -- 

q. append programatically generated xml tags
a. useful => scala - xml insert/update
, link => substituting xml values programatically scala

for example, wrote this:
looks like, transform method make easy manipulate xmls.

        // q. append programatically generated xml tags         // a. use rewriterule#transform         val rule = new appendrule         val appended = rule.transform(christmas)         println("-- print custom element --")         println(pp.format(appended(0)))         println("-- end --")     } }  class appendrule extends rewriterule {     override def transform(n: node): seq[node] = n match {         case <entry>{v}</entry>              => println(s"find value in tag <entry> => $v")                <entry>abc</entry>                <entry>{v}</entry>         case elem: elem              => elem copy (child = elem.child flatmap (this transform))         case other              => other     } } 

output:

find value in tag <entry> => def -- print custom element -- <root>   <set start="christmas">     <data>       <val>abc</val>     </data>     <custom>       <entry>abc</entry>       <entry>def</entry>       <!-- append here -->     </custom>   </set>   <set start="halloween"> </set> </root> -- end -- 

q. save whole xml tree file
a. please use scala.xml.xml#save


Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -