Groovy - 删除 XML 负载中的非唯一值

问题描述 投票:0回答:1

我试图从 xml 有效负载中提取两个节点。但它会导致一些重复值。有没有办法获得唯一的值组合或稍后删除重复值。

import java.text.*
import groovy.xml.*

def text = '''
<root>
  <results>
    <loc>Loc 10</loc>
    <city>ABC</city>
    <points>3</points>
    <StartDate>2023-09-11T22:39:40Z</StartDate>
    <EndDate>2023-09-13T22:45:36.437000Z</EndDate>
  </results>
  <results>
    <loc>Loc 11</loc>
   <city>ABC</city> 
    <points>4</points>
    <StartDate>2023-09-18T22:39:40Z</StartDate>
    <EndDate>2023-09-18T22:45:36.437000Z</EndDate>
  </results>
  <results>
    <loc>Loc 11</loc>
    <city>ABC</city>
    <points>4</points>
    <StartDate>2023-02-16T22:39:40Z</StartDate>
    <EndDate>2023-09-18T22:45:36.437000Z</EndDate>
  </results>
  <results>
    <loc>Loc 11</loc>
    <city>XYZ</city>
    <points>4</points>
    <StartDate>2023-09-16T22:39:40Z</StartDate>
    <EndDate>2023-12-18T22:45:36.437000Z</EndDate>
  </results>
</root>
'''
def xml = new XmlSlurper().parseText( text )
def output = new XmlParser().parseText("<root/>")

xml.results.each { resXml ->
       Node resultsNode = output.appendNode( new QName("results"), [:] )
       resXml.children().findAll { child -> child.name() != "points" && child.name()!= "StartDate" && child.name() != "EndDate" }.each { child ->
          resultsNode.appendNode( new QName(child.name()), [:], child.text() )
       }

       
}
       


println XmlUtil.serialize(output ) 

上述代码生成以下输出:

<?xml version="1.0" encoding="UTF-8"?><root>
    
  <results xmlns="">
        
    <loc>Loc 10</loc>
        
    <city>ABC</city>
      
  </results>
    
  <results xmlns="">
        
    <loc>Loc 11</loc>
        
    <city>ABC</city>
        
    <loc_name>Loc Desc 11</loc_name>
      
  </results>
    
  <results xmlns="">
        
    <loc>Loc 11</loc>
        
    <city>ABC</city>
      
  </results>
    
  <results xmlns="">
        
    <loc>Loc 11</loc>
        
    <city>XYZ</city>
      
  </results>
  
</root>

它会生成一些重复项。有没有办法删除或仅向新负载添加唯一值?

xml groovy xml-parsing
1个回答
0
投票

我会添加一个集合来检查重复项:

import java.text.*
import groovy.xml.*

def text = '''
<root>
  <results>
    <loc>Loc 10</loc>
    <city>ABC</city>
    <points>3</points>
    <StartDate>2023-09-11T22:39:40Z</StartDate>
    <EndDate>2023-09-13T22:45:36.437000Z</EndDate>
  </results>
  <results>
    <loc>Loc 11</loc>
   <city>ABC</city> 
    <points>4</points>
    <StartDate>2023-09-18T22:39:40Z</StartDate>
    <EndDate>2023-09-18T22:45:36.437000Z</EndDate>
  </results>
  <results>
    <loc>Loc 11</loc>
    <city>ABC</city>
    <points>4</points>
    <StartDate>2023-02-16T22:39:40Z</StartDate>
    <EndDate>2023-09-18T22:45:36.437000Z</EndDate>
  </results>
  <results>
    <loc>Loc 11</loc>
    <city>XYZ</city>
    <points>4</points>
    <StartDate>2023-09-16T22:39:40Z</StartDate>
    <EndDate>2023-12-18T22:45:36.437000Z</EndDate>
  </results>
</root>
'''
def xml = new XmlSlurper().parseText( text )
def output = new XmlParser().parseText("<root/>")

Set uniques = new HashSet()

xml.results.each { resXml ->
    if( !uniques.add( resXml.loc.text() ) ) return
    
    Node resultsNode = output.appendNode( new QName("results"), [:] )
    resXml.children().findAll { child -> child.name() in [ 'loc', 'city' ] }.each { child ->
        resultsNode.appendNode( new QName(child.name()), [:], child.text() )
    }
}

XmlUtil.serialize( output )

退货

<?xml version="1.0" encoding="UTF-8"?><root>
    
  <results>
        
    <loc>Loc 10</loc>
        
    <city>ABC</city>
      
  </results>
    
  <results>
        
    <loc>Loc 11</loc>
        
    <city>ABC</city>
      
  </results>
  
</root>
© www.soinside.com 2019 - 2024. All rights reserved.