Python解析XML

Python解析XML,python,xml,xpath,Python,Xml,Xpath,我创建了如下根目录: import xml.etree.ElementTree as ET tree = ET.parse('country_data.xml') root = tree.getroot() 下面是我的XML示例: <?xml version="1.0" encoding="UTF-8"?> <feed gd:etag="&quot;Rn84fzVSLyt7I2A9XRVbFkwOQAE.&quot;" xmlns="http://www.w3

我创建了如下根目录:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()
下面是我的XML示例:

<?xml version="1.0" encoding="UTF-8"?>
<feed gd:etag="&quot;Rn84fzVSLyt7I2A9XRVbFkwOQAE.&quot;" xmlns="http://www.w3.org/2005/Atom" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:gd="http://schemas.google.com/g/2005" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">
 <id>moha****ee@gmail.com</id>
 <updated>2015-08-03T15:12:37.137Z</updated>
 <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
 <title>Mohammad Amin's Contacts</title>
 <link rel="alternate" type="text/html" href="https://www.google.com/"/>
 <link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/mohamma***ee%40gmail.com/full"/>
 <link rel="http://schemas.google.com/g/2005#post" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/mohamm***aee%40gmail.com/full"/>
 <link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/moha****ee%40gmail.com/full/batch"/>
 <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/moham***ee%40gmail.com/full?max-results=25"/>
 <link rel="next" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/moha****aee%40gmail.com/full?max-results=25&amp;start-index=26"/>
 <author>
  <name>Mohammad Amin</name>
  <email>moha****ee@gmail.com</email>
 </author>
 <generator version="1.0" uri="http://www.google.com/m8/feeds">Contacts</generator>
 <openSearch:totalResults>131</openSearch:totalResults>
 <openSearch:startIndex>1</openSearch:startIndex>
 <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
 <entry gd:etag="&quot;SXc5cTNQJit7I2A9XRRbGEsPQQY.&quot;">
  <id>http://www.google.com/m8/feeds/contacts/moh***ee%40gmail.com/base/15281000e768a31</id>
  <updated>2015-04-12T19:07:08.929Z</updated>
  <app:edited xmlns:app="http://www.w3.org/2007/app">2015-04-12T19:07:08.929Z</app:edited>
  <category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact"/>
  <title>Sina Ghazi</title>
  <link rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/moh***aee%40gmail.com/15****a31" gd:etag="&quot;WR1-e34pSit7I2BlWW4TbChNHHg6LF88WhE.&quot;"/>
  <link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/moham****aee%40gmail.com/full/1528****8a31"/>
  <link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/mohamm***ee%40gmail.com/full/15***a31"/>
  <gd:name>
   <gd:fullName>Si***i</gd:fullName>
   <gd:givenName>Si***a</gd:givenName>
   <gd:familyName>G***zi</gd:familyName>
  </gd:name>
  <gd:email rel="http://schemas.google.com/g/2005#home" address="si***i@gmail.com" primary="true"/>
  <gContact:website href="http://www.google.com/profiles/1167****31" rel="profile"/>
 </entry>
.....
但是当我想要获取title属性时,它将返回None。有什么想法吗?

你可以这样尝试:

for item in root.findall('.//{http://www.w3.org/2005/Atom}title'):
    title = item.text
您可以这样尝试:

for item in root.findall('.//{http://www.w3.org/2005/Atom}title'):
    title = item.text

python文档中有一节是关于

您可以使用har07s方式,这种方式工作得非常好,如果不想多次键入整个命名空间,也可以这样做:

ns = {'ns': 'http://www.w3.org/2005/Atom'}

for element in root.findall('.//ns:title', ns):
    title = element.text

python文档中有一节是关于

您可以使用har07s方式,这种方式工作得非常好,如果不想多次键入整个命名空间,也可以这样做:

ns = {'ns': 'http://www.w3.org/2005/Atom'}

for element in root.findall('.//ns:title', ns):
    title = element.text

显示用于提取地址标记的代码。顺便说一句,我找不到任何地址标签,你是说属性吗?是的。你说得对。它是地址属性。XML中没有“title”属性。但是有一个
{http://www.w3.org/2005/Atom}title
元素位于两个位置。显示用于提取地址标记的代码。顺便说一句,我找不到任何地址标签,你是说属性吗?是的。你说得对。它是地址属性。XML中没有“title”属性。但是有一个
{http://www.w3.org/2005/Atom}title
元素位于两个位置。是否有一种方法可以循环浏览文件并按顺序提取每个项目?我不想提取电子邮件和标题。我可以同时做吗?@AminA你可以在
节点上迭代,然后从每个节点提取电子邮件地址和标题。有没有一种方法可以循环浏览文件并按顺序提取每个项目?我不想提取电子邮件和标题。我可以同时做吗?@AminA您可以迭代
节点,然后从每个节点提取电子邮件地址和标题。