Home  >  Article  >  Backend Development  >  PHP simplexmlElement operates xml namespace implementation code_PHP tutorial

PHP simplexmlElement operates xml namespace implementation code_PHP tutorial

WBOY
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





Elizabeth Bennet
liz@gmail.com

Contacts
1
1
25

http://www.google.com/m8/feeds/contacts/liz%40gmail.com/base/c9012de
2008-12-10T04:45:03.331Z2008-12-10T04:45:03.331Z
< ;category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/contact/2008#contact' />
Fitzwilliam Darcy

Fitzwilliam Darcy




456





This structure is in the above address. This is the XML data I formatted. Now I want to get something like " 456 ”.

The final code is as follows:
Copy code The code is as follows:

$x = new SimpleXmlElement($str );
foreach($x->entry as $t){
echo $t->id . "
";
echo $t->updated . "< ;br />";
$namespaces = $t->getNameSpaces(true);
$gd = $t->children($namespaces['gd']);
echo $ gd->phoneNumber;
}

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:
Copy code The code is as follows:



Learn QB in Minutes
9

02/12/2009
02/ 12/2009
11
30
NOT_INPROGRESS

PUBLIC< /event:listStatus>


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