2012-03-22 15 views
0

djangoでlxml.etree.parseを使用しています。外部のRSSフィードのコンテンツを解析し、findallを使用して名前空間を回避します。findtextとxpathは、解析されたxml文書から何も返さない

私は結果を反復することができますが、結果からテキストを表示することはできません。ここで

は、xmlファイルは、私がからこすりしようとしていますことを次のようになります。ここでは

<feed xmlns="http://www.w3.org/2005/Atom"> 
    <title>Open Library : Author Name</title> 
    <link href="http://www.somedomain.org/people/atom/author_name" rel="self"/> 
    <updated>2012-03-20T16:41:00Z</updated> 
    <author> 
     <name>somedomain.org</name> 
    </author> 
    <id>tag:somedomain.org,2007:/person_feed/123456</id> 
    <entry> 
     <link href="http://www.somedomain.org/roll_call/show/1234" rel="alternate"/> 
     <id> 
     tag:somedomain.org,2012-03-20:/roll_call_vote/1234 
     </id> 
     <updated>2012-03-20T16:41:00Z</updated> 
     <title>Once upon a time</title> 
     <content type="html"> 
     This is a book full of words 
     </content> 
    </entry> 
</feed> 

はジャンゴでの私の見解は次のようになります。

def openauthors(request): 

    tree = lxml.etree.parse("http://www.somedomain.org/people/atom/author_name") 
    namespace = "{http://www.w3.org/2005/Atom}" 
    listings = tree.findall("{http://www.w3.org/2005/Atom}entry") 

    listings_info = [] 

    for listing in listings: 
     this_value = { 
      "link": listing.findtext("content"), 
      "title": listing.findtext("feed/content"), 
      "content": listing.findtext("content"), 
      } 

     listings_info.append(this_value) 


    json_listings = '{"listings":' + simplejson.dumps(listings_info) + '}' 

    if("callback" in request.GET.keys()): 
     callback = request.GET["callback"] 
    else: 
     callback = None 

    if(callback): 
     response = HttpResponse("%s(%s)" % (
       callback, 
       simplejson.dumps(listings_info) 
       ), mimetype="application/json" 
      ) 
    else: 
     response = HttpResponse(json_listings, mimetype="application/json") 
    return response 

私はまた、次のことを試してみましたfindtextの代わりにxpathを使用していますが、同じ結果が得られています。

"link":listing.xpath("link/text()"), 
    "title":listing.xpath("entry/link/text()"), 
    "content":listing.xpath("content/text()"), 

助けてください。

答えて

1

あなたはXML名前空間を考慮しません。

tree = lxml.etree.parse("http://www.somedomain.org/people/atom/author_name") 
xmlns = {"atom": "http://www.w3.org/2005/Atom"} 
listings = tree.xpath("//atom:entry", namespaces=xmlns) 

listings_info = [] 

for listing in listings: 
    listings_info.append({ 
     "link": listing.xpath("./atom:link/@href", namespaces=xmlns), 
     "title": listing.xpath("./atom:title", namespaces=xmlns), 
     "content": listing.xpath("./atom:content", namespaces=xmlns), 
    }) 

接頭辞を定義し(XMLには何もない場合でも)、XPath式で使用する必要があります。つまり、どのプレフィックスをどの名前空間に使用するかについて、.xpath()に通知する必要があります。つまり、2番目のパラメータです。

+0

ほとんどの場合、私は '/ text()'から '' title ''に注意しなければなりませんでした:listing.xpath( "./ atom:title"、namespaces = xmlns) :listing.xpath( "./ atom:title/text()"、namespaces = xmlns)、およびコンテンツ用のものです。ありがとう! – bigmike7801

+0

@ bigmike7801そうですね。要素やその内容が必要かどうかはわかりませんでした。 – Tomalak

関連する問題