PhpsimplexmlElement: namespace implementation code for operating xml

Source: Internet
Author: User
This is what happened at noon today. Someone asked for help in the group. For example, if the xml file is marked as xx: xxxxcontentxx: xxxx, xx: xxxx cannot be taken as the underlying value.

This is what happened at noon today. Someone asked for help in the group. For example, if the xml file is marked as xx: xxxxcontent/xx: xxxx, xx: xxxx cannot be taken as the underlying value.

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: # retrieving_without_query. The data structure is roughly as follows:

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

% 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, the server space, the Hong Kong server, and the U.S. server can also be written as follows if it is not as described above:

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

PHP SimpleXML

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.