ruby如何解析包含<g:?的xml数据

时间:2017-02-10 10:25:48

标签: ruby xml parsing

=“”

我有xml数据如下 -

                      <rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
                  <channel>
                  <title>Test Store</title>
                  <link>http://www.example.com</link>
                  <description>An example item from the feed</description>
                  <item>
                  <g:id>DB_1</g:id>
                  <g:title>Dog Bowl In Blue</g:title>
                  <applink property="ios_url" content="example-ios://electronic/db_1"/>
                  <applink property="ios_app_store_id" content="123"/>
                  <applink property="ios_app_name" content="Electronic Example iOS"/>
                  <applink property="android_url" content="example-android://electronic/db_1"/>
                  <applink property="android_package" content="com.electronic.example"/>
                  <applink property="android_app_name" content="Electronic Example Android"/>
                  <applink property="windows_phone_url" content="example-windows://electronic/db_1"/>
                  <applink property="windows_phone_app_id" content="64ec0d1b-5b3b-4c77-a86b-5e12d465edc0"/>
                  <applink property="windows_phone_app_name" content="Electronic Example Windows"/>
                  <g:description>Solid plastic Dog Bowl in marine blue color</g:description>
                  <g:google_product_category>Animals > Pet Supplies</g:google_product_category>
                  <g:product_type>Bowls & Dining > Food & Water Bowls</g:product_type>
                  <g:link>http://www.example.com/bowls/db-1.html</g:link>
                  <g:image_link>http://images.example.com/DB_1.png</g:image_link>
                  <g:condition>new</g:condition>
                  <g:availability>in stock</g:availability>
                  <g:price>9.99 GBP</g:price>
                  <g:brand>Example</g:brand>
                  <g:item_group_id>DB_GROUP_1</g:item_group_id>
                  <g:shipping>
                  <g:country>UK</g:country>
                  <g:service>Standard</g:service>
                  <g:price>9.95 GBP</g:price>
                  </g:shipping>
                  <g:custom_label_0>Made in Waterford, IE</g:custom_label_0>
                  </item>
                  <item>
                  <g:id>DB_2</g:id>
                  <g:title>Dog Bowl In Yellow</g:title>
                  <applink property="ios_url" content="example-ios://electronic/db_2"/>
                  <applink property="ios_app_store_id" content="123"/>
                  <applink property="ios_app_name" content="Electronic Example iOS"/>
                  <applink property="android_url" content="example-android://electronic/db_2"/>
                  <applink property="android_package" content="com.electronic.example"/>
                  <applink property="android_app_name" content="Electronic Example Android"/>
                  <applink property="windows_phone_url" content="example-windows://electronic/db_2"/>
                  <applink property="windows_phone_app_id" content="64ec0d1b-5b3b-4c77-a86b-5e12d465edc0"/>
                  <applink property="windows_phone_app_name" content="Electronic Example Windows"/>
                  <g:description>Solid plastic Dog Bowl in yellow color</g:description>
                  <g:google_product_category>Animals > Pet Supplies</g:google_product_category>
                  <g:product_type>Bowls & Dining > Food & Water Bowls</g:product_type>
                  <g:link>http://www.example.com/bowls/db-2.html</g:link>
                  <g:image_link>http://images.example.com/DB_2.png</g:image_link>
                  <g:condition>new</g:condition>
                  <g:availability>in stock</g:availability>
                  <g:price>9.99 GBP</g:price>
                  <g:brand>Example</g:brand>
                  <g:item_group_id>DB_GROUP_1</g:item_group_id>
                  <g:shipping>
                  <g:country>UK</g:country>
                  <g:service>Standard</g:service>
                  <g:price>9.95 GBP</g:price>
                  </g:shipping>
                  <g:custom_label_0>Made in Waterford, IE</g:custom_label_0>
                  </item>
                  </channel>
                  </rss>

我正在尝试解析下面的内容 -

def self.process_xml_data(xml_str)
puts "process_xml_data"
xml_doc  = Nokogiri::XML(xml_str) 

我想替换

<g:image_link>http://static2.buyma.com/imgdata/item/170209/0026293105/428_1.jpg</g:image_link>

使用另一个网址,但我没有任何想法。

我尝试了各种方式,例如 **xml_doc.css('image_link')** ,但似乎它们不适用于此xml数据。

1 个答案:

答案 0 :(得分:1)

g是名称空间,需要https://www.w3.org/TR/xml-names/

中列出的名称空间声明
xmlns:g="http://base.google.com/ns/1.0"

您应该能够选择带有CSS查询g|image_link的链接并更改此链接。

def self.process_xml_data(xml_str)
  puts "process_xml_data"
  xml_doc  = Nokogiri::XML(xml_str)
  image_link_tag = xml_doc.at_css "g|image_link"
  image_link_tag.content = "new_link"
end