How can I parse a complex XML with PHP and CDATA?

XML and & Symbol in ASP

  • I'm having problems with an XML feed. I use the below ASP code to download the feed: Set objSrvHTTP = Server.CreateObject ("MSXML2.ServerXMLHTTP") objSrvHTTP.open "GET","http://www.website.com?getfeed=1", false objSrvHTTP.send if objSrvHTTP.responseXML.getElementsByTagName("ErrCount").item(0).text <> 0 then the feed looks like this: <?xml version="1.0"?> <interface-response> <host> <name>@</name> <type>FRAME</type> <address>http://www.website.com?x=1&y=2</address> </host> <ErrCount>0</ErrCount> <debug><![CDATA[]]></debug> </interface-response> The problem is with the <address> data. When that data is a full url with parameters (with a "&y=2") I get an error. The error looks like this: Microsoft VBScript runtime (0x800A01A8) Object required: '[object]' The line at which the error occurs is the "if error = 0 then" line. If the <address> field does not have a "&" and looks just like "http://www.website.com?x=1" then I have no problems. My question is...why is that "&" causing an error when I try to parse my XML? Also, if you can answer how to keep it from happening, that would be greatly appreciated (think "tip" ;-). Please ask any questions if you have them, thank you. As always, rating will be give. xemion-ga

  • Answer:

    As the ampersand is used to signify the encoding of an entity (ie escaped character) it needs to be, itself, encoded as an entity. Thus, the url address will need to be entered as: http://www.website.com?x=1&amp;y=2 wherein the ampersand in the URL is replaced with &amp; In order to ensure that this encoding comes across your way accurately and is not interpreted by the browsers, I'm going to post this with spaces between each character: & a m p ; There are a total of five predefined entities available in XML, further explained and listed here: http://msxml.com/intro_xml/entities.html --- In some articles on the subject it is recommended, for interoperability with SGML and other mark-up languages, that the entity name be replaced with &#38;#38; XML Entities and their Applications (in the subsection of "Predefined Entities") http://tech.irt.org/articles/js212/#character_references Let me know if this fully answers your question and feel free to ask for any further clarifications. Thanks for using Google Answers, Dave Bug

xemion-ga at Google Answers Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.