1

私はgroovyにまったく慣れていないので、あなたの助けを求めています. デルタ xml ファイルを読み取り、名前属性に基づいてマスター xml ファイルを更新したいと考えています。両方の xml ファイルは同じ構造です。マスター xml ファイルの属性値を更新しようとしています。ただし、マスター ファイルが一度空白になると、ファイルが更新されます。どこが間違っているのかわかりません。

以下は、xml 構造です。

<item-descriptor name="user" cache-mode="simple" item-cache-size="3000" query-cache-size="1000"  item-cache-timeout="900000"  query-expire-timeout="60000" />
<item-descriptor name="contactInfo" cache-mode="simple" item-cache-size="10000" query-cache-size="1000"  item-cache-timeout="900000"  query-expire-timeout="60000" /> 

以下は、このためのコードです。

def templatexmlConfig = new XmlParser().parse(templateConfigFile)
def basexmlConfig = new XmlSlurper().parse(baseConfigFile)
def templateItemDesNode = templatexmlConfig.'item-descriptor'
def baseItemDesNode=basexmlConfig.'item-descriptor'
templateItemDesNode.each()
{
    Map bindings=[:]
    def nameAttr=it.attribute('name')
    it.attributes().each{attrName,attrValue->
    if(!attrName.equals('name'))
    {
             bindings.put(attrName,attrValue)
    }}

    if(baseItemDesNode.find{ it.@name.text().equals(nameAttr)}.size()!=0)
    {   
             bindings.each
             {   
                 def a=it.key
                 def v=it.value
             baseItemDesNode.find{ it.@name.text().equals(nameAttr)}.@'a'="${v}"                                             }

    }                           
}
new XmlNodePrinter(new PrintWriter(outputFile)).print(basexmlConfig)
4

1 に答える 1

0

わかりました、2 つの xml ドキュメントの例が与えられました。

def templateXml = '''<xml>
                    |  <item-descriptor name="a" cache-mode="r1" item-cache-size="1"/>
                    |  <item-descriptor name="b" cache-mode="r2" item-cache-size="2" new-attr="tim"/>
                    |  <item-descriptor name="z" cache-mode="r3" item-cache-size="3"/>
                    |</xml>'''.stripMargin() ;
def baseXml = '''<xml>
                |  <item-descriptor name="b" cache-mode="o1" item-cache-size="10"/>
                |  <item-descriptor name="c" cache-mode="o2" item-cache-size="11"/>
                |  <item-descriptor name="a" cache-mode="o3" item-cache-size="12"/>
                |</xml>'''.stripMargin()

これらを解析できます (両方に XmlParser を使用すると、一方にはパーサーがあり、もう一方にはスラーパーがありました)。

def templatexmlConfig = new XmlParser().parseText( templateXml )
def basexmlConfig = new XmlParser().parseText( baseXml )

そして、item-descriptorノードを取得します(あなたが持っていたように)

def templateItemDesNode = templatexmlConfig.'item-descriptor'
def baseItemDesNode = basexmlConfig.'item-descriptor'

次に、 template をループし、item-descriptors名前以外の属性のマップを生成し (findAllループよりも簡単に使用できます)、ノード内のすべてのノードをbaseXml同じ名前に置き換えます。

templateItemDesNode.each() { tidn ->
  Map bindings = tidn.attributes().findAll { it.key != 'name' }
  def nameAttr = tidn.@name
  baseItemDesNode.find { b -> b.@name == nameAttr }?.with { node ->
    bindings.each { a, v ->
      node.@"$a" = v
    }
  }
}

この簡単に実行できる例では、Xml を出力するだけです。

println new StringWriter().with { sw ->
  new XmlNodePrinter( new PrintWriter( sw ) ).print(basexmlConfig)
  sw.toString()
}

そして、実行すると、上記が出力されます:

<xml>
  <item-descriptor name="b" cache-mode="r2" item-cache-size="2" new-attr="tim"/>
  <item-descriptor name="c" cache-mode="o2" item-cache-size="11"/>
  <item-descriptor name="a" cache-mode="r1" item-cache-size="1"/>
</xml>

明らかに、作業コードについては、ここにある文字列ではなくparseon を使用してファイルを解析し、出力をファイルへの書き込みに戻す必要があります...XmlParsers

それが役に立てば幸い!

于 2012-06-21T08:30:15.367 に答える