Last active
July 25, 2017 03:15
-
-
Save muumin/4761570 to your computer and use it in GitHub Desktop.
GroovyでXML操作(GPath)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def xml = '''<?xml version="1.0" encoding="UTF-8"?> | |
<hoge> | |
<hoge1>test</hoge1> | |
<hoge-2>test2</hoge-2> | |
<hoge3 id='999' name="name3">test3</hoge3> | |
<hoge4> | |
<value>1</value> | |
<value>2</value> | |
<value>3</value> | |
</hoge4> | |
</hoge> | |
''' | |
def node = new XmlParser().parseText(xml) | |
// ハイフン含まれてないないならそのまま | |
println node.hoge1.text() | |
// 含んでる場合シングルクォートで囲む | |
println node.'hoge-2'.text() | |
// 属性 | |
println node.hoge3['@name'].text() | |
println node.hoge3.'@id'.text() | |
// n番目を取得 | |
println node.hoge4.value[1].text() | |
// 要素分繰り返し | |
node.hoge4.value.each { | |
println it.text() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment