Converting an array to an XML file

Source: Internet
Author: User
Tags cdata
This time for you to bring the array and XML files to each other, the array and XML files to convert each other considerations are what, the following is the actual case, take a look.

Recently paid, the server returned is an XML file, so you need to convert an array, it will be easy to operate, well, not much to say, directly on the code:

1. XML-to-array

/** * Convert XML to array * @param string  $xml XML string or XML file name * @param bool   $isfile whether the XML file name was passed in @return array  conversion. Array */function xmltoarray ($xml, $isfile =false) {  //disallow referencing of external XML entities  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 examples:

$xmlDoc =<<<eto<books> <book> <author>jack herrington</author> <title>php Hacks</title> <publisher>o ' reilly</publisher> </book> <book> <author>jack herrington</author> <title>podcasting hacks</title> <publisher>o ' Reilly</publisher> </book> <book> <author>xml formatting </author> <title> scripting Home online tools </title> <publisher >tools.jb51.net</publisher> </book></books>ETO; $relarr =xmltoarray ($XMLDOC);p Rint_r ($relarr );

Operation 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 format C17/>[title] + Script Home online tool          [Publisher] = tools.jb51.net)        )    )

2. Array to XML

/** * Array to XML character * @param string  $xml 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 examples:

$arrDoc = Array ("Author" = "XML format", "title" and "Script home online Tool", "publisher" and "Tools.jb51.net"); $xmlrel = Arraytoxml ($ARRDOC);//Run Result: <xml><author><! [Cdata[xml formatted]]></author><title><! [cdata[Script's home online tool]]></title><publisher><! [cdata[tools.jb51.net]]></publisher></xml>

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Vue Making picture Carousel

JS gets the value within the first element in the Select drop-down box

Installation electron failure how to deal with

Related Article

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.