我对使用XML是一个新手,但我刚刚有了一个需求.我得到了一种常见的XML格式.标签中有冒号.
<THING1:things type="Container">
<PART1:Id type="Property">1234</PART1:Id>
<PART1:Name type="Property">The Name</PART1:Name>
</THING1:things>
这是一个很大的文件,它的内容远不止这些,但我希望这种格式会让人熟悉.有人知道如何处理这种XML文档吗?
我不想只写一个强力解析文本的方法,但我似乎无法在REXML或Hpricot上取得任何进展,我怀疑这是由于这些不寻常的标签.
我的ruby代码:
require 'hpricot'
xml = File.open( "myfile.xml" )
doc = Hpricot::XML( xml )
(doc/:things).each do |thg|
[ 'Id', 'Name' ].each do |el|
puts "#{el}: #{thg.at(el).innerHTML}"
end
end
...刚从http://railstips.org/blog/archives/2006/12/09/parsing-xml-with-hpricot/提高到http://railstips.org/blog/archives/2006/12/09/parsing-xml-with-hpricot/
我想我可以从这里找到一些东西,但这段代码什么也不返回.这不是错误.它刚刚回来.