2017-02-10 95 views
-1

我有XML数据,如如下─ruby​​如何解析这个包含<g :?的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

这不是整个文件吗?我认为应该有一个'xmlns:g =“something”'属性来建立g作为名字空间。请参阅http://stackoverflow.com/questions/15849357/when-styling-xml-with-css-how-to-refer-to-tag-names-that-c​​ontain-periods-or-col#15849617和https:/ /www.w3.org/TR/xml-names/ – HarlemSquirrel

+0

对不起,我更新了我的csv。 – RajSharma

+0

我只更新了它的一部分。它包含男人标签 – RajSharma

回答

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 
+0

对不起,我已经更新了我的csv。你能检查一次吗? – RajSharma

+0

我想我不能使用这行doc = File.open(“source.xml”){| f | Nokogiri :: XML(f)},因为我的csv长度为1.5 GB。 – RajSharma

+0

我只更新了它的一部分。它包含男人标签。 – RajSharma