How php parses xml files

Source: Internet
Author: User
The template package has been imported recently. The template package contains xml files and needs to be parsed into a given php array format. I have come across two methods, namely the DOMDocument method and simple_load_file. personal preference. This is simple. The following describes the usage of $ template_config_pathmodule_1.xml; functionpar

The template package has been imported recently. The template package contains xml files and needs to be parsed into a given php array format. I have come across two methods, namely the DOMDocument method and simple_load_file. personal preference. This is simple. The following describes the usage of $ template_config_path = 'module _ 1. xml'; function par

The template package has been imported recently. The template package contains xml files and needs to be parsed into a given php array format.

I have come across two methods, namely the DOMDocument method and simple_load_file. personal preference. This is simple.

The usage is as follows:

$template_config_path = 'module_1.xml';function parseXMLConfig($template_config_path){$myxml = simplexml_load_file($template_config_path);return xmlToArray($myxml);}function xmlToArray($xml){$arr_xml = (array)$xml;foreach($arr_xml as $key=>$item){if(is_object($item) || is_array($item)){$arr_xml[$key] = xmlToArray($item);}}return $arr_xml;}$test = parseXMLConfig($template_config_path);print_r($test);

In fact, the DOMDocument method is to convert xml into a dom object and then perform related operations using the methods provided by dom, such as getElementById.

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.