Php simplexmlElement: namespace implementation code for operating xml

Source: Internet
Author: User

After reading this question, the first reaction was the relationship between namespace, but I never used simplexml to operate namespace. So I opened the manual and checked the information. The problem was not solved, google eventually solved the problem.

A friend who asked the question posted the data source, from:Copy codeThe Code is as follows: <feed xmlns = 'HTTP: // www.w3.org/2005/Atom' xmlns: openSearch = 'HTTP: // your 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. "'>
Liz@gmail.com
<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 = 'https: // your/>
<Link rel = 'HTTP: // schemas.google.com/g/2005?post' type = 'application/atom + xml' href = 'https: // www.google.com/m8/feeds/contacts/liz?40gmail.com/full'/>
<Link rel = 'HTTP: // schemas.google.com/g/2005?batch' type = 'application/atom + xml' href = 'https: // your/>
<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."'>
Http://www.google.com/m8/feeds/contacts/liz%40gmail.com/base/c9012de
<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 = 'https: // your gd: etag =' "Your." '/>
<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 shown in the preceding address. This is the XML data that I have formatted. Now, we need to obtain the XML data similar to "<gd: phoneNumber rel = 'HTTP: // value in schemas.google.com/g/2005#home'primary = '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 is not written as above, it can also be written 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 );
// Note the difference between this and the above section
$ Gd = $ t-> children ('HTTP: // schemas.google.com/g/2005 ');
Echo $ gd-> phoneNumber;
}

Just like the second method, it is hard-coded. This is not good. In case of any change, you have to change N more codes.
The problem arises one after another, such as the following section: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>

This non-standard XML does not define a namespace. What should I do? In this case, SimpleXmlElement can be solved directly, but it will report warnging because he thinks the namespace event does not exist.
Solution:Copy codeThe Code is as follows: $ xml = @ new SimpleXmlElement ($ str); // Add @ to the front to stop the error.
Echo "<pre> ";
Print_r ($ xml );

Currently, this solution is better.

PHP SimpleXML Functions
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.