2

以下に示すようなXML応答があります。どうすれば要素を取得できますか?

Railsには次のものがあります。

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

'user@company.com'を返さない

ありがとう

<?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>user@company.com</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>user@company.com</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="jon@adams.com" primary="true" />
        <gd:email rel="http://schemas.google.com/g/2005#home" address="jon@adams.com" />
    </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="person@site.com" primary="true" />
        <gd:email rel="http://schemas.google.com/g/2005#home" address="person@gmail.com" />
    </entry>
</feed>
4

1 に答える 1

2

問題

あなたがするとき:

xml[1].attributes["id"] 

のテキストノード<?xml version='1.0' encoding='UTF-8'?>を取得してから、そのid属性を取得しようとしています。存在しないので空白になります。

解決

あなたが欲しい:

xml.root.elements['id'].text
#=> user@company.com

「user@company.com」はid要素のテキストノードであることに注意してください。属性ではありません。

于 2012-10-30T19:16:17.917 に答える