How to use simplexml to obtain the node name in PHP-php Tutorial

Source: Internet
Author: User
How to use simplexml to get the node name in php I tried to use phpsimplexml's getName () to get the node name, but it is not easy to know why please take a look at the content of the xml file as follows: & lt; root & gt; & nbsp; & lt; collectionid & quot; new1 & quot; & gt; & nbsp; & lt; category & g PHP how to get the node name using simple xml
I tried to use the getName () of php simple xml to get the node name, but I am not sure why can't I help you?

The content of the xml file is roughly as follows:


Aaa


Php code:
$ Xmldoc = simplexml_load_file ("test. xml ");

Foreach ($ xmldoc-> children () as $ child ){

Foreach ($ child-> attributes () as $ a => $ B ){
Echo $ B; // Print the property value of the collection here. it works normally.

}
Echo $ child-> getName (); // an error occurs when you try to name the node.

}
The normal result is new1 collection, but if new1 is available, the collection cannot be created. it seems that the page still has an error and I cannot find the cause. Is getName () incorrect?


------ Solution --------------------
No problem

It may be related to your php version.
------ Solution --------------------
Conversion between XML and PHP encapsulation function: xmlparse. php
PHP code
    
    
   
    
Aaa
     
  XML; require (dirname (_ FILE __). "/xmlparse. php "); $ cont = parse_xml_config ($ contxml," root "); $ keys = array_keys ($ cont); $ key1 = array_keys ($ cont ['collect']); print_r ($ cont); # Array ([collection] => Array ([category] => aaa) [encoding] => UTF-8) print_r ($ keys ); # Array ([0] => collection [1] => encoding) print_r ($ key1); # Array ([0] => category)?>
------ Solution --------------------
Php5.3 Wood problems

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.