XmlSlurper Issue while Parsing

跟風遠走 提交于 2019-12-11 07:17:20

问题


I have XML as below,

<div class="storeContainer">
      <div class="storeCount">50</div>
      <div class="mapAddress" id="store50">
         <h4 onclick="changeMap(50,38.872432,-78.530463);">Woodstock, VA</h4>
         <h5>Woodstock Square</h5>467 West Reservoir Road
<br/>540-459-7505
<br/>
         <a href="https://maps.google.com/maps?saddr=geo&amp;daddr=467+West+Reservoir+Road+Woodstock+VA+22664&amp;sll=38.872432,-78.530463&amp;sspn=0.011265,0.014098&amp;z=12"
            class="getDirections">Get Directions</a>
      </div>
   </div>
</div>

I wanted to read address details and am using the

def envelope = new XmlSlurper().parseText(response.toString());
        envelope.'**'
        .each {
            println it.h4.text()
            println it.h5.text()

            println it.a.@'href'    
        }

to get the values but i don't know how to get 467 West Reservoir Road and 540-459-7505


回答1:


If you switch to XmlParser, you can do:

def envelope = new XmlParser().parseText(xml);

envelope.'**'.find { it.@class == 'mapAddress' }.with { node ->
    println h4.text()
    println h5.text()
    println a.@'href'
    println node.children().findAll { it.getClass() == String }*.trim()
}


来源:https://stackoverflow.com/questions/43348809/xmlslurper-issue-while-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!