AnApprentice
AnApprentice

Reputation: 111040

Getting the XML ID using REXML

I have an XML response as included below. How can I obtain the element?

In rails I have:

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

Which is not returning '[email protected]'

Thanks

<?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>

Upvotes: 2

Views: 161

Answers (1)

Justin Ko
Justin Ko

Reputation: 46846

Problem

When you do:

xml[1].attributes["id"] 

You are getting the text node of <?xml version='1.0' encoding='UTF-8'?> and then trying to get its id attribute. Since it does not exist, you got blank.

Solution

You want:

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

Note that "[email protected]" is a text node of the id element. It is not an attribute.

Upvotes: 2

Related Questions