Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to parse xml data with colon (:) from response using getNamespaces()
    primarykey
    data
    text
    <p>I want to read whatever is inside the <code>&lt;q:content&gt;&lt;/q:content&gt;</code> tags in the following xml -</p> <pre><code>$xml = '&lt;?xml version="1.0"?&gt; &lt;q:response xmlns:q="http://api-url"&gt; &lt;q:impression&gt; &lt;q:content&gt; &lt;html&gt; &lt;head&gt; &lt;meta name="HandheldFriendly" content="True"&gt; &lt;meta name="viewport" content="width=device-width, user-scalable=no"&gt; &lt;meta http-equiv="cleartype" content="on"&gt; &lt;/head&gt; &lt;body style="margin:0px;padding:0px;"&gt; &lt;iframe scrolling="no" src="http://some-url" width="320px" height="50px" style="border:none;"&gt;&lt;/iframe&gt; &lt;/body&gt; &lt;/html&gt; &lt;/q:content&gt; &lt;q:cpc&gt;0.02&lt;/q:cpc&gt; &lt;/q:impression&gt; ... ... some more things ... &lt;/q:response&gt;'; </code></pre> <p>I have put the xml in the variable above and then I use <a href="http://www.php.net/manual/en/domdocument.getelementsbytagnamens.php" rel="nofollow noreferrer">SimpleXMLElement::getNamespaces</a> as given in the section "Example #1 Get document namespaces in use" - </p> <pre><code>//code continued $dom = new DOMDocument; // load the XML string defined above $dom-&gt;loadXML($xml); var_dump($dom-&gt;getElementsByTagNameNS('http://api-url', '*') ); // shows object(DOMNodeList)#3 (0) { } foreach ($dom-&gt;getElementsByTagNameNS('http://api-url', '*') as $element) { //this does not execute echo 'see - local name: ', $element-&gt;localName, ', prefix: ', $element-&gt;prefix, "\n"; } </code></pre> <p>But the code inside the for loop does not execute.</p> <p>I have read these questions - </p> <ul> <li><p><a href="https://stackoverflow.com/questions/5801089/php-script-cant-read-xml-data-with-colon">php script cant read xml data with colon (:)</a></p></li> <li><p><a href="https://stackoverflow.com/questions/3130605/how-read-abc-xyz-xml-tag-using-php">How read &lt; abc: xyz &gt; xml tag using php?</a></p></li> </ul> <p><strong>Update</strong><br> Also tried this solution <a href="https://stackoverflow.com/questions/595946/parse-xml-with-namespace-using-simplexml/635907#635907">Parse XML with Namespace using SimpleXML</a> - </p> <pre><code>$xml = new SimpleXMLElement($xml); $xml-&gt;registerXPathNamespace('e', 'http://api-url'); foreach($xml-&gt;xpath('//e:q') as $event) { echo "not coming here"; $event-&gt;registerXPathNamespace('e', 'http://api-url'); var_export($event-&gt;xpath('//e:content')); } </code></pre> <p>In this case too, the code inside the foreach does not execute. Not sure if I wrote everything correct ...</p> <p><strong>Further Update</strong><br> Going with the first solution ... with error_reporting = -1, found that the problem is with the URL in the <code>src</code> attr of the <code>iframe</code> tag. Getting warnings like - </p> <pre><code>Warning: DOMDocument::loadXML(): EntityRef: expecting ';' in Entity, line: 13 </code></pre> <p>Updated code - </p> <pre><code>$xml = '&lt;?xml version="1.0"?&gt; &lt;q:response xmlns:q="http://api-url"&gt; &lt;q:impression&gt; &lt;q:content&gt; &lt;html&gt; &lt;head&gt; &lt;meta name="HandheldFriendly" content="True" /&gt; &lt;meta name="viewport" content="width=device-width, user-scalable=no" /&gt; &lt;meta http-equiv="cleartype" content="on" /&gt; &lt;/head&gt; &lt;body style="margin:0px;padding:0px;"&gt; &lt;iframe scrolling="no" src="http://serve.qriously.com/v1/request?type=SERVE&amp;aid=ratingtest&amp;at=2&amp;uid=0000000000000000&amp;noHash=true&amp;testmode=true&amp;ua=Mozilla/5.0 (Linux; U; Android 2.2.1; en-us; Nexus One Build/FRG83) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1&amp;appid=12e2561f048158249e30000012e256826ad&amp;pv=2&amp;rf=2&amp;src=admarvel&amp;type=get&amp;lang=eng" width="320px" height="50px" style="border:none;"&gt;&lt;/iframe&gt; &lt;/body&gt; &lt;/html&gt; &lt;/q:content&gt; &lt;q:cpc&gt;0.02&lt;/q:cpc&gt; &lt;/q:impression&gt; &lt;q:app_stats&gt; &lt;q:total&gt;&lt;q:ctr&gt;0.023809523809523808&lt;/q:ctr&gt;&lt;q:ecpm&gt;0.5952380952380952&lt;/q:ecpm&gt;&lt;/q:total&gt; &lt;q:today&gt;&lt;q:ctr&gt;0.043478260869565216&lt;/q:ctr&gt;&lt;q:ecpm&gt;1.0869565217391306&lt;/q:ecpm&gt;&lt;/q:today&gt; &lt;/q:app_stats&gt; &lt;/q:response&gt;'; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload