2011-10-07 34 views
2

我真的不知道,問題是什麼?我收到以下錯誤:ExpatError:文檔元素後的垃圾

File "C:\Python27\lib\xml\dom\expatbuilder.py", line 223, in parseString 
parser.Parse(string, True) 
ExpatError: junk after document element: line 5, column 0 

我看不到垃圾!任何幫助?我越來越瘋狂......

text = """<questionaire> 
<question> 
    <questiontext>Question1</questiontext> 
    <answer>Your Answer: 99</answer> 
</question> 
<question> 
    <questiontext>Question2</questiontext> 
    <answer>Your Answer: 64</answer> 
</question> 
<question> 
    <questiontext>Question3</questiontext> 
    <answer>Your Answer: 46</answer> 
</question> 
<question> 
    <questiontext>Bitte geben</questiontext> 
    <answer>Your Answer: 544</answer> 
    <answer>Your Answer: 943</answer> 
</question> 
</questionaire>""" 

cleandata = text.split('<questionaire>') 
cleandatastring= "".join(cleandata) 
stripped = cleandatastring.strip() 
planhtml = stripped.split('</questionaire>')[0] 
clean= planhtml.strip() 


from xml.dom import minidom 

doc = minidom.parseString(clean) 
for question in doc.getElementsByTagName('question'): 
    for answer in question.getElementsByTagName('answer'): 
     if answer.childNodes[0].nodeValue.strip() == 'Your Answer: 99': 
      question.parentNode.removeChild(question) 

print doc.toxml() 

Thanx!

+1

不要刪除'問號'標籤! – JBernardo

+0

哈哈爲什麼呢? – Jurudocs

+1

您需要根標籤 – JBernardo

回答

6

您的原始text字符串是格式良好的XML。然後你做了一堆東西來打破它。解析你的原始text,你會沒事的。

XML必須只有一個頂層元素。當你解析它時,它有一些頂級的<question>標籤。 XML解析器將第一個解析爲根元素,然後驚奇地發現另一個頂級元素。

+0

謝謝...你能推薦一個很好的介紹到解析HTML和XML? – Jurudocs

相關問題