Example of using DOM to store XML data into arrays in PHP: domxml

Source: Internet
Author: User

Example of using DOM to store XML data into arrays in PHP: domxml

This example describes how to use DOM to store XML data into arrays in PHP. We will share this with you for your reference. The details are as follows:

<? Php $ doc = new DOMDocument ('1. 0 ', 'utf-8'); $ doc-> load ("config. xml "); $ roots = $ doc-> documentElement; // get the root node, that is, config (only one) $ childs = $ roots-> childNodes; // obtain all the child nodes under the root node, namely db smartyfor ($ I = 0; $ I <$ childs-> length; $ I ++) {// cyclically store the array $ config_item = $ childs-> item ($ I) according to the number of subnodes under the root node ); // obtain the db smarty $ configs [$ config_item-> nodeName] = array (); // store the names of the db smarty subnodes as data in the array $ items = $ config_item-> childNodes; // obtain all child nodes in the database smarty for ($ j = 0; $ j <$ items-> length; $ j ++) {// cyclically store the names and values of child nodes in db smarty into two-dimensional arrays according to the number of child nodes in db smarty $ item = $ items-> item ($ j ); // cyclically obtain the subnode $ configs [$ config_item-> nodeName] [$ item-> nodeName] = $ item-> nodeValue;} under each db smarty ;} // writing a two-dimensional array} var_dump ($ configs );

PS: Here are some online tools for xml operations for your reference:

Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson

Online formatting XML/online compression XML:
Http://tools.jb51.net/code/xmlformat

XMLOnline compression/formatting tools:
Http://tools.jb51.net/code/xml_format_compress

XMLCode Online formatting and beautification tools:
Http://tools.jb51.net/code/xmlcodeformat

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.