Skip to content

Instantly share code, notes, and snippets.

@nobusue
Created October 10, 2010 07:44
Show Gist options
  • Save nobusue/619066 to your computer and use it in GitHub Desktop.
Save nobusue/619066 to your computer and use it in GitHub Desktop.
// g100pon #40 XMLの読み書きを色んな方法で(DOMBuilder->XmlUtil)
import groovy.xml.XmlUtil
import groovy.xml.dom.DOMCategory
import groovy.xml.DOMBuilder
// DOMBuilderを使うとパースが簡単にできる
// パース結果のオブジェクトはDOM
def doc = DOMBuilder.parse(
new InputStreamReader(new FileInputStream('sample.xml'),'UTF-8') )
def root = doc.documentElement
// DOMCategoryを使うとDOM操作が簡単になる
use(DOMCategory) {
def groceries = root.category.findAll{ it.'@type' == 'groceries' }[0].item
groceries.each { g ->
g.value = 'luxury ' + g.text()
}
def supplies = root.category.findAll{ it.'@type' == 'supplies' }[0].item
supplies.findAll{ it.text() == 'pen' }.each { s ->
s['@quantity'] = s.'@quantity'.toInteger() + 2
s['@when'] = 'Urgent'
}
def presents = root.category.find{ it.'@type' == 'present' }
presents.item.each {
presents.removeChild(it)
}
presents.appendNode('item', "mother's birthday")
presents.appendNode('item', [when:'Oct 15'], "Monica's birthday")
}
def out = new FileOutputStream('domcategory.output.xml')
// XmlUtilは内部でXSLTを利用してシリアライズを行う(空のスタイルシートを適用)
XmlUtil.serialize(root, out)
// g100pon #40 XMLの読み書きを色んな方法で(XmlParser->XmlNodePrinter)
// パース結果のオブジェクトはNode型なので、コレクション型のGDKが全て使える
def root = new XmlParser().parse('sample.xml')
def groceries = root.category.findAll{ it.@type == 'groceries' }
groceries[0].item.each { g ->
g.value = 'luxury ' + g.text()
}
def supplies = root.category.findAll{ it.@type == 'supplies' }
supplies[0].item.findAll{ it.text() == 'pen' }.each { s ->
s.@quantity = s.@quantity.toInteger() + 2
s.@when = 'urgent'
}
def presentCategory = root.category.find{ it.@type == 'present' }
presentCategory.children().clear()
presentCategory.appendNode('item', "mother's birthday")
presentCategory.appendNode('item', [when:'Oct 15'], "Monica's birthday")
def writer = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream("xmlparser.output.xml"), "UTF-8"))
// パース結果のオブジェクトはNode型なので、XML固有の情報はパース時点で失われている
writer << '<?xml version="1.0" encoding="UTF-8"?>' << "\n"
def printer = new XmlNodePrinter(new PrintWriter(writer))
printer.setPreserveWhitespace(true)
printer.print(root)
// g100pon #40 XMLの読み書きを色んな方法で(XmlSlurper->StreamingMarkupBuilder)
import groovy.xml.StreamingMarkupBuilder
// パース結果のオブジェクトはGPath型なので、GPath型で定義されているメソッド以外は使えない
def root = new XmlSlurper().parse('sample.xml')
def groceries = root.category.findAll{ it.@type == 'groceries' }
def g = groceries[0]
g.item.eachWithIndex {item, i ->
g.item[i] = 'luxury ' + item
}
def supplies = root.category.findAll{ it.@type == 'supplies' }
def pens = supplies[0].item.findAll{ it.text() == 'pen' }
pens.each { p ->
p.@quantity = (p.@quantity.toInteger() + 2).toString()
p.@when = 'Urgent'
}
def presents = root.category.find{ it.@type == 'present' }
presents.replaceNode{ node ->
category(type:'present'){
item("mother's birthday")
item("Monica's birthday", when:'Oct 15')
}
}
def out = new OutputStreamWriter(
new FileOutputStream("xmlslurper.output.xml"), "UTF-8")
// XMLの構造しか保持していないので、MarkupBuilderで組み立て直す必要がある
def outputBuilder = new StreamingMarkupBuilder()
def writer = outputBuilder.bind{
mkp.xmlDeclaration()
mkp.yield(root)
}
out << writer
<?xml version="1.0" encoding="UTF-8"?>
<shopping>
<category type="groceries">
<item>chocolate</item>
<item>coffee</item>
</category>
<category type="supplies">
<item>paper</item>
<item quantity="4">pen</item>
</category>
<category type="present">
<item when="Aug 10">Katherine's birthday</item>
</category>
</shopping>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment