2012年1月21日土曜日

Anti-XML を使ってみた

XMLを弄る用があったので、Anti-XMLを使ってみた。

scala.xml の代替を目指しているライブラリなので使い勝手は似ているが、SelectorやZipperなど、scala.xmlには無い機能がある。

基本

com.codecommit.antixml._ をインポートすると、scala.xmlのクラスをantixmlのクラスに置き換えることができる。

import com.codecommit.antixml._

// scala.xml.Elem を com.codecommit.antixml.Elem に変換
val xml = Stay hungry, don't eat..convert
// ドキュメントではconvertメソッドではなく、
// antiメソッドを読んでいるのだが見つからない。。

// Text, Groupなどはcom.codecommit.antixml のクラスに上書きされてる。
val textGroup = Group(Text("stealth marketing"))

Selector

SelectorはPartialFunctionを継承していて、ノードをマッチさせるために使うのだが、任意の戻り値に変換できる。StringとSymbolはSelector[Elem]に暗黙変換される。

val text: Selector[String] = Selector({
  case Text(str) => str
})

// Vector[String]が返ってくる。
xml \ text // => Vector[String]

// Selector[Elem]の場合はZipper[Elem]が返ってくる。
xml \ "string" // => Zipper[Elem]
xml \ 'symbol // => Zipper[Elem]

Zipper

ZipperはXMLツリーをimmutableに更新する機能。ノードを探索して取得したZipperを書き換えた後に、unselectメソッドを呼ぶと更新されたXML全体が取得できる。

val xml = Carol Bartz.convert

val updatedXml = (xml \ 'ceo).updated(0, Scott Thompson.convert).unselect
// => Scott Thompson

探索したあとのノードから親ノードを再構成できるのは割とうれしい。でもscala.xml使うよりはXMLの更新が楽にはなっているが、まだちょっと面倒。使い方が悪いのかもしれないから今後調べる必要がある。

ちなみに xml \ 'hoge \ 'fuga のように2段階で探索した場合は、unselectを2回呼ぶ必要がある。

一応gistに上げといた↓

12 件のコメント:

匿名 さんのコメント...

I'm curious to find out what blog platform you have been utilizing? I'm experiencing some small security
issues with my latest site and I'd like to find something more safeguarded. Do you have any suggestions?

my blog; mimosa

匿名 さんのコメント...

I'm starting a new internet blog directory and was wondering if I can submit your website? I'm
hoping to increase my directory gradually by hand so that
it retains quality. I'll make sure and put your website in the proper category and I'll additionally use,
"Blogger: sca sca lab" as your anchor text. Please make
sure to let me know if this is okay with you by
mailing me at: iolacolson@gmail.com. Cheers

Feel free to surf to my web-site :: driveway

匿名 さんのコメント...

Heya. I was contemplating adding a hyperlink back to your site since both of our websites are primarily based around the
same niche. Would you prefer I link to you using your site address: http://www.
blogger.com/comment.g?blogID=2657646645413259290&postID=9184931117573142655
or website title: Blogger: sca sca lab. Please let me know at your earliest convenience.

Thanks

My website; finite

匿名 さんのコメント...

Hey there. I was thinking of adding a website link back to your site since both of our websites are based
around the same niche. Would you prefer I link to you using your site address:
http://www.blogger.com/comment.g?blogID=2657646645413259290&postID=9184931117573142655 or web site title: Blogger: sca sca lab.

Be sure to let me know at your earliest convenience.

Thanks

Also visit my web blog; importance

匿名 さんのコメント...

Hola! I've been following your weblog for some time now and finally got the courage to go ahead and give you a shout out from Humble Tx! Just wanted to tell you keep up the great job!

Here is my web blog - private road

匿名 さんのコメント...

Greetings! Quick question that's entirely off topic. Do you know how to make your site mobile friendly? My website looks weird when viewing from my iphone4. I'm trying to find a theme
or plugin that might be able to fix this problem.
If you have any suggestions, please share.

Thank you!

Check out my webpage :: stump

匿名 さんのコメント...

Appreciating the dedication you put into your blog
and detailed information you present. It's awesome to come across a blog every once in a while that isn't the same out of date
rehashed information. Fantastic read! I've bookmarked your site and I'm adding your RSS feeds to my Google account.


Review my site los

匿名 さんのコメント...

This design is steller! You certainly know how to keep
a reader entertained. Between your wit and your
videos, I was almost moved to start my own blog (well, almost.
..HaHa!) Great job. I really enjoyed what you had to say, and more than that, how
you presented it. Too cool!

Also visit my weblog ... pakerice
Also see my page: vaga

匿名 さんのコメント...

Hello. I was thinking about adding a hyperlink back to your website
since both of our sites are based mostly around the same topic.
Would you prefer I link to you using your website address:
http://www.blogger.com/comment.g?blogID=2657646645413259290&postID=9184931117573142655 or
website title: Blogger: sca sca lab. Please let me know at your
earliest convenience. Thanks

Also visit my web site; pakovanje

匿名 さんのコメント...

I was wondering if you ever considered changing the structure of your
website? Its very well written; I love what youve got to
say. But maybe you could a little more in the way of
content so people could connect with it better. Youve got an
awful lot of text for only having one or 2 pictures.
Maybe you could space it out better?

Feel free to surf to my page - http://abanyx.blinkweb.com

匿名 さんのコメント...

Gday. I'm sorry to trouble you but I happened to run across your website and discovered you happen to be using the exact same theme as me. The only problem is on my website, I'm battling to get the page layout looking like yours.
Would you mind contacting me at: reneemack@gmail.
com so I can get this figured out. By the way I have bookmarked your web page: http://www.
blogger.com/comment.g?blogID=2657646645413259290&postID=9184931117573142655 and will be
visiting frequently. Thanks alot :)!

Also visit my blog post: computer

匿名 さんのコメント...

Hello there. I noticed your site title, "Blogger: sca sca lab" doesn't really reflect the content of your web page. When writing your website title, do you believe it's most beneficial to write
it for Search engine optimization or for your viewers?

This is something I've been struggling with due to the fact I want good search rankings but at the same time I want the best quality for my website visitors.

Feel free to surf to my weblog ... Luggage shops paris