PhpsimplexmlElement: namespace implementation code for operating xml

Source: Internet
Author: User
This is what happened at noon today. someone asks for help in the group. for example, if the tag in xml is & lt; xx: xxxx & gt; content & lt; xx: xxxx & gt; in this case, the value of xx: xxxx is not obtained. 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:

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.331Z
2008-12-10T04: 45: 03.331Z

Fitzwilliam Darcy

Fitzwilliam Darcy




456





This structure is shown in the preceding address. this is the XML data that I have formatted. 456 .

The final code is as follows:

The code is as follows:


$ X = new SimpleXmlElement ($ str );
Foreach ($ x-> entry as $ t ){
Echo $ t-> id ."
";
Echo $ t-> updated ."
";
$ 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:

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 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:

The code is as follows:




Learn QB in Minutes
9

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

NOT_INPROGRESS

PUBLIC


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:

The code is as follows:


$ Xml = @ new SimpleXmlElement ($ str); // add @ to the front to stop the error.
Echo"

";
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.