2014-11-27 4 views
0

Я новичок в xml и REST, но имею некоторые базовые знания с помощью python. У меня возникли некоторые проблемы при попытке проанализировать прикрепленный файл xml.проблемы с python xml parsing

Я использую библиотеку Beautifulsoup для анализа файла и по неизвестной причине могу получить доступ к различным полям записей 2 и 3, но не к записи 1, в то время как все они отформатированы одинаково. Может кто-нибудь сказать, что я делаю неправильно с моим (прилагаемым) кодом и вывести пожалуйста? Код

<?xml version='1.0' encoding='UTF-8'?> 
<feed xmlns="http://www.w3.org/2005/Atom"> 
    <title type="text">News</title> 
    <id>1</id> 
    <link href="" /> 
    <link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/entries" rel="self" /> 
    <updated>2014-11-26T10:41:12.424Z</updated> 
    <author /> 
    <entry xmlns:georss="http://www.georss.org/georss"> 
     <title type="html">TEST REST</title> 
     <content type="html">1</content> 
     <author> 
      <name>User213</name> 
     </author> 
     <summary type="html">Test PUT Entry 3</summary> 
     <id>7</id> 
     <georss:point>21.94420760726878 17.44</georss:point> 
     <updated>2014-11-24T09:55:31.000Z</updated> 
     <link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/7" rel="self" type="application/atom+xml" length="0" /> 
     <link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/7/editEntry" rel="edit" type="application/atom+xml" length="0" /> 
     <link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/7/comments" rel="replies" type="application/atom+xml" length="0" /> 
    </entry> 
    <entry xmlns:georss="http://www.georss.org/georss"> 
     <title type="html">TEST REST</title> 
     <content type="html">1</content> 
     <author> 
      <name>User213</name> 
     </author> 
     <summary type="html">Test PUT Entry 8</summary> 
     <id>8</id> 
     <georss:point>21.94420760726878 17.44</georss:point> 
     <updated>2014-11-24T13:47:09.000Z</updated> 
     <link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/8" rel="self" type="application/atom+xml" length="0" /> 
     <link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/8/editEntry" rel="edit" type="application/atom+xml" length="0" /> 
     <link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/8/comments" rel="replies" type="application/atom+xml" length="0" /> 
    </entry> 
    <entry xmlns:georss="http://www.georss.org/georss"> 
     <title type="html">TEST REST</title> 
     <content type="html">1</content> 
     <author> 
      <name>User213</name> 
     </author> 
     <summary type="html">Test POST</summary> 
     <id>12</id> 
     <georss:point>21.94420760726878 17.44</georss:point> 
     <updated>2014-11-25T14:29:02.000Z</updated> 
     <link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/12" rel="self" type="application/atom+xml" length="0" /> 
     <link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/12/editEntry" rel="edit" type="application/atom+xml" length="0" /> 
     <link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/12/comments" rel="replies" type="application/atom+xml" length="0" /> 
    </entry> 
</feed> 

Python:

#!/usr/bin/python 
from BeautifulSoup import BeautifulSoup 
handler = open("/tmp/test.xml").read() 

results = soup.findAll('entry') 
for r in results: 
    print r 
    print r.find('title').text 
    print r.find('content').text 
    print r.find('georss:point') 
    print r.find('id') 
    print r.find('updated') 

И выход заключается в следующем:

<entry xmlns:georss="http://www.georss.org/georss"> 
<title type="html">TEST REST</title> 
<content type="html">1</content> 
</entry> 
TEST REST 
1 
None 
None 
None 
<entry xmlns:georss="http://www.georss.org/georss"> 
<title type="html">TEST REST</title> 
<content type="html">1</content> 
<author> 
<name>User213</name> 
</author> 
<summary type="html">Test PUT Entry 8</summary> 
<id>8</id> 
<georss:point>21.94420760726878 17.44</georss:point> 
<updated>2014-11-24T13:47:09.000Z</updated> 
<link href="http://192.168.20.223:8083/myWebApp/rest/listOfEntries/1/8" rel="self" type="application/atom+xml" length="0" /> 
<link href="http://192.168.20.223:8083/myWebApp/rest/listOfEntries/1/8/editEntry" rel="edit" type="application/atom+xml" length="0" /> 
<link href="http://192.168.20.223:8083/myWebApp/rest/listOfEntries/1/8/comments" rel="replies" type="application/atom+xml" length="0" /> 
</entry> 
TEST REST 
1 
<georss:point>21.94420760726878 17.44</georss:point> 
<id>8</id> 
<updated>2014-11-24T13:47:09.000Z</updated> 
<entry xmlns:georss="http://www.georss.org/georss"> 
<title type="html">TEST REST</title> 
<content type="html">1</content> 
<author> 
<name>User213</name> 
</author> 
<summary type="html">Test POST</summary> 
<id>12</id> 
<georss:point>21.94420760726878 17.44</georss:point> 
<updated>2014-11-25T14:29:02.000Z</updated> 
<link href="http://192.168.20.223:8083/myWebApp/rest/listOfEntries/1/12" rel="self" type="application/atom+xml" length="0" /> 
<link href="http://192.168.20.223:8083/myWebApp/rest/listOfEntries/1/12/editEntry" rel="edit" type="application/atom+xml" length="0" /> 
<link href="http://192.168.20.223:8083/myWebApp/rest/listOfEntries/1/12/comments" rel="replies" type="application/atom+xml" length="0" /> 
</entry> 
TEST REST 
1 
<georss:point>21.94420760726878 17.44</georss:point> 
<id>12</id> 
<updated>2014-11-25T14:29:02.000Z</updated> 

ответ

1

Из того, что я испытал с помощью следующего кода:

#!/usr/bin/python 
from BeautifulSoup import BeautifulSoup 
handler = open("./test.xml").read() 

soup = BeautifulSoup(handler) 
print soup.prettify() 

выводе вот так:

<?xml version='1.0' encoding='utf-8'?> 
<feed xmlns="http://www.w3.org/2005/Atom"> 
<title type="text"> 
    News 
</title> 
<id> 
    1 
</id> 
<link href="" /> 
<link href="http://192.168.1.12:8083/myWebApp/rest/listOfEntries/1/entries" rel="self" /> 
<updated> 
    2014-11-26T10:41:12.424Z 
</updated> 
<author> 
    <entry xmlns:georss="http://www.georss.org/georss"> 
    <title type="html"> 
    TEST REST 
    </title> 
    <content type="html"> 
    1 
    </content> 
    </entry> 
</author> 
<author> 
    <name> 
    User213 
    </name> 
</author> 

Если вы посмотрите внимательно, вы увидите, что в вашем XML-<author /> рассматривается как открытый тег по BeautifulSoup.

Вот почему вы не находите название, содержание ... потому что для него они не теги.

Надеюсь, что это поможет

+0

Большое спасибо. Кажется, это проблема. – radar