See this problem, the first reaction is namespace relationship, but I have never used SimpleXML operation namespace, so opened the manual to check the data, the problem is not resolved, and ultimately through Google to solve the problem.
This structure is in the address above, this is the XML data I formatted, and now I'm going to get a similar "<gd:phonenumber rel= ' http://schemas.google.com/g/2005#home ' primary= ' The value in true ' > 456 </gd:phoneNumber> '.
The final code is as follows:
Copy CodeThe code is as follows:
$x = new SimpleXMLElement ($STR);
foreach ($x->entry as $t) {
Echo $t->id. "<br >";
Echo $t->updated. "<br/>";
$namespaces = $t->getnamespaces (true);
$GD = $t->children ($namespaces [' GD ']);
Echo $GD->phonenumber;
}
Of course, if it's not written like this, it can be written like this:
Copy CodeThe code is as follows:
$x = new SimpleXMLElement ($STR);
foreach ($x->entry as $t) {
Echo $t->id. "<br >";
Echo $t->updated. "<br/>";
$namespaces = $t->getnamespaces (true);
Notice the difference between here and the above paragraph
$GD = $t->children (' http://schemas.google.com/g/2005 ');
Echo $GD->phonenumber;
}
Just like the second way of writing is hard coded, this is not good, in the event of a change in the day, you have to change more than n code.
The problem follows, as in the following paragraph:
Copy CodeThe code is as follows:
<event:event>
<event:sessionKey></event:sessionKey>
<event:sessionname>learn QB in Minutes</event:sessionname>
<event:sessionType>9</event:sessionType>
<event:hostWebExID></event:hostWebExID>
<event:startDate>02/12/2009</event:startDate>
<event:endDate>02/12/2009</event:endDate>
<event:timeZoneID>11</event:timeZoneID>
<event:duration>30</event:duration>
<event:description></event:description>
<event:status>NOT_INPROGRESS</event:status>
<event:panelists></event:panelists>
<event:listStatus>PUBLIC</event:listStatus>
</event:event>
What about this non-standard XML, which does not define namespaces? In this case, in fact simplexmlelement can be solved directly, but will report warnging, because he thought that the event this namespace does not exist.
The solution is:
Copy CodeThe code is as follows:
$xml = @new simplexmlelement ($STR);//precede with @ to suppress errors.
echo "<pre>";
Print_r ($xml);
It seems that this solution is better.
PHP SimpleXML function Related data
Http://www.jb51.net/w3school/php/php_ref_simplexml.htm
PHP SimpleXML
Http://www.jb51.net/w3school/php/php_xml_simplexml.htm
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.