利用Hpricot解析XML
上一篇 /
下一篇 2010-12-21 22:32:20
/ 个人分类:自动化测试
<ProductInfo>
<DocID>22893</DocID>
<Saled>7400</Saled>
<sale_day>472</sale_day>
<sale_week>3269</sale_week>
<PID>706089</PID>
<HighPrice>1700</HighPrice>
<Class id="01.56.00.00" Count="29187" Name="医学"/>
<Class id="01.58.00.00" Count="19275" Name="进口原版书"/>
</Content>
<Summary>
本书是近年来难得一见的优秀的家庭教育原创作品,是教育专家尹建莉的教子<font class=dot>...</font>
</Summary>
<ISBN>9787506345040</ISBN>
</ProductInfo>
−
<ProductInfo>
……………
<ProductInfo>
Require ‘hpricot
require 'net/http'
require 'rexml/document'’
doc=Hpricot(open(“http://xmlexchange.dangdang.com/list.php?catalog=01&page=1&pagesize=25&ORDER=-1&MEDIUM=&length=188&icon_flag_bang=&promotion_filt=&stock”))
doc.search("*/ProductInfo") do |a| ###获取一个商品的所有信息,然后存储到a
a.search("*/#{key}") do |b| ##寻找下一个子节点
puts b.inner_html ##打印文本信息
if key=="Summary"
xml_result<<b.inner_html. 获取文本信息
elsif key=="Info"
xml_result<<b.inner_html
elsif key==’class’
cat_xml<<b.attributes[‘id’] ##获取属性值
cat_xml<<b.attributes['count']
end
end
end
end
hpricot(open(“www.xxxxxx”)) 相当于 hpricot(URI.parse(‘www.xxxx’).read)
收藏
举报
TAG: