I want to read whatever is inside the <q:content></q:content>
tags in the following xml -
$xml = '<?xml version="1.0"?>
<q:response xmlns:q="http://api-url">
<q:impression>
<q:content>
<html>
<head>
<meta name="HandheldFriendly" content="True">
<meta name="viewport" content="width=device-width, user-scalable=no">
<meta http-equiv="cleartype" content="on">
</head>
<body style="margin:0px;padding:0px;">
<iframe scrolling="no" src="http://some-url" width="320px" height="50px" style="border:none;"></iframe>
</body>
</html>
</q:content>
<q:cpc>0.02</q:cpc>
</q:impression>
...
... some more things
...
</q:response>';
I have put the xml in the variable above and then I use SimpleXMLElement::getNamespaces as given in the section "Example #1 Get document namespaces in use" -
//code continued
$dom = new DOMDocument;
// load the XML string defined above
$dom->loadXML($xml);
var_dump($dom->getElementsByTagNameNS('http://api-url', '*') ); // shows object(DOMNodeList)#3 (0) { }
foreach ($dom->getElementsByTagNameNS('http://api-url', '*') as $element)
{
//this does not execute
echo 'see - local name: ', $element->localName, ', prefix: ', $element->prefix, "\n";
}
But the code inside the for loop does not execute.
I have read these questions -
php script cant read xml data with colon (:)
How read < abc: xyz > xml tag using php?
Update
Also tried this solution Parse XML with Namespace using SimpleXML -
$xml = new SimpleXMLElement($xml);
$xml->registerXPathNamespace('e', 'http://api-url');
foreach($xml->xpath('//e:q') as $event) {
echo "not coming here";
$event->registerXPathNamespace('e', 'http://api-url');
var_export($event->xpath('//e:content'));
}
In this case too, the code inside the foreach does not execute. Not sure if I wrote everything correct ...
Further Update
Going with the first solution ... with error_reporting = -1, found that the problem is with the URL in the src
attr of the iframe
tag. Getting warnings like -
Warning: DOMDocument::loadXML(): EntityRef: expecting ';' in Entity, line: 13
Updated code -
$xml = '<?xml version="1.0"?>
<q:response xmlns:q="http://api-url">
<q:impression>
<q:content>
<html>
<head>
<meta name="HandheldFriendly" content="True" />
<meta name="viewport" content="width=device-width, user-scalable=no" />
<meta http-equiv="cleartype" content="on" />
</head>
<body style="margin:0px;padding:0px;">
<iframe scrolling="no" src="http://serve.qriously.com/v1/request?type=SERVE&aid=ratingtest&at=2&uid=0000000000000000&noHash=true&testmode=true&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&appid=12e2561f048158249e30000012e256826ad&pv=2&rf=2&src=admarvel&type=get&lang=eng" width="320px" height="50px" style="border:none;"></iframe>
</body>
</html>
</q:content>
<q:cpc>0.02</q:cpc>
</q:impression>
<q:app_stats>
<q:total><q:ctr>0.023809523809523808</q:ctr><q:ecpm>0.5952380952380952</q:ecpm></q:total>
<q:today><q:ctr>0.043478260869565216</q:ctr><q:ecpm>1.0869565217391306</q:ecpm></q:today>
</q:app_stats>
</q:response>';