首页  >  文章  >  后端开发  >  PHP如何将XML转为数组

PHP如何将XML转为数组

PHPz
PHPz原创
2023-04-20 15:05:401343浏览

在Web开发中,XML被广泛使用作为数据传输和存储格式。当我们需要使用XML格式数据时,我们通常需要将XML字符串转换为数组或对象,以方便后续的操作。在PHP中,可以很方便地实现将XML字符串转换为数组操作。本篇文章将介绍PHP如何将XML转为数组。

一、XML格式介绍

XML是一种标记语言,主要用于数据传输和存储。XML包含了一些元素(Element)和属性(Attribute)。Element通常用标签(Tag)表示,如:

<book>
  <title>PHP从入门到精通</title>
  <price>50</price>
</book>

这个XML文本定义了一个book元素,其中包含了title和price子元素。

Attribute是给元素添加额外信息的方式,如:

<book category="Programming">
  <title>PHP从入门到精通</title>
  <price>50</price>
</book>

在这个例子中,我们为book元素设置了category属性,值为"Programming"。

二、PHP中将XML转为数组

PHP提供了许多用于处理XML的函数,其中比较常用的函数是simplexml_load_string()和xml_parse_into_struct()。下面我们分别介绍这两个函数的用法。

  1. simplexml_load_string()

simplexml_load_string()函数可以从一个XML字符串中创建一个SimpleXMLElement对象,通过这个对象可以方便地访问XML中的元素和属性,也可以将SimpleXMLElement对象转换为数组或其他类型的数据。下面是一个使用simplexml_load_string()函数将XML转换为数组的例子:

$xml = '<bookstore>
            <book category="Programming">
                <title>PHP从入门到精通</title>
                <author>张三</author>
                <price>50</price>
            </book>
            <book category="Web Development">
                <title>JavaScript高级编程</title>
                <author>李四</author>
                <price>45</price>
            </book>
        </bookstore>';
        
$data = json_decode(json_encode(simplexml_load_string($xml)), true);
print_r($data);

运行以上代码,输出结果如下:

Array
(
    [book] => Array
        (
            [0] => Array
                (
                    [@attributes] => Array
                        (
                            [category] => Programming
                        )
                    [title] => PHP从入门到精通
                    [author] => 张三
                    [price] => 50
                )
            [1] => Array
                (
                    [@attributes] => Array
                        (
                            [category] => Web Development
                        )
                    [title] => JavaScript高级编程
                    [author] => 李四
                    [price] => 45
                )
        )
)

可以看到,simplexml_load_string()函数将XML转换为一个由多个数组构成的关系数组。

  1. xml_parse_into_struct()

xml_parse_into_struct ()函数将XML字符串解析为一个数组。下面是一个使用xml_parse_into_struct()函数将XML转换为数组的例子:

$xml = '<bookstore>
            <book category="Programming">
                <title>PHP从入门到精通</title>
                <author>张三</author>
                <price>50</price>
            </book>
            <book category="Web Development">
                <title>JavaScript高级编程</title>
                <author>李四</author>
                <price>45</price>
            </book>
        </bookstore>';

$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals);
xml_parser_free($p);

print_r($vals);

运行以上代码,输出结果如下:

Array
(
    [0] => Array
        (
            [tag] => BOOKSTORE
            [type] => open
            [level] => 1
        )

    [1] => Array
        (
            [tag] => BOOK
            [type] => open
            [level] => 2
            [attributes] => Array
                (
                    [CATEGORY] => Programming
                )
        )

    [2] => Array
        (
            [tag] => TITLE
            [type] => open
            [level] => 3
        )

    [3] => Array
        (
            [tag] => TITLE
            [type] => close
            [level] => 3
            [value] => PHP从入门到精通
        )

    [4] => Array
        (
            [tag] => AUTHOR
            [type] => open
            [level] => 3
        )

    [5] => Array
        (
            [tag] => AUTHOR
            [type] => close
            [level] => 3
            [value] => 张三
        )

    [6] => Array
        (
            [tag] => PRICE
            [type] => open
            [level] => 3
        )

    [7] => Array
        (
            [tag] => PRICE
            [type] => close
            [level] => 3
            [value] => 50
        )

    [8] => Array
        (
            [tag] => BOOK
            [type] => close
            [level] => 2
        )

    [9] => Array
        (
            [tag] => BOOK
            [type] => open
            [level] => 2
            [attributes] => Array
                (
                    [CATEGORY] => Web Development
                )
        )

    [10] => Array
        (
            [tag] => TITLE
            [type] => open
            [level] => 3
        )

    [11] => Array
        (
            [tag] => TITLE
            [type] => close
            [level] => 3
            [value] => JavaScript高级编程
        )

    [12] => Array
        (
            [tag] => AUTHOR
            [type] => open
            [level] => 3
        )

    [13] => Array
        (
            [tag] => AUTHOR
            [type] => close
            [level] => 3
            [value] => 李四
        )

    [14] => Array
        (
            [tag] => PRICE
            [type] => open
            [level] => 3
        )

    [15] => Array
        (
            [tag] => PRICE
            [type] => close
            [level] => 3
            [value] => 45
        )

    [16] => Array
        (
            [tag] => BOOK
            [type] => close
            [level] => 2
        )

    [17] => Array
        (
            [tag] => BOOKSTORE
            [type] => close
            [level] => 1
        )
)

可以看到,xml_parse_into_struct()函数将XML解析成了一个由多个数组构成的关系数组,其中每个数组表示了XML中的一个元素或属性。

三、使用DOMDocument扩展进行转化

在PHP还有一种使用DOMDocument扩展来对XML进行解析的方法。DOMDocument是PHP自带的扩展,功能十分强大。下面是一个使用DOMDocument转换XML数据的例子:

$xml = '<bookstore>
            <book category="Programming">
                <title>PHP从入门到精通</title>
                <author>张三</author>
                <price>50</price>
            </book>
            <book category="Web Development">
                <title>JavaScript高级编程</title>
                <author>李四</author>
                <price>45</price>
            </book>
        </bookstore>';

$dom = new DOMDocument;
$dom->loadXML($xml);
$data = array();
foreach ($dom->getElementsByTagName('book') as $book) {
    $data[] = array(
        'category' => $book->getAttribute('category'),
        'title' => $book->getElementsByTagName('title')->item(0)->textContent,
        'author' => $book->getElementsByTagName('author')->item(0)->textContent,
        'price' => $book->getElementsByTagName('price')->item(0)->textContent,
    );
}
print_r($data);

运行以上代码,输出结果如下:

Array
(
    [0] => Array
        (
            [category] => Programming
            [title] => PHP从入门到精通
            [author] => 张三
            [price] => 50
        )

    [1] => Array
        (
            [category] => Web Development
            [title] => JavaScript高级编程
            [author] => 李四
            [price] => 45
        )

)

可以看到,使用DOMDocument解析XML数据,我们可以方便地获取XML中的元素和属性,进而转换为数组。

四、总结

以上就是PHP将XML转换为数组的几种方法,分别是simplexml_load_string()函数、xml_parse_into_struct()函数和使用DOMDocument扩展。每个方法都有其适用的场景,根据实际需求选择合适的方法。当然,无论使用何种方法,我们都需要注意XML的格式和规范,以确保数据的正确性和完整性。

以上是PHP如何将XML转为数组的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn