Search code examples
pythonxmldjangominidom

xml.dom.minidom Document() in Python/django outputting memory location


I'm learning Python and django at the same time. I'm trying to create an xml document to return some XML from a view. I'm using the django development server at the moment and I keep getting this information spitting out in my views instead of the document I tried to create.

Here's my code

    from django.http import HttpResponse
    from mypoject.myapp.models import Username
    from django.core import serializers
    from xml.dom.minidom import Document
    import datetime


    def authenticate(request, username):
        if request.method == "GET":

            #Try to get the username
            try:
                checkUser = Username.objects.get(username__exact = username)
                user = userCheck.get(username__exact = username)
                userXML = serializers.serialize("xml", checkUser)

            except Username.DoesNotExist:
                #return XML with status "Failed"
                return HttpResponse(xml, mimetype="text/xml")       
            except:
                #return XML with status "Failed"

                xmlFailed = Document()

                meta = xmlFailed.createElement("meta")
                xmlFailed.appendChild(meta)

                status = xmlFailed.createElement("status")
                meta.appendChild(status)
                statusText = xmlFailed.createTextNode("Failed")
                status.appendChild(statusText)

                message = xmlFailed.createElement("message")
                meta.appendChild(message)

                totalRecords = xmlFailed.createElement("totalRecords")
                meta.appendChild(totalRecords)

                executionTime = xmlFailed.createElement("executionTime")
                meta.appendChild(executionTime)

                return HttpResponse(xmlFailed, mimetype="text/xml")
            else:
                #return happy XML code with status "Success"

And here's what's going to the screen when I view it in my browser...

<xml.dom.minidom.Document instance at 0x993192c>

If I comment out the Document() creation that goes away. So I'm think I just need it to not spit out the information. I've been searching all over and I can't find a strait answer which leads me to believe I'm missing something blatantly obvious.

Thanks for any help!


Solution

  • You'll need to call xmlFailed.toxml() or the like in order to get XML out of your object -- looks like that's not what you're doing (in the code you didn't show us).