`
wangshaofei
  • 浏览: 279526 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

xml 转换成 array

阅读更多
xmltoarray 写道
<?php

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

class XmlToArray {

function __construct() {

}

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
}

//Set the attributes too.
if (isset($attributes) and $get_attributes) {
foreach ($attributes as $attr => $val) {
if ($priority == 'tag')
$attributes_data[$attr] = $val;
else
$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
}
}

//See tag status and do the needed.
if ($type == "open") {//The starting of the tag '<tag>'
$parent[$level - 1] = &$current;
if (!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
$current[$tag] = $result;
if ($attributes_data)
$current[$tag . '_attr'] = $attributes_data;
$repeated_tag_index[$tag . '_' . $level] = 1;

$current = &$current[$tag];
} else { //There was another element with the same tag name
if (isset($current[$tag][0])) {//If there is a 0th element it is already an array
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
$repeated_tag_index[$tag . '_' . $level]++;
} else {//This section will make the value an array if multiple tags with the same name appear together
$current[$tag] = array($current[$tag], $result); //This will combine the existing item and the new item together to make an array
$repeated_tag_index[$tag . '_' . $level] = 2;

if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset($current[$tag . '_attr']);
}
}
$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
$current = &$current[$tag][$last_item_index];
}
} elseif ($type == "complete") { //Tags that ends in 1 line '<tag />'
//See if the key is already taken.
if (!isset($current[$tag])) { //New Key
$current[$tag] = $result;
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $attributes_data)
$current[$tag . '_attr'] = $attributes_data;
} else { //If taken, put all things inside a list(array)
if (isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
// ...push the new element into that array.
$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;

if ($priority == 'tag' and $get_attributes and $attributes_data) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
$repeated_tag_index[$tag . '_' . $level]++;
} else { //If it is not an array...
$current[$tag] = array($current[$tag], $result); //...Make it an array using using the existing value and the new value
$repeated_tag_index[$tag . '_' . $level] = 1;
if ($priority == 'tag' and $get_attributes) {
if (isset($current[$tag . '_attr'])) { //The attribute of the last(0th) tag must be moved as well
$current[$tag]['0_attr'] = $current[$tag . '_attr'];
unset($current[$tag . '_attr']);
}

if ($attributes_data) {
$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
}
}
$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
}
}
} elseif ($type == 'close') { //End of tag '</tag>'
$current = &$parent[$level - 1];
}
}

return $xml_array;
}

}

?>
 
0
1
分享到:
评论

相关推荐

    PHP 的Array2xml

    然而,有时我们处理的数据格式是数组,这时就需要将数组转换成XML以便与其他系统进行交互。"PHP的Array2xml"就是这样一个工具,它是一个类文件,专为PHP4.0及更高版本设计,用于将PHP数组转换为XML字符串。 Array2...

    php xml 转json和array

    `xml2json0.php`可能是一个实现XML到JSON转换的脚本,而`Xmltoarray.php`则可能是将XML转换为数组的实现。这两个文件可以作为实际操作的例子,帮助理解XML处理的流程。 总结来说,XML和JSON之间的转换在PHP中是常见...

    XML 动态转换成类

    这可以通过在类中定义其他类或者使用`XmlArray`和`XmlArrayItem`特性来实现。此外,还可以使用`XmlAttribute`特性来处理XML属性。 在提供的"XMLtoClass.sln"和"XMLtoClass"文件中,很可能是包含了一个示例项目,该...

    探讨:array2xml和xml2array以及xml与array的互相转化

    在客户端和服务器之间进行数据交换时,有时需要将XML转换为PHP数组,反之亦然。本文将详细介绍`array2xml`和`xml2array`这两个函数,以及如何实现XML和数组之间的互相转化。 首先,我们来看`array2xml`函数。这个...

    java XML转成LIST可以转成指定的类数组

    本篇文章将深入探讨如何将XML转换为指定类型的List数组,并涉及多层数据结构的处理。 1. **Java XML解析库** 在Java中,有多种库可用于解析XML文件,如DOM(Document Object Model)、SAX(Simple API for XML)和...

    Flex中Tree组件的数据源举例(xml,array,object)

    在Flex中,我们可以使用mx.collections.XMLListCollection将XML转换为可绑定的数据集合。例如: ```xml &lt;xml&gt; &lt;item&gt;Item 1.1 &lt;item&gt;Item 1.2 &lt;item&gt;Item 2.1 &lt;/xml&gt; ``` 然后在Flex代码中,你可以...

    PHP 数组与Xml转换

    在 PHP 中,数组和 XML 之间的转换是一种常见的需求,特别是在数据交换、存储或者解析 XML 文件时。本篇文章将深入探讨如何在 PHP 中实现数组到 XML 的转换,并提供两种不同的实现方式。 首先,我们来看第一种方法...

    php中Array2xml类实现数组转化成XML实例

    如果需要处理更复杂的XML转换,可以考虑使用PHP内置的DOMDocument类或者SimpleXMLElement类。 总的来说,Array2xml类提供了一个简单的方法,将PHP数组转换为XML字符串,方便在不同系统间进行数据交换。然而,对于...

    xml-to-array:将xml转换为数组的简单类

    将xml转换为数组 该软件包提供了一个非常简单的类,可以将xml字符串转换为数组。 受Spatie的启发 :red_heart_selector: 安装 您可以通过composer安装此软件包。 composer require vyuldashev/xml-to-array 用法 ...

    PHP实现数组array转换成xml的方法

    在PHP编程中,有时我们需要将数据结构转换成XML格式,以便于数据交换或者与XML相关的服务进行交互。本文介绍了一种方法,展示了如何通过...这些资源将帮助你进一步提升PHP编程技能,特别是涉及到数组和XML转换的部分。

    xml-to-array:轻松将有效的xml转换为php数组

    轻松地将有效的xml转换为php数组。 安装 通过composer安装: composer require mtownsend/xml-to-array 快速开始 使用课程 use Mtownsend \ XmlToArray \ XmlToArray ; $ xml = &lt;&lt;&lt;XML &lt;?xml version=...

    PHP二维数组形成XML内容形式

    它展示了如何加载数据到二维数组,然后调用`array_to_xml`函数将数据转换为XML,最后输出生成的XML内容。这个例子是学习如何在PHP中操作数组和XML数据的一个很好的起点,对于任何需要在PHP项目中进行数据交换的工作...

    php xml转数组

    有两种方法可以将XML转换为数组,一种是使用`DOMDocument`和`DOMXpath`,另一种是使用`simplexml_load_string`函数。这里我们先介绍第一种: ```php $resultArray = []; foreach ($xpath-&gt;query('//root/item')...

    array-to-xml:将数组转换为xml的简单类

    将数组转换为xml 该程序包提供了一个非常简单的类,可以将数组转换为xml字符串。支持我们 我们投入了大量资源来创建。 您可以通过来支持我们。 非常感谢您从家乡寄给我们一张明信片,其中提及您使用的是哪个包装。 ...

    PHP实现XML与数据格式进行转换类

    - **XML转数组**:可以使用`iterator_to_array()`结合`SimpleXMLElement`的迭代器功能,将XML转换为PHP数组。或者使用DOMXPath查询XML节点,再进一步处理成数组。 - **数组转XML**:SimpleXML提供了一个方便的方法`...

    as 3.0读取XML

    var xmlArray:Array = []; for each (var node:XML in xml.children()) { xmlArray.push(node.toString()); } displayXMLContent(xmlArray); // 将数组传递给显示函数 } ``` 此函数遍历XML的子节点,并将每个...

    OpenCV中xml矩阵转换为MATLAB的矩阵

    本篇文章将详细介绍如何将OpenCV中的XML矩阵转换为MATLAB可读的矩阵。 首先,OpenCV使用XML或YAML格式存储一些结构化数据,如特征点、相机内参等。这些文件可以用`cv::FileStorage`类来读取和写入。XML文件是一种...

    PHP 将 XML文件或内容直接转成数组

    因此,如何有效地将XML转换为数组成为了一个常见的问题。 #### 核心知识点 1. **XML简介** - XML是一种标准的、开放的文件格式,被广泛应用于不同系统之间的数据交换。 - XML文件具有良好的自描述性和层次性,...

Global site tag (gtag.js) - Google Analytics