简体   繁体   中英

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!

您需要调用xmlFailed.toxml()之类的东西才能从您的对象中获取XML -看起来这不是您在做什么(在您未显示给我们的代码中)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM