Example of the Conversion Function between arrays and XML files implemented by PHP. xml is converted to each other.

Source: Internet
Author: User

Example of the Conversion Function between arrays and XML files implemented by PHP. xml is converted to each other.

This example describes the conversion between arrays and XML files implemented by PHP. We will share this with you for your reference. The details are as follows:

Recently I made a payment. The XML files are returned by the server. Therefore, it is easy to operate only by converting them into arrays. If you do not need to talk about them, directly go to the Code:

1. Convert XML to array

/*** Convert xml into array * @ param string $ xml string or xml file name * @ param bool $ isfile whether the input is an xml file name * @ return array converted array */ function xmlToArray ($ xml, $ isfile = false) {// disable reference of external xml Entity libxml_disable_entity_loader (true); if ($ isfile) {if (! File_exists ($ xml) return false; $ xmlstr = file_get_contents ($ xml);} else {$ xmlstr = $ xml;} $ result = json_decode (json_encode (simplexml_load_string ($ xmlstr, 'simplexmlelement', LIBXML_NOCDATA), true); return $ result ;}

Usage example:

$ XmlDoc = <ETO <books> <book> <author> Jack Herrington </author> <title> PHP Hacks </title> <publisher> O 'Reilly </publisher> </book> <author> Jack Herrington </author> <title> Podcasting Hacks </title> <publisher> O 'Reilly </publisher> </book> <book> <author> XML formatting </author> <title> helper's online tool </title> <publisher> tools.jb51.net </publisher> </book> </books> ETO; $ relarr = xmlToArray ($ xmlDoc); print_r ($ relarr );

Running result:

Array ([book] => Array ([0] => Array ([author] => Jack Herrington [title] => PHP Hacks [publisher] => O 'Reilly) [1] => Array ([author] => Jack Herrington [title] => Podcasting Hacks [publisher] => O 'Reilly) [2] => Array ([author] => XML formatting [title] => online tool for helping customers [publisher] => tools.jb51.net )))

2. Convert arrays to XML

/*** Convert an array to an xml character * @ param string $ xml string **/function arrayToXml ($ data) {if (! Is_array ($ data) | count ($ data) <= 0) {return false;} $ xml = "<xml> "; foreach ($ data as $ key => $ val) {if (is_numeric ($ val) {$ xml. = "<". $ key. "> ". $ val. "</". $ key. ">";} else {$ xml. = "<". $ key. "> <! [CDATA [". $ val. "]> </". $ key. ">" ;}}$ xml. = "</xml>"; return $ xml ;}

Usage example:

$ ArrDoc = array ("author" => "XML formatting", "title" => "", "publisher" => "tools.jb51.net "); $ xmlrel = arrayToXml ($ arrDoc); // running result: <xml> <author> <! [CDATA [XML formatting]> </author> <title> <! [CDATA [online tools for helping customers]> </title> <publisher> <! [CDATA [tools.jb51.net]> </publisher> </xml>

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.