PHP simplexmlelement operation XML Namespace implementation Code _php tutorial

Source: Internet
Author: User
See this problem, the first reaction is namespace relationship, but I never used simplexml operation namespace, so I opened the manual to check the information, the problem is not resolved, and ultimately through Google to solve the problem.

The friend who raised the question posted the 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:

liz@gmail.com
2008-12-10t10:04:15.446z

<title>Elizabeth Bennet ' s Contacts</title>





Elizabeth Bennet
liz@gmail.com

Contacts
1
1
-

http://www.google.com/m8/feeds/contacts/liz%40gmail.com/base/c9012de
2008-12-10t04:45:03.331z
2008-12-10t04:45:03.331z

<title>Fitzwilliam Darcy</title>

Fitzwilliam Darcy




456





This structure is in the above address, this is my formatted XML data, now to get similar to the " 456 The value in the.

The final code is as follows:
Copy CodeThe 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 not written as above, it can be written like this:
Copy CodeThe code is as follows:
$x = new SimpleXMLElement ($STR);
foreach ($x->entry as $t) {
Echo $t->id. "
";
Echo $t->updated. "
";
$namespaces = $t->getnamespaces (true);
Notice the difference between this 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 very good, in case of changes in any day, you have to change the N-Multiple code.
Problems follow, such as the following paragraph:
Copy CodeThe 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 to do? In this case, in fact, SimpleXMLElement has been directly resolved, but will be reported warnging, because he believes that the event namespace does not exist.
The workaround is to:
Copy CodeThe code is as follows:
$xml = @new simplexmlelement ($STR),//At the front Plus @ to suppress the error.
echo "





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

http://www.bkjia.com/PHPjc/322686.html www.bkjia.com true http://www.bkjia.com/PHPjc/322686.html techarticle See this problem, the first reaction is namespace relationship, but I have never used SimpleXML operation namespace, so I opened the manual to check the information, the problem has not been solved, ...

  • 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.