gpt4 book ai didi

ruby-on-rails - 使用 REXML 获取 XML ID

转载 作者:太空宇宙 更新时间:2023-11-03 16:06:17 25 4
gpt4 key购买 nike

我有一个 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>

最佳答案

问题

当你这样做时:

xml[1].attributes["id"] 

您正在获取 <?xml version='1.0' encoding='UTF-8'?> 的文本节点然后尝试获取其 id 属性。因为它不存在,所以你一片空白。

解决方案

你想要:

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

请注意,“user@company.com”是 id 元素的文本节点。它不是一个属性。

关于ruby-on-rails - 使用 REXML 获取 XML ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13145888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com