我对Groovy有一点xml解析问题 这就是我的数据:
ID 001=1234
sID 001=q8b6v
d1 001=some Text
d2 001=more Text2
tzh 001=data
dse 001=data
ID 001=567823
sID 001=l3n37v2
d1 001=some Text
d2 001=more Text2
hrg 001=data
dfe 001=data
...
我需要ID,sID和d1。
我得到了什么:
What I get:
<add>
<doc>
<field name='ID'>00573419</field>
<field name='sID'>20120110572</field>
<field name='d1'>some Text</field>
<field name='ID'>00573406</field>
<field name='sID'>20120111110</field>
<field name='d1'>some Text2</field>
</doc>
</add>
我想要的是什么:
<add>
<doc>
<field name='ID'>00573419</field>
<field name='sID'>20120110572</field>
<field name='d1'>some Text</field>
</doc>
<doc>
<field name='ID'>00573406</field>
<field name='sID'>20120111110</field>
<field name='d1'>some Text2</field>
</doc>
</add>
这是我的代码的一部分:
if(!dataFile.exists()){
println "File does not exist!"
}else{
def key1, key2, key3, key4
def val1, val2, val3, val4
xml.add{
doc {
dataFile.eachLine {line ->
if(line.startsWith(regExId)){
(key1, val1) = line.split(/001=/).collect {it.trim()}
field(name:key1, val1)
}
if(line.startsWith(regExEntryId)){
(key2, val2) = line.split(/001=/).collect {it.trim()}
field(name:key2, val2)
}
if(line.startsWith(regExTitle)){
(key3, val3) = line.split(/001=/).collect {it.trim()}
field(name:key3, val3)
}
}
}
}
我希望尽可能保持简单。所以我需要的是一个声明,如'如果这里启动一个新的regExId然后启动一个新的xml-block',这样我就可以根据需要轻松添加数据的新部分(如d2)。没有现有的xml文件 感谢您的任何信息!
答案 0 :(得分:1)
这就是我提出的:
import groovy.xml.MarkupBuilder
dataFile = new File( 'data.txt' )
// A closure that will add a new doc element to our xml
def addDoc = { builder, data ->
builder.doc {
data.each { k, v ->
builder.field( name:k, v )
}
}
}
String result = new StringWriter().with { out -> // create a StringWriter
new MarkupBuilder( out ).with { xml -> // pass it to MarkupBuilder
add { // create our root level element
def data = [:]
// Then, for each line in our input file
dataFile.eachLine { line ->
// Get the key and value as you had it
def (key,value) = line.split( /001=/ )*.trim()
// Just use the keys we want
if( key in [ 'ID', 'sID', 'd1' ] ) {
// If we have a new ID and we have data
if( key == 'ID' && data ) {
// Add it to the document
addDoc( xml, data )
// And start again with a new map
data = [ ID: value ]
}
else {
// Otherwise, just add the key to the map
data[ key ] = value
}
}
}
// So, we've run out of lines. If we have data, write it in
if( data ) addDoc( xml, data )
}
}
// Convert our StringWriter to a String (this goes into the variable `result)
out.toString()
}
// And print it out
println result