好吧,由于速度和 _why 的消失,从工作的 Hpricot 切换到 Libxml-ruby,看了 Nokogiri 片刻,但决定看看 Libxml-ruby 的速度和寿命。我一定遗漏了一些基本的东西,但我试图做的却是行不通,这是我的 XML 字符串:
<?xml version="1.0" encoding="utf-8" ?>
<feed>
<title type="xhtml"></title>
<entry xmlns="http://www.w3.org/2005/Atom">
<id>urn:publicid:xx.xxx:xxxxxx</id>
<title>US--xxx-xxxxx</title>
<updated>2009-08-19T15:49:51.103Z</updated>
<published>2009-08-19T15:44:48Z</published>
<author>
<name>XX</name>
</author>
<rights>blehh</rights>
<content type="text/xml">
<nitf>
<head>
<docdata>
<doc-id regsrc="XX" />
<date.issue norm="20090819T154448Z" />
<ed-msg info="Eds:" />
<doc.rights owner="xx" agent="hxx" type="none" />
<doc.copyright holder="xx" year="2009" />
</docdata>
</head>
<body>
<body.head>
<hedline>
<hl1 id="headline">headline</hl1>
<hl2 id="originalHeadline">blah blah</hl2>
</hedline>
<byline>john doe<byttl>staffer</byttl></byline>
<distributor>xyz</distributor>
<dateline>
<location>foo</location>
</dateline>
</body.head>
<body.content>
<block id="Main">
story content here
</block>
</body.content>
<body.end />
</body>
</nitf>
</content>
</entry>
</feed>
完整的提要中大约有 150 个这样的条目。
我只想遍历 150 个条目,然后找出内容和属性,但我在 libxml-ruby 上玩得很开心,因为它与 Hpricot 一起工作得很好。
这个小片段显示我什至没有得到条目:
parser = XML::Parser.string(file)
doc = parser.parse
entries = doc.find('//entry')
puts entries.size
entries.each do |node|
puts node.inspect
end
有任何想法吗?我查看了文档,找不到一个简单的 XML 文件,这里是 x,y,z 的示例。这应该很简单。