2012-01-24 13 views
0

プロファイルのgoogle apisから提供されるXMLフィードを解析しようとしています。 XMLは次のようになります。GoogleプロフィールのXMLフィードの解析

<ns0:feed ns1:etag="W/"Dk8BQ3o8eCt7I2A9WhRUE0g.""> 
<ns0:updated>2012-01-23T21:40:52.470Z</ns0:updated> 
<ns0:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#profile"/> 
<ns0:id>domain.com</ns0:id> 
<ns0:generator uri="http://www.google.com/m8/feeds" version="1.0">Contacts</ns0:generator> 
<ns0:author> 
<ns0:name>domain.com</ns0:name> 
</ns0:author> 
<ns0:link href="http://www.google.com/" rel="alternate" type="text/html"/> 
<ns0:link href="https://www.google.com/m8/feeds/profiles/domain/domain.com/full" rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml"/> 
<ns0:link href="https://www.google.com/m8/feeds/profiles/domain/domain.com/full/batch" rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml"/> 
<ns0:link href="https://www.google.com/m8/feeds/profiles/domain/domain.com/full?max-results=300" rel="self" type="application/atom+xml"/> 
<ns2:startIndex>1</ns2:startIndex> 
<ns2:itemsPerPage>300</ns2:itemsPerPage> 
<ns0:entry ns1:etag=""URRaQR4KTit7I2A4""> 
<ns0:category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#profile"/> 
<ns0:id>http://www.google.com/m8/feeds/profiles/domain/domain.com/full/pname</ns0:id> 
<ns1:name> 
<ns1:familyName>Name</ns1:familyName> 
<ns1:fullName>Persobn Name</ns1:fullName> 
<ns1:givenName>Robert</ns1:givenName> 
</ns1:name> 
<ns0:updated>2012-01-23T21:40:52.597Z</ns0:updated> 
<ns1:organization primary="true" rel="http://schemas.google.com/g/2005#work"> 
<ns1:orgTitle>JobField</ns1:orgTitle> 
<ns1:orgDepartment>DepartmentField</ns1:orgDepartment> 
<ns1:orgName>CompanyField</ns1:orgName> 
</ns1:organization> 
<ns3:status indexed="true"/> 
<ns0:title>Person Name</ns0:title> 
<ns0:link href="https://www.google.com/m8/feeds/photos/profile/domain.com/pname" rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*"/> 
<ns0:link href="https://www.google.com/m8/feeds/profiles/domain/domain.com/full/pname" rel="self" type="application/atom+xml"/> 
<ns0:link href="https://www.google.com/m8/feeds/profiles/domain/domain.com/full/pname" rel="edit" type="application/atom+xml"/> 
<ns1:email address="[email protected]" rel="http://schemas.google.com/g/2005#other"/> 
<ns1:email address="[email protected]" primary="true" rel="http://schemas.google.com/g/2005#other"/> 
<ns4:edited>2012-01-23T21:40:52.597Z</ns4:edited> 
</ns0:entry> 

私は名前だけのフィールドとOfganization NSの下にあるフィールドを必要としています。私の質問はこれを行う正しい方法です。私は前にxmlを解析する必要はありませんでした。要素ツリー、ストーンスープ、サックスなどと言っている人がいます。私はこれまでこれを持っています:

コンソールにはすべての属性名が表示され、コンソールにはすべての名前が表示され、名前またはテキストのあるものはすべて表示されます。私が望むのは、すべての名前と組織のテキストを1行にまとめたものです。私は完全に失われています。どんな助けでも大歓迎です。

+1

あなたはXMLの例をかなり乱しました。あなたはそれを再投稿し、4つのスペース(Ctrl + K)でインデントすることはできますか? –

+0

@larsmans私はそれをきれいにしました – Kevin

+0

それでも無効なXMLです。 –

答えて

0

あなたは、これを手動で行うGoogleのGDATAライブラリを使用する必要はありません。

pip install gdata 

その後:

>>> from gdata.contacts import client 
>>> gd_client = client.ContactsClient(source='YOUR_APPLICATION_NAME', domain='place.com') 
>>> profile = gd_client.GetProfile('https://www.google.com/m8/feeds/profiles/domain/place.com/full/pname') 

より多くの例:http://code.google.com/googleapps/domain/profiles/developers_guide.html(すべての例でのPythonタブ)

+0

私はgdataからファイルを取得していますが、私はファイルとは対照的にフィードを解析できることを知っています。私の質問は、データを取り除く方法のラインに沿っており、フィードを解析したり、データを解析します。 – Kevin

+2

ああ...私はそれが単に文字列を返すPythonのオブジェクトまたは辞書を返すと思った?その後、lxml + xpathはあなたの方法です:) – virhilo

+0

@ virhilo私はオブジェクトとしてそれを解析することができました、私はちょうど私がここにデータを投稿できるようにファイルを作った – Kevin