PHP simplexmlElement operates xml namespace implementation code_PHP tutorial
WBOYOriginal
2016-07-21 15:33:11795browse
After reading this question, my first reaction was the relationship between namespace, but I had never used simplexml to operate namespace, so I opened the manual and checked the information. The problem was not solved, and finally the problem was solved through Google.
The friend who asked the question posted the data source, which comes from: http://code.google.com/intl/zh-CN/apis/contacts/docs/3.0/developers_guide_protocol.html#retrieving_without_query, data The structure is roughly as follows:
Copy code The code is as follows:
liz@gmail.com 2008-12-10T10:04:15.446Z Elizabeth Bennet's Contacts
Of course, if you don’t write it like the above, you can also write it like this:
Copy the code The code is as follows:
$ x = new SimpleXmlElement($str); foreach($x->entry as $t){ echo $t->id . " "; echo $t ->updated . " "; //$namespaces = $t->getNameSpaces(true); //Note the difference between this and the previous paragraph $gd = $t->children('http://schemas.google.com/g/2005'); echo $gd->phoneNumber; }
Just like The second way of writing is hard coding, which is not very good. If there is a change one day, N more codes will have to be changed. Questions come one after another, such as the following paragraph:
This kind of non-standard XML has no namespace defined. What should I do? In this case, SimpleXmlElement can actually solve it directly, but it will report a warning because it thinks that the event namespace does not exist. The solution is:
Copy the code The code is as follows:
$xml = @new SimpleXmlElement($str); //Add @ in front to suppress errors. echo "
"; print_r($xml);
At present, this solution seems to be better.
PHP SimpleXML function related information http://www.jb51.net/w3school/php/php_ref_simplexml.htm PHP SimpleXML http://www.jb51.net/w3school /php/php_xml_simplexml.htm
http://www.bkjia.com/PHPjc/322686.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322686.htmlTechArticleAfter reading this question, my first reaction was the relationship between namespace, but I have never used simplexml to operate namespace. So I opened the manual and checked the information, but the problem was not solved...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn