简体   繁体   English

使用saxerator gem(ruby)时,put_attributes_in_hash! 方法获取标签的内部文本而不是属性

[英]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.

相关问题 使用 pagy gem -Ruby on Rails 时没有方法错误 - No method error when using pagy gem -Ruby on Rails 使用提交的哈希安装 gem - Installing a gem using a commit's hash Rails model #attributes 方法将 gem 插入关联显示为 model 属性 - Rails model #attributes method shows gem inserted association as model attribute 如何在Rails上的ruby中的nested_attributes哈希中添加额外的属性? - How to add an extra attribute in nested_attributes hash in ruby on rails? 使用克隆方法使用超级类而非宝石来制作红宝石对象的深层副本 - Making deep copy's of ruby objects using clone method using super class not a gem 如何在 Ruby 中访问此哈希对象的属性? - How do I access this hash's object's attributes in Ruby? 使用 roo gem 将从 xlsx 提取的哈希存储在 ruby​​ 中的数据库中 - Store hash extracted from xlsx in database in ruby using roo gem Ruby on Rails - 使用acts_as_votable gem 时“nil:NilClass 的未定义方法‘[]’” - Ruby on Rails - "undefined method `[]' for nil:NilClass" when using acts_as_votable gem Ruby Money gem的最佳rounding_method是什么? - What's the best rounding_method for Ruby Money gem? 使用属性将XML转换为ruby哈希 - convert XML to ruby hash with attributes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM