2017-04-11 50 views
1

我有XML如下,的XmlSlurper問題在解析

<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> 

我想讀的詳細地址和現在用的

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

      println [email protected]'href'  
     } 

得到的值,但我不知道怎麼弄467西水庫路540-459-7505

回答

1

如果切換到XmlParser,你可以這樣做:

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

envelope.'**'.find { [email protected] == 'mapAddress' }.with { node -> 
    println h4.text() 
    println h5.text() 
    println [email protected]'href' 
    println node.children().findAll { it.getClass() == String }*.trim() 
} 
+0

非常感謝Tim! :-) –

+0

@ThangavelLoganathan,如果這有助於你需要[接受它作爲答案] – Rao

+0

蒂姆 - 其實我得到超過50 storecontainer div標記意味着我得到超過50個mapaddress div。我想逐個迭代。我使用每種方法,但沒有運氣,因爲這是新手。我可以使用該行獲得值「node.children()。findAll {it.getClass()== String} *。trim()」。我想單獨使用467 West Reservoir Road和540-459-7505在下一行使用一些拆分操作。你可以請我這個。這會很棒!提前致謝。 –