Home PHP Libraries Other libraries PHP data format and XML conversion class
PHP data format and XML conversion class
<?php
function xml2array($contents, $get_attributes = 1, $priority = 'tag') {
  if (!$contents) return array();
  if (!function_exists('xml_parser_create')) {
    // print "'xml_parser_create()' function not found!";
    return array();
  }
  // Get the XML parser of PHP - PHP must have this module for the parser to work
  $parser = xml_parser_create('');
  xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); // http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
  xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  xml_parse_into_struct($parser, trim($contents), $xml_values);
  xml_parser_free($parser);
  if (!$xml_values) return; //Hmm...
  // Initializations
  $xml_array = array();
  $parents = array();
  $opened_tags = array();
  $arr = array();
  $current = &$xml_array; //Refference
  // Go through the tags.
  $repeated_tag_index = array(); //Multiple tags with same name will be turned into an array
  foreach($xml_values as $data) {
    unset($attributes, $value); //Remove existing values, or there will be trouble
    // This command will extract these variables into the foreach scope
    // tag(string), type(string), level(int), attributes(array).
    extract($data); //We could use the array by itself, but this cooler.
    $result = array();
    $attributes_data = array();
    if (isset($value)) {
      if ($priority == 'tag') $result = $value;
      else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
    }

This is a class library that can convert between XML and data formats. Friends who need it can download and use it.

Disclaimer

All resources on this site are contributed by netizens or reprinted by major download sites. Please check the integrity of the software yourself! All resources on this site are for learning reference only. Please do not use them for commercial purposes. Otherwise, you will be responsible for all consequences! If there is any infringement, please contact us to delete it. Contact information: admin@php.cn

Related Article

PHP realizes XML and data format conversion class example_php skills PHP realizes XML and data format conversion class example_php skills

16 May 2016

This article mainly introduces the PHP implementation of XML and data format conversion classes. It analyzes the method of PHP to convert XML format data with examples. It has certain reference value. Friends in need can refer to it.

PHP implements XML and data format conversion class examples, xml examples_PHP tutorial PHP implements XML and data format conversion class examples, xml examples_PHP tutorial

13 Jul 2016

PHP implements XML and data format conversion class instances, xml instances. PHP implements XML and data format conversion class examples, xml examples This article describes PHP's implementation of XML and data format conversion class. Share it with everyone for your reference. The details are as follows: php/

PHP data conversion xml format file PHP data conversion xml format file

28 May 2023

PHP is a widely used server-side scripting language and one of the most popular. PHP is highly portable and scalable and can be used to write a variety of web applications and services. In web development, converting data into XML format files is a common requirement. So, how to use PHP to convert data into XML format files? This article will introduce you to some ways to achieve this. ## What is XML? XML was originally Extensible Markup Language (Extensible Markup Language)

Introducing JSON and XML format conversion in PHP Introducing JSON and XML format conversion in PHP

31 Mar 2023

JSON and XML format conversion in PHP During the development process, data format conversion is a problem that is often encountered. In PHP, the two commonly used data formats are JSON and XML. JSON is a lightweight data exchange format that is easy to read and write, while XML is an extensible markup language that is widely used in Web data transmission and configuration file storage. This article will introduce how to convert JSON format to XML format in PHP. 1. JSON to XML PHP provides a tool that can be used to convert JSON data into XML format.

PHP parsing xml format data tool class example explanation PHP parsing xml format data tool class example explanation

03 Jul 2018

This article mainly introduces the PHP parsing xml format data tool class, involving PHP's related operation skills such as adding, obtaining, and parsing xml format data nodes. Friends in need can refer to the following

PHP parsing xml format data tool class example sharing PHP parsing xml format data tool class example sharing

09 Jan 2018

This article mainly introduces the PHP parsing xml format data tool class, involving PHP's related operation skills for adding, obtaining, and parsing xml format data nodes. Friends who need it can refer to it. I hope it can help everyone.

See all articles