在解析xml文件时,我得到以下回溯
ParseError at /addxml/
junk after document element: line 13, column 2
Request Method: POST
Request URL: http://localhost:8000/addxml/
Django Version: 1.3.7
Exception Type: ParseError
Exception Value:
junk after document element: line 13, column 2
Exception Location: /root/Samples/DemoApp/DemoApp/views.py in addxml, line 98
Python Executable: /usr/bin/python
这是我的代码
if request.POST:
path = "{0}/app_name/filename.xml".format(settings.PROJECT_ROOT)
xmlDoc = open(path, 'r')
xmlDocData = xmlDoc.read()
xmlDocTree = etree.XML(xmlDocData)
以下行正在给出错误
xmlDocTree = etree.XML(xmlDocData)
<book>
<book_id>101</book_id>
<book_name>Python</book_name>
<publisher_name>Maxwell</publisher_name>
<author_id>1002</author_id>
<first_name>John</first_name>
<last_name>Dezosa</last_name>
<email>john@gmail.com</email>
<age>34</age>
</book>
<book>
<book_id>102</book_id>
<book_name>Django</book_name>
<publisher_name>Technical</publisher_name>
<author_id>1003</author_id>
<first_name>Josep</first_name>
<last_name>Raj</last_name>
<email>joseph@gmail.com</email>
<age>29</age>
</book>
由于
答案 0 :(得分:11)
您没有单个根元素。尝试在每个book
元素上方添加父级。也许books
?
<books>
<book> ... </book>
<book> ... </book>
</books>
如果这样做没有帮助,请尝试查看第13行,第2列就像错误消息所示。
bad_line = xmlDocData.splitlines()[12]