2013-07-11 142 views
0

假設我有一個節點我可以使用scala編輯xml嗎?

var xml = XML.loadFile("some/file/here") 

我能做些什麼,我應該想一些元素的值更改爲新值

也就是說,

... 
<anElement>5</anElement> 
... 

... 
<anElement>blooblahblahyah</anElement> 
...  

道歉,如果這是一個非常愚蠢的問題,我會懷疑對Scala來說是新的,並且在xml編輯中沒有找到明確的答案。

回答

3

Node是不可變的,這使得編輯它有點乏味。
在Scala中有例子XMLbook

val foo = <foo><bar>1</bar><bar>2</bar></foo> 
foo.copy (child = foo.child.map {case bar: scala.xml.Elem => 
    bar.copy (child = scala.xml.Text ((bar.text.toInt + 1).toString))}) 

res0: scala.xml.Elem = <foo><bar>2</bar><bar>3</bar></foo>

+2

也看看xml.transform:http://www.scala-lang.org/api/current/index.html#scala.xml.transform.RewriteRule;幾個例子:http://daily-scala.blogspot.ru/2009/12/xml-transformation-1.html – dmitry

相關問題