0

Hi I'm trying to get the xml data from php to javascript using DOM document and it keeps giving me : XML or text declaration not at start of entity error.(on firebug) I tried saving it to a real xml file and it outputs a well formatted xml.

I guess the reason for this is white space on top of the xml file. I tried using ob_start(); and ob_end_flush(); but couldn't get that working BTW I'm new to Ajax!

JS script


var xh = createRequest();
var xhrObject = false;

if (window.XMLHttpRequest)
{ 
    xHRObject = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{ 
    xHRObject = new ActiveXObject("Microsoft.XMLHTTP");
}




function getData()
{   

    if ((xh.readyState == 4) &&(xh.status == 200))
        {   
            alert("hi");
            var serverResponse = xh.responseXML;
            var header = serverResponse.getElementsByTagName("good");

            var spantag = document.getElementById("sp");
            var x;
            spantag.innerHTML = "";
            x = "<table cellpadding='1' cellspacing='6' border='0'>";
            x += "<tr><td>ID</td><td>price</td><td>quantity</td><td>Total</td><td>Remove</td></tr>";
            for (i=0; i<header.length; i++)
            {  
                var id =  header[i].getElementsByTagName("ID")[0].childNodes[0].nodeValue;
                var price =  header[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;
                var qty =  header[i].getElementsByTagName("quantity")[0].childNodes[0].nodeValue;
                var total =  header[i].getElementsByTagName("total")[0].childNodes[0].nodeValue;


                if(qty=="0")
                {
                    continue;
                }
                x += "<tr>"
                + "<td>" + id + "</td>"
                + "<td>" + price + "</td>"
                + "<td>" + qty + "</td>"
                + "<td>" + total + "</td>"
                + "<td>" + "<a href='#' onclick='AddRemoveItem(\"Remove\","+id+");'>Remove Item</a>" + "</td>"
                + "</tr>";

            }
            x += "</table>";
            if (header.length != 0)
                spantag.innerHTML = x;
        }
}

PHP code

function toXml($shop_goods)
    {   
        $doc = new DomDocument();   

        $goods = $doc->createElement('goods');
        $goods = $doc->appendChild($goods);

        foreach ($shop_goods as $Item => $ItemName)
        { 

            $good = $doc->createElement('good');
            $good = $goods->appendChild($good);


            $title = $doc->createElement('ID'); 
            $title = $good->appendChild($title);
            $value = $doc->createTextNode($Item);
            $value = $title->appendChild($value);

            $price = $doc->createElement('price');
            $price = $good->appendChild($price);
            $value3 = $doc->createTextNode($ItemName["price"]);
            $value3 = $price->appendChild($value3);

            $quantity = $doc->createElement('quantity');
            $quantity = $good->appendChild($quantity);
            $value2 = $doc->createTextNode($ItemName["qty"]);
            $value2 = $quantity->appendChild($value2);

            $total = $doc->createElement('total');
            $total = $good->appendChild($total);
            $value3 = $doc->createTextNode($ItemName["total"]);
            $value3 = $total->appendChild($value3);


        }
        $strXml = $doc->saveXML();


        //echo("<br>----- DATA RECOREDED!!! ----");


        return $strXml;

    }
4
  • 1
    Might be an encoding issue. Does your server send the document in UTF-8 (or any other multibyte-encoding)? If so, make sure there is NO byte-order-mark. Commented Nov 1, 2013 at 21:52
  • Check if there is other response than the xml. Also try setting the header Content-type in php to text/xml before response/echo. Commented Nov 1, 2013 at 21:58
  • Any suggestions on how to remove byte order mark? Commented Nov 2, 2013 at 0:00
  • @Ayesha Amarasinghe: My suggestion is to not create it in the first place so you don't need to remove it. Have you verified you've got a BOM there? Not that you do something too much. BOMs can be removed with XMLRecoder. Commented Nov 2, 2013 at 10:52

1 Answer 1

1

As others have noted, this could be an encoding issue.

Check your code for the Xml byte order mark.

Check the document is returning the correct mime type.

Not sure of your constraints but have you considered serializing your data to Json format. It's much easier to work with in the browser.

Finally consider using a library like JQuery to handle your ajax requests, much easier for cross browser compatibility.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your recommendation. I was able to save all the other xml files successfully but not for this one! I'm a fresher and don't know how to Check the document is returning the correct mime type and stuff...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.