Groovy:创建新的xml结构以及如何在构建时插入新节点
我在Groovy中遇到了一点xml解析问题。Groovy:创建新的xml结构以及如何在构建时插入新节点,groovy,xml-parsing,Groovy,Xml Parsing,我在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
这就是我的数据的样子:
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
...
我需要身份证,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>
我想让它尽可能简单。所以我需要一个类似“如果这里启动一个新的regExId,然后启动一个新的xml块”的语句,这样我就可以根据需要轻松地添加数据的新部分(如d2)。没有现有的xml文件。谢谢你提供的任何信息 这就是我想到的:
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
这就是我想到的:
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
您的数据在每行末尾都有
?我现在看到了。。。不,每行末尾没有?编辑了该部分,谢谢。现在它应该是正确的。您的数据在每行末尾都有
?我现在看到了。。。不,每行末尾没有?编辑了该部分,谢谢。现在它应该是正确的。只是“哇”!谢谢你!我有很多东西要学:)结果是正确的。只有一个问题:“data=[ID:value]”ID来自哪里?@user1095901这是新地图条目的键(我们知道它是
ID
)。。。放上:数据=[(键):值]
可能会更容易理解。它基本上是将新ID放入地图中,以便下次写入文档谢谢您的快速响应。我将把我的问题标记为已解决。再次感谢。@user1095901很高兴我能帮上忙,希望我解释得足够好:-)关键部分起了作用:)ID的语法很混乱,我期待的是带有引号的“ID”。只是“哇”!谢谢你!我有很多东西要学:)结果是正确的。只有一个问题:“data=[ID:value]”ID来自哪里?@user1095901这是新地图条目的键(我们知道它是ID
)。。。放上:数据=[(键):值]
可能会更容易理解。它基本上是将新ID放入地图中,以便下次写入文档谢谢您的快速响应。我将把我的问题标记为已解决。再次感谢。@user1095901很高兴我能帮上忙,希望我解释得足够好:-)关键部分起了作用:)ID的语法很混乱,我希望是带有引号的“ID”。
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