使用PHP将XML转换为关联数组

时间:2011-06-30 11:08:15

标签: php xml arrays

任何人都可以帮助将XML文档中的数据转换为关联数组吗?我遇到了问题,因为XML结构是一种3D结构,并且数组更像是2D结构(请原谅我缺乏正确的术语)。 XML元素有属性,子元素和子元素(但我从来不知道它们的名字),所以我想我会尝试使数组中的键成为每个子/属性名称的串联,并且值等于,嗯,价值。麻烦的是我需要将属性名称和值作为连接数组键的一部分,以使其唯一...

例如:

<Computer id="1">   
    <OS>
        <Name>Linux</Name>
        <Age>Older than me</Age>
    </OS>
</Computer>
<Computer id="2">
    <OS>
        <Name>Windows</Name>
        <Age>Not so much</Age>
    </OS>
</Computer>

理想情况下应该给出:

[Computer-id-1-OS-Name] = 'Linux'
[Computer-id-1-OS-Age] = 'Older than me'
[Computer-id-2-OS-Name] = 'Windows'
[Computer-id-2-OS-Age] = 'Not so much'

但我得到了这个结果:

[Computer-id] = '1'
[Computer-OS-Name] = 'Linux'
[Computer-OS-Age] = 'Older than me'
[Computer-id] = '2'
[Computer-OS-Name] = 'Windows'
[Computer-OS-Age] = 'Not so much'

因此[Computer-id]键不是唯一的。我正在使用递归函数来读取值,但我无法弄清楚如何将属性名称和属性值转换为从属键的名称...(顺便说一句,有一个很好的理由这样做看似不合逻辑的任务!) 任何帮助将不胜感激......

这是在将XML数据读入多维数组后“展平”XML函数的函数。我不确定我会以正确的方式做到这一点!

function flattenArray ($array, $baseName = NULL)
{
    reset($array);
    while (list ($key, $value) = each($array)) {
        $outKey = $key . "-";
        if (is_array($value)) {
            flattenArray($value, $baseName . $outKey);
        } else {
            $finalKey = $baseName . rtrim($outKey, '-');
            $finalValue = $value;
            echo "$finalKey = $finalValue\n";
        }
    }
}

6 个答案:

答案 0 :(得分:42)

这对我很有用,而且很简单。

$ob = simplexml_load_file('test.xml');
$json = json_encode($ob);
$array = json_decode($json, true);

答案 1 :(得分:5)

一个例子可能是:

$dom = new DOMDocument;
$dom->loadXML(
    '<root>
        <Computer id="1">   
            <OS>
                <Name>Linux</Name>
                <Age>Older than me</Age>
            </OS>
        </Computer>

        <Computer id="2">
            <OS>
                <Name>Windows</Name>
                <Age>Not so much</Age>
            </OS>
        </Computer>
    </root>'
);

$xpath = new DOMXPath($dom);
$result = array();

foreach ($xpath->query('//*[count(*) = 0]') as $node) {
    $path = array();
    $val = $node->nodeValue;

    do {
        if ($node->hasAttributes()) {
            foreach ($node->attributes as $attribute) {
                $path[] = sprintf('%s[%s]', $attribute->nodeName, $attribute->nodeValue);
            }
        }
        $path[] = $node->nodeName;
    }
    while ($node = $node->parentNode);

    $result[implode('/', array_reverse($path))] = $val;
}

print_r($result);

输出:

