3

在解析 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>   

谢谢

4

1 回答 1

11

您没有单个根元素。book尝试在每个元素上方添加父级。也许books

<books>
  <book> ... </book>
  <book> ... </book>
</books>

如果这无助于尝试查看第 13 行,第 2 列,就像错误消息所暗示的那样。

bad_line = xmlDocData.splitlines()[12]
于 2013-04-05T15:30:02.227 回答