2012-10-30 58 views
2

我有一個XML響應,如下所示。我如何獲得元素?使用REXML獲取XML ID

在鐵軌我:

xml = REXML::Document.new(contacts_as_xml) 
Rails.logger.info xml[1].attributes["id"] 

這是沒有返回 '[email protected]'

感謝

<?xml version='1.0' encoding='UTF-8'?> 
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/&quot;Ck8BQ38yfyt7I2A9WhNSFk8.&quot;"> 
    <id>[email protected]</id> 
    <updated>2012-10-30T18:14:12.197Z</updated> 
    <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" /> 
    <title>Users's Contacts</title> 
    <link rel="alternate" type="text/html" href="http://www.google.com/" /> 
    <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full" /> 
    <link rel="http://schemas.google.com/g/2005#post" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full" /> 
    <link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/batch" /> 
    <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full?max-results=10000" /> 
    <author> 
     <name>User Howdy</name> 
     <email>[email protected]</email> 
    </author> 
    <generator version="1.0" uri="http://www.google.com/m8/feeds">Contacts</generator> 
    <openSearch:totalResults>2</openSearch:totalResults> 
    <openSearch:startIndex>1</openSearch:startIndex> 
    <openSearch:itemsPerPage>10000</openSearch:itemsPerPage> 
    <entry gd:etag="&quot;QXc5eDVSLit7I2A9WhdSFk8PTgU.&quot;"> 
     <id>http://www.google.com/m8/feeds/contacts/myuser%40mysite.com/base/970a99881a51f3</id> 
     <updated>2011-07-25T20:31:50.920Z</updated> 
     <app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-25T20:31:50.920Z</app:edited> 
     <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" /> 
     <title>Jon Paris</title> 
     <link rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/myuser%40mysite.com/970a99881a51f3" /> 
     <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/970a99881a51f3" /> 
     <link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/970a99881a51f3" /> 
     <gd:name> 
      <gd:fullName>Jon Adams</gd:fullName> 
      <gd:givenName>Jon</gd:givenName> 
      <gd:familyName>Adams</gd:familyName> 
     </gd:name> 
     <gd:email rel="http://schemas.google.com/g/2005#other" address="[email protected]" primary="true" /> 
     <gd:email rel="http://schemas.google.com/g/2005#home" address="[email protected]" /> 
    </entry> 
    <entry gd:etag="&quot;R3YzcTFbKit7I2A9WhJbE00ORQY.&quot;"> 
     <id>http://www.google.com/m8/feeds/contacts/myuser%40mysite.com/base/1229b5e8eeef3e3</id> 
     <updated>2012-09-22T08:57:06.889Z</updated> 
     <app:edited xmlns:app="http://www.w3.org/2007/app">2012-09-22T08:57:06.889Z</app:edited> 
     <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" /> 
     <title></title> 
     <link rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/myuser%40mysite.com/1229b5e8eeef3e3" gd:etag="&quot;UQZOJlcjWit7I2BmGBdVTzBfL2E2GGI5Myk.&quot;" /> 
     <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/1229b5e8eeef3e3" /> 
     <link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/1229b5e8eeef3e3" /> 
     <gd:email rel="http://schemas.google.com/g/2005#other" address="[email protected]" primary="true" /> 
     <gd:email rel="http://schemas.google.com/g/2005#home" address="[email protected]" /> 
    </entry> 
</feed> 

回答

2

問題

當你這樣做:

xml[1].attributes["id"] 

您正在獲取<?xml version='1.0' encoding='UTF-8'?>的文本節點,然後嘗試獲取其id屬性。由於它不存在,你得到空白。

解決方案

你想:

xml.root.elements['id'].text 
#=> [email protected] 

注意, 「[email protected]」 是id元素的文本節點。這不是一個屬性。

+0

有道理,它的工作。謝謝 – AnApprentice