Ruby Hpricot XML 解析 ARIN 文档
我正在尝试解析此 ARIN xml 文档。我想获取 orgRef 标记内的“name”属性,当我用 Hpricot 解析它时,它没有按照我的预期执行
<?xml version='1.0'?><?xml-stylesheet type='text/xsl' href='http://whois.arin.net/xsl/website.xsl' ?><nets xmlns="http://www.arin.net/whoisrws/core/v1" xmlns:ns2="http://www.arin.net/whoisrws/rdns/v1" termsOfUse="https://www.arin.net/whois_tou.html"><limitExceeded limit="256">false</limitExceeded><net termsOfUse="https://www.arin.net/whois_tou.html"><registrationDate>1998-04-18T00:00:00-04:00</registrationDate><ref>http://whois.arin.net/rest/net/NET-205-188-0-0-1</ref><ns2:delegations termsOfUse="https://www.arin.net/whois_tou.html"><ns2:delegationRef name="188.205.in-addr.arpa.">http://whois.arin.net/rest/rdns/188.205.in-addr.arpa.</ns2:delegationRef></ns2:delegations><endAddress>205.188.255.255</endAddress><handle>NET-205-188-0-0-1</handle><name>AOL-DTC</name><netBlocks><netBlock><cidrLength>16</cidrLength><endAddress>205.188.255.255</endAddress><description>Direct Assignment</description><type>DS</type><startAddress>205.188.0.0</startAddress></netBlock></netBlocks><pocs termsOfUse="https://www.arin.net/whois_tou.html"><pocLinkRef handle="AOL-NOC-ARIN" function="T" description="Tech">http://whois.arin.net/rest/poc/AOL-NOC-ARIN</pocLinkRef></pocs><orgRef name="America Online, Inc" handle="AMERIC-59">http://whois.arin.net/rest/org/AMERIC-59</orgRef><parentNetRef name="NET205" handle="NET-205-0-0-0-0">http://whois.arin.net/rest/net/NET-205-0-0-0-0</parentNetRef><startAddress>205.188.0.0</startAddress><updateDate>1998-04-27T00:00:00-04:00</updateDate><version>4</version></net></nets>
此操作:
doc = Hpricot(contents)
(doc/:orgRef).each do |orefRef|
puts "FOUND ELEMENT!"
end
当我执行 (doc/:net) 时,它会输出一些内容,但对于某些内容原因是它找不到该单个元素。任何想法。我也尝试(doc/:net/orgRef)。这是怎么回事? ARIN 是否故意使该 XML 无法解析?
I am trying to parse this ARIN xml document. I want to get the "name" attribute inside the orgRef tag, and it's not doing what I expected when I parse it with Hpricot
<?xml version='1.0'?><?xml-stylesheet type='text/xsl' href='http://whois.arin.net/xsl/website.xsl' ?><nets xmlns="http://www.arin.net/whoisrws/core/v1" xmlns:ns2="http://www.arin.net/whoisrws/rdns/v1" termsOfUse="https://www.arin.net/whois_tou.html"><limitExceeded limit="256">false</limitExceeded><net termsOfUse="https://www.arin.net/whois_tou.html"><registrationDate>1998-04-18T00:00:00-04:00</registrationDate><ref>http://whois.arin.net/rest/net/NET-205-188-0-0-1</ref><ns2:delegations termsOfUse="https://www.arin.net/whois_tou.html"><ns2:delegationRef name="188.205.in-addr.arpa.">http://whois.arin.net/rest/rdns/188.205.in-addr.arpa.</ns2:delegationRef></ns2:delegations><endAddress>205.188.255.255</endAddress><handle>NET-205-188-0-0-1</handle><name>AOL-DTC</name><netBlocks><netBlock><cidrLength>16</cidrLength><endAddress>205.188.255.255</endAddress><description>Direct Assignment</description><type>DS</type><startAddress>205.188.0.0</startAddress></netBlock></netBlocks><pocs termsOfUse="https://www.arin.net/whois_tou.html"><pocLinkRef handle="AOL-NOC-ARIN" function="T" description="Tech">http://whois.arin.net/rest/poc/AOL-NOC-ARIN</pocLinkRef></pocs><orgRef name="America Online, Inc" handle="AMERIC-59">http://whois.arin.net/rest/org/AMERIC-59</orgRef><parentNetRef name="NET205" handle="NET-205-0-0-0-0">http://whois.arin.net/rest/net/NET-205-0-0-0-0</parentNetRef><startAddress>205.188.0.0</startAddress><updateDate>1998-04-27T00:00:00-04:00</updateDate><version>4</version></net></nets>
I am doing this:
doc = Hpricot(contents)
(doc/:orgRef).each do |orefRef|
puts "FOUND ELEMENT!"
end
When I do (doc/:net), it is outputting something, but for some reason it can't find that single element. Any ideas. I try (doc/:net/orgRef) as well. What's up? Did ARIN purposely made this XML impossible to parse?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系。我得到了它。我必须做 ipWebHost = (doc/"net/orgRef")[0]['name']
Never mind. I got it. I had to do ipWebHost = (doc/"net/orgRef")[0]['name']