[英]When using saxerator gem (ruby), the put_attributes_in_hash! method grabs a tag's inner text and not the attribute
I'm attempting to use the saxerator gem to parse an xml file, and I want to get the attributes for some xml tags. 我正在尝试使用saxerator gem来解析xml文件,并且我想获取某些xml标签的属性。 I am using the put_attributes_in_hash! 我正在使用put_attributes_in_hash! method to accomplish this, and it's working well, except for tags that have inner text. 方法来完成此任务,并且运行良好,除了带有内部文本的标签之外。 The method ignores attributes for these tags, and instead grabs the inner text and the inner text alone. 该方法将忽略这些标签的属性,而是仅获取内部文本和内部文本。 I want to be sure I can get the attributes for ANY tag, whether it contains text or not. 我想确保可以获取ANY标签的属性,无论它是否包含文本。
Here's my xml 这是我的XML
<PropertyPhoto
ImageUrl="http://example.com/L600/6660734.jpg"
ModificationDate="2014-10-03T09:50:14" Primary="true">Welcome home!
</PropertyPhoto>
<PropertyPhoto
ImageUrl="http://example.com/L600/6660736.jpg"
ModificationDate="2014-10-03T09:50:38">Bathroom 1
</PropertyPhoto>
<PropertyPhoto
ImageUrl="http://example.com/L600/6660737.jpg"
ModificationDate="2014-10-03T09:50:43">Bedroom 1
</PropertyPhoto>
<PropertyPhoto
ImageUrl="http://example.com/L600/6660738.jpg"
ModificationDate="2014-10-03T09:50:48">Bedroom 2
</PropertyPhoto>
<PropertyPhoto
ImageUrl="http://example.com/L600/6660739.jpg"
ModificationDate="2014-10-03T09:50:54">Bedroom 3
</PropertyPhoto>
<PropertyPhoto
ImageUrl="http://example.com/L600/6660740.jpg"
ModificationDate="2014-10-03T09:50:58">Backyard
</PropertyPhoto>
<PropertyPhoto
ImageUrl="http://example.com/L600/6660741.jpg"
ModificationDate="2014-10-03T09:51:05">Dining room
</PropertyPhoto>
<PropertyPhoto
ImageUrl="http://example.com/L600/6660742.jpg"
ModificationDate="2014-10-03T09:51:10">Living room
</PropertyPhoto>
<PropertyPhoto
ImageUrl="http://example.com/L600/6660743.jpg"
ModificationDate="2014-10-03T09:51:19">Bathroom 2
</PropertyPhoto>
<PropertyPhoto
ImageUrl="http://example.com/L600/6660744.jpg"
ModificationDate="2014-10-03T09:51:24">Kitchen
</PropertyPhoto>
<PropertyPhoto
ImageUrl="http://example.com/L600/9987810.png"
ModificationDate="2015-03-09T15:06:17">Rental Images-07
</PropertyPhoto>
and my output looks like this... 我的输出看起来像这样...
"PropertyPhoto"=>["Welcome home!", "Bathroom 1", "Bedroom 1", "Bedroom 2", "Bedroom 3", "Backyard", "Dining room", "Living room", "Bathroom 2", "Kitchen", "Rental Images-07"]
and here's an example of it working for tags with no inner text 这是一个示例,它适用于没有内部文本的标签
xml: XML:
<Property PropertyID="1234567" LocalPropertyID="">
output: 输出:
"PropertyID"=>"1234567",
"LocalPropertyID"=>"",
(in a hash) (以散列为单位)
and I'm invoking put_attributes_in_hash in the proper, functional way: 并且我以适当的功能性方式调用put_attributes_in_hash:
feed = Saxerator.parser(Net::HTTP.get(@xml_file)) do |config|
config.put_attributes_in_hash!
end
Please help! 请帮忙!
Saxerator author here. Saxerator的作者在这里。 I know this is way late, apologies. 我知道这很晚了,很抱歉。 The documentation for this could use some improvement, as you're not the first person to wonder what happens to attributes on elements that have inner text . 有关此文档的内容可能会有所改进,因为您不是第一个想知道具有内部text的元素的属性会发生什么的人。
The objects that appear to be strings in your array (eg "Welcome home!"
) are actually StringElement
objects, and you can access the attributes on them through the #attributes
method. 在您的数组中看似字符串的对象(例如"Welcome home!"
)实际上是StringElement
对象,您可以通过#attributes
方法访问它们的属性。
As of Saxerator 0.9.5 this fact is hidden because the library inherited from the String
object, which inherits String#inspect
and gives no clue that there's anything else on that object. 从Saxerator 0.9.5开始,此事实已隐藏,因为该库是从String
对象继承的,该对象继承了String#inspect
并且没有任何线索表明该对象上还有其他内容。 I plan to release a new version soon that makes this more-clear by relying on Delegate
classes and exposing its attributes through inspect
. 我计划很快发布一个新版本,该版本将依靠Delegate
类并通过inspect
公开其属性,从而使这一点更加清晰。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.