Array
(
    [#document/root/Computer/id[1]/OS/Name] => Linux
    [#document/root/Computer/id[1]/OS/Age] => Older than me
    [#document/root/Computer/id[2]/OS/Name] => Windows
    [#document/root/Computer/id[2]/OS/Age] => Not so much
)

这不完全是你想要的,但它是一个开始,可以很容易地调整,以给出不同的结果。

答案 2 :(得分:2)

将xml读入DOM对象,循环遍历,将结果保存到数组中。就这么简单。

答案 3 :(得分:2)

这是我生成相关数组的函数,派生自

Recursive cast from SimpleXMLObject to Array

function xml2assoc($obj, &$arr) {
  $children = $obj->children();
  foreach ( $children as $elementName => $node ) {

    if (!isset($arr[$elementName])) {
      $arr[$elementName] = array();
    }
    $temp = array();
    $attributes = $node->attributes();
    foreach ( $attributes as $attributeName => $attributeValue ) {
      $attribName = strtolower(trim((string) $attributeName));
      $attribVal = trim((string) $attributeValue);
      $temp[$attribName] = $attribVal;
    }
    $text = (string) $node;
    $text = trim($text);
    if (strlen($text) > 0) {
      $temp ['text='] = $text;
    }
    $arr[$elementName][] = $temp;
    $nextIdx = count($arr[$elementName]);
    xml2assoc($node, $arr[$elementName][$nextIdx - 1]);
  }
  return;
}

$xml = '<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>2</ArticleCount>
<Articles>
<item>
<Title><![CDATA[title1]]></Title> 
<Description><![CDATA[description1]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
<item>
<Title><![CDATA[title]]></Title>
<Description><![CDATA[description]]></Description>
<PicUrl><![CDATA[picurl]]></PicUrl>
<Url><![CDATA[url]]></Url>
</item>
</Articles>
</xml> ';

$dom = new SimpleXMLElement($xml);

$arr = array();

xml2assoc($dom, $arr);
print_r($arr);

生成的数组:

Array
(
    [ToUserName] => Array
        (
            [0] => Array
                (
                    [text=] => toUser
                )

        )

    [FromUserName] => Array
        (
            [0] => Array
                (
                    [text=] => fromUser
                )

        )

    [CreateTime] => Array
        (
            [0] => Array
                (
                    [text=] => 12345678
                )

        )

    [MsgType] => Array
        (
            [0] => Array
                (
                    [text=] => news
                )

        )

    [ArticleCount] => Array
        (
            [0] => Array
                (
                    [text=] => 2
                )

        )

    [Articles] => Array
        (
            [0] => Array
                (
                    [item] => Array
                        (
                            [0] => Array
                                (
                                    [Title] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => title1
                                                )

                                        )

                                    [Description] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => description1
                                                )

                                        )

                                    [PicUrl] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => picurl
                                                )

                                        )

                                    [Url] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => url
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [Title] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => title
                                                )

                                        )

                                    [Description] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => description
                                                )

                                        )

                                    [PicUrl] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => picurl
                                                )

                                        )

                                    [Url] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [text=] => url
                                                )

                                        )

                                )

                        )

                )

        )

)

答案 4 :(得分:0)

简单数组可能是2d,但多维数组可以很容易地复制像xml这样的层次结构。

Google'关联多维数组php'以获取更多信息。

然而,正如已经说过的那样,PHP有一个内置的xml解析器,所以不管怎样都不需要在数组中重新创建xml,更不用说将它展平为一个简单的数组了。

在PHP中,您的数组结构应该类似于:

$computers["computers"]["computer-1"]["OS"]["Name"] = "Linux";
$computers["computers"]["computer-1"]["OS"]["Age"] = "Older Than Me";

$computers["computers"]["computer-2"]["OS"]["Name"] = "Windows";
$computers["computers"]["computer-2"]["OS"]["Age"] = "Not so much";

等...

答案 5 :(得分:0)

我修改了user655000的答案,使其更接近json_decode(json_encode($ dom))格式化/返回数据的方式。我还将初始数组参数设为可选,因为无论如何它都将为空。

由于在PHP的编码函数中似乎存在错误,因此我无法使用encode(encode)方法,这导致在某些示例数据上,decode()返回null。我尝试使用编码器功能的安全版本,但内存不足。

行为上有细微的差别。如果存在nodeText,则decode(encode)方法将丢弃任何属性(可能也包括子级)。我的方法没有。

function readxml($xmlfile, $recursive = false){
    $ob = simplexml_load_file($xmlfile);
    //primary method
    $json = json_encode($ob);
    $array = json_decode($json, true);
    if(is_null($array)){//backup method
        $array = xml2assoc($ob);
    }
    return $array;
}

function xml2assoc($obj, &$arr = null) {
    $children = $obj->children();//->count(); 
    $nodes = [];
    foreach ( $children as $elementName => $node ) {
        if(!isset($nodes[$elementName])){
            $nodes[$elementName] = 0;
        }
        $nodes[$elementName]++;
    }
    $indexes = [];

    if($arr === null){
        $arr = [];
    }
    foreach ( $children as $elementName => $node ) {
        $temp = array();
        $grandchildren = $node->children()->count();
        
        //attributes        
        $attributes = $node->attributes();
        foreach ( $attributes as $attributeName => $attributeValue ) {
            $attribName = trim((string) $attributeName);
            $attribVal = trim((string) $attributeValue);
            $temp["@attributes"][$attribName] = $attribVal;
        }
        
        //text      
        $text = (string) $node;
        $text = trim($text);
        if (strlen($text) > 0) {
            if(count($temp) == 0 && $grandchildren == 0){
                $temp = $text;//discard the children/attribute data since there aren't any
            } else {
                $temp["NodeText"] = $text;//retain the children/attributes
            }
        }       
        
        //grandchildren
        if($temp || is_string($temp) || $grandchildren > 0 ){
            if( $nodes[$elementName] == 1 ){//only one of it's kind
                $arr[$elementName] = $temp;
                xml2assoc($node, $arr[$elementName]);
            } else {//has multiple nodes of the same kind
                if(isset($indexes[$elementName])){
                    $indexes[$elementName]++;
                } else {
                    $indexes[$elementName] = 0;
                }
                $index = $indexes[$elementName];
                $arr[$elementName][$index] = $temp;
                xml2assoc($node, $arr[$elementName][$index]);
            }
        }
    }
    return $arr;
}