PHP simplexmlelement operation XML Namespace implementation code

Source: Internet
Author: User
Tags foreach copy generator xmlns
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.

Ask a question friend posted a data source, from: http://code.google.com/intl/zh-CN/apis/contacts/docs/3.0/developers_guide_protocol.html# Retrieving_without_query, the data structure is roughly as follows:
Copy CodeThe code is as follows:
<feed xmlns= ' http://www.w3.org/2005/Atom ' xmlns:opensearch= ' http://a9.com/-/spec/opensearch/1.1/' xmlns: gcontact= ' http://schemas.google.com/contact/2008 ' xmlns:batch= ' http://schemas.google.com/gdata/batch ' xmlns:gd= ' http://schemas.google.com/g/2005 ' gd:etag= ' w/' cumbrho_fip7ima9wxrbgu0. ' >
<id>liz@gmail.com</id>
<updated>2008-12-10T10:04:15.446Z</updated>
<category scheme= ' http://schemas.google.com/g/2005#kind ' term= ' http://schemas.google.com/contact/2008#contact '/>
<title>elizabeth Bennet ' s contacts</title>
<link rel= ' http://schemas.google.com/g/2005#feed ' type= ' application/atom+xml ' href= ' Feeds/contacts/liz%40gmail.com/full '/>
<link rel= ' http://schemas.google.com/g/2005#post ' type= ' application/atom+xml ' href= ' Feeds/contacts/liz%40gmail.com/full '/>
<link rel= ' http://schemas.google.com/g/2005#batch ' type= ' application/atom+xml ' href= ' /feeds/contacts/liz%40gmail.com/full/batch '/>
<link rel= ' self ' type= ' application/atom+xml ' href= ' https://www.google.com/m8/feeds/contacts/liz%40gmail.com/ Full?max-results=25 '/>
<author>
<name>elizabeth bennet</name>
<email>liz@gmail.com</email>
</author>
<generator version= ' 1.0 ' uri= ' http://www.google.com/m8/feeds ' > Contacts </generator>
<openSearch:totalResults>1</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>25</openSearch:itemsPerPage>
<entry gd:etag= ' "Qn04etvslyp7ima9wxrbgeuoraq." >
<id> http://www.google.com/m8/feeds/contacts/liz%40gmail.com/base/c9012de </id>
<updated>2008-12-10T04:45:03.331Z</updated>
<app:edited xmlns:app= ' Http://www.w3.org/2007/app ' >2008-12-10T04:45:03.331Z</app:edited>
<category scheme= ' http://schemas.google.com/g/2005#kind ' term= ' http://schemas.google.com/contact/2008#contact '/>
<title>fitzwilliam darcy</title>
<gd:name>
<gd:fullname>fitzwilliam darcy</gd:fullname>
</gd:name>
<link rel= ' Http://schemas.google.com/contacts/2008/rel#photo ' type= ' image/* ' href= ' Feeds/photos/media/liz%40gmail.com/c9012de ' gd:etag= ' "Ktlczws1bcp7imbbpv43vuv4lxezcxerzac." '/>
<link rel= ' self ' type= ' application/atom+xml ' href= ' https://www.google.com/m8/feeds/contacts/liz%40gmail.com/ Full/c9012de '/>
<link rel= ' edit ' type= ' application/atom+xml ' href= ' https://www.google.com/m8/feeds/contacts/liz%40gmail.com/ Full/c9012de '/>
<gd:phonenumber rel= ' http://schemas.google.com/g/2005#home ' primary= ' true ' > 456 </gd:phoneNumber>
<gd:extendedproperty name= ' pet ' value= ' hamster '/>
<gcontact:groupmembershipinfo deleted= ' false ' href= ' http://www.google.com/m8/feeds/groups/liz%40gmail.com/ base/270f '/>
</entry>
</feed>

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

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.