junk after document element: line 13, column 2

13,089

You do not have a single root element. Try adding a parent above each of your book elements. Maybe books?

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

If that does not help try looking at line 13, col 2 like the error message suggests.

bad_line = xmlDocData.splitlines()[12]
Share:
13,089
user2086641
Author by

user2086641

Updated on June 04, 2022

Comments

  • user2086641
    user2086641 almost 2 years

    while parsing xml file,i am getting the following traceback

    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
    

    This is my code

    if request.POST:
            path = "{0}/app_name/filename.xml".format(settings.PROJECT_ROOT)
            xmlDoc = open(path, 'r')
            xmlDocData = xmlDoc.read()
            xmlDocTree = etree.XML(xmlDocData)
    

    below line is giving the error

    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>[email protected]</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>[email protected]</email>
        <age>29</age>
      </book>   
    

    Thanks