Re: Creating a DOM Document from HTTP Request of XML failing
Cris Rys <[email protected]> Sun, 11 Apr 2010 23:44:09 -0700
| Newsgroups | gmane.comp.mozilla.devel.dom |
|---|---|
| Message-ID | <[email protected]> |
On Tue, Apr 6, 2010 at 9:05 AM, Boris Zbarsky <[email protected]> wrote: > On 4/6/10 12:01 PM, Cris wrote: >> >> No headers. That XML was the entire output. I'm guessing this is the >> problem. > > Yep. Either the server needs to claim the output is XML or you need to tell > your XHR to treat the data as XML no matter what the server says. > > -Boris > Thank you Boris. I am successfully able to create the Document. Walking through the document, I am able to GetTagName, successfully, eg I get "first_name", "last_name", "uid", as tag names, but when call element->GetAttribute("value") I get an empty string result. Should I not be querying the element's value? What should I be doing? The XML code is below, and the C++ code that is causing the error is below. Thanks, -cris ------- <?xml version="1.0" encoding="UTF-8"?> <users_getInfo_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://api.facebook.com/1.0/facebook.xsd" list="true"> <user> <about_me xsi:nil="true"/> <first_name>Cris</first_name> <last_name>Rys</last_name> <uid>693893361</uid> </user> </users_getInfo_response> ------- nsCOMPtr<nsIDOMDocument> document; rv = inXMLHttpRequest->GetResponseXML(getter_AddRefs(document)); NS_ENSURE_SUCCESS(rv, rv); NS_ENSURE_TRUE(document, NS_ERROR_FAILURE); nsCOMPtr<nsIDOMNodeList> userNodeList; rv = document->GetElementsByTagName(NS_LITERAL_STRING("user"), getter_AddRefs(userNodeList)); NS_ENSURE_STATE(userNodeList); NS_ENSURE_SUCCESS(rv, rv); PRUint32 userNodeListLength = 0; rv = userNodeList->GetLength(&userNodeListLength); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIDOMNode> userNode; rv = userNodeList->Item(0, getter_AddRefs(userNode)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIDOMNodeList> childNodes; rv = userNode->GetChildNodes(getter_AddRefs(childNodes)); NS_ENSURE_SUCCESS(rv, rv); PRUint32 childNodeListLength = 0; rv = childNodes->GetLength(&childNodeListLength); NS_ENSURE_SUCCESS(rv, rv); nsString first_name, last_name, pic_square,profile_url, uidStr; uint64_t uid = 0; for(PRUint32 i = 0; i < childNodeListLength; ++i) { nsCOMPtr<nsIDOMNode> domNode; rv = childNodes->Item(i, getter_AddRefs(domNode)); NS_ENSURE_SUCCESS(rv, rv); nsCOMPtr<nsIDOMElement> element = do_QueryInterface(domNode, &rv); if(NS_FAILED(rv)) { continue; } nsString tagName; rv = element->GetTagName(tagName); // successfully gets names of "first_name", "last_name", "uid" NS_ENSURE_SUCCESS(rv, rv); nsString value; rv = element->GetAttribute(NS_LITERAL_STRING("value"), value); // but attribute value is an empty string if(NS_FAILED(rv)) { continue; } if(tagName.EqualsLiteral("first_name")) { first_name = value; } else if(tagName.EqualsLiteral("last_name")) { last_name = value; } else if(tagName.EqualsLiteral("uid")) { uidStr = value; } }