从xml文件创建一个数组

时间:2021-06-17 21:16:28

My xml file looks like this:

我的xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <item>
    <Post>
      <id><![CDATA[1]]></id>
      <title><![CDATA[The title]]></title>
      <body><![CDATA[This is the post body.]]></body>
      <created><![CDATA[2008-07-28 12:01:06]]></created>
      <modified><![CDATA[]]></modified>
    </Post>
  </item>
  <item>
    <Post>
      <id><![CDATA[2]]></id>
      <title><![CDATA[A title once again]]></title>
      <body><![CDATA[And the post body follows.]]></body>
      <created><![CDATA[2008-07-28 12:01:06]]></created>
      <modified><![CDATA[]]></modified>
      <item>
        <item><![CDATA[fdgs]]></item>
      </item>
    </Post>
  </item>
  <item>
    <Post>
      <id><![CDATA[3]]></id>
      <title><![CDATA[Title strikes back]]></title>
      <body><![CDATA[This is really exciting Not.]]></body>
      <created><![CDATA[2008-07-28 12:01:06]]></created>
      <modified><![CDATA[]]></modified>
    </Post>
  </item>
</root>

Here is the my expected output:

这是我的预期输出:

Array(
0=>Array(
    'Post'=>Array(
        'id'=>1, 
        'title'=>'The title', 
        'body'=>'This is the post body.', 
        'created'=>'2008-07-28 12:01:06', 
        'modified'=>'',)
        ), 
1=>Array(
    'Post'=>Array(
        'id'=>2, 
        'title'=>'A title once again', 
        'body'=>'And the post body follows.', 
        'created'=>'2008-07-28 12:01:06', 
        'modified'=>'', 
        array('fdgs'),)
        ), 
2=>Array(
    'Post'=>Array(
        'id'=>3, 
        'title'=>'Title strikes back', 
        'body'=>'This is really exciting Not.', 
        'created'=>'2008-07-28 12:01:06', 
        'modified'=>'',)
        ),
);

And this is my code:

这是我的代码:

$xml=new Xml2Array();
        $xmlData = simplexml_load_file('d:\\xmlfile\\Array2XmlExampleData.xml');
        $expectedResult=$xml->simpleXMLToArray($xmlData);
        var_dump($expectedResult);

The array result I get from var_dump() is null. How can I solve this problem? Please help me out, thanks.

我从var_dump()得到的数组结果为null。我怎么解决这个问题?请帮帮我,谢谢。

1 个解决方案

#1


0  

You did not show the relevant which is the simpleXMLToArray() function. So we can't really tell what's wrong with your code.

您没有显示相关的simpleXMLToArray()函数。所以我们无法确定您的代码有什么问题。

But converting a SimpleXML-Object into an array is actually not that hard - here's one way to do it:

但是将SimpleXML-Object转换为数组实际上并不难 - 这是一种方法:

$array = json_decode( json_encode( (array) $xmlData ), true);

converts a given XML to an array. But to make it work in your case you need to make sure you load your data with the LIBXML_NOCDATA flag (see documentation):

将给定的XML转换为数组。但要使其在您的情况下工作,您需要确保使用LIBXML_NOCDATA标志加载数据(请参阅文档):

$xmlData = simplexml_load_file('d:\\xmlfile\\Array2XmlExampleData.xml', 'SimpleXMLElement', LIBXML_NOCDATA);

$ xmlData = simplexml_load_file('d:\\ xmlfile \\ Array2XmlExampleData.xml','SimpleXMLElement',LIBXML_NOCDATA);

Now you just load your XML, iterate through the <item>-tags and convert them to arrays:

现在您只需加载XML,遍历 -tags并将它们转换为数组:

$xmlData = simplexml_load_file(
      'd:\\xmlfile\\Array2XmlExampleData.xml', 
      'SimpleXMLElement', 
      LIBXML_NOCDATA
);

$results = [];

foreach($xmlData->item as $item)
{
  $results[] = json_decode(json_encode((array)$item), true);
}

Here's a working example. Of course you'll need to add your sanitization logic to filter the unwanted elements or do some formatting. But you get the idea.

这是一个有效的例子。当然,您需要添加清理逻辑来过滤不需要的元素或进行一些格式化。但是你明白了。

Also, make sure the xml is loaded properly and that your application has reading permissions for the file.

此外,请确保正确加载xml并确保您的应用程序具有该文件的读取权限。

#1


0  

You did not show the relevant which is the simpleXMLToArray() function. So we can't really tell what's wrong with your code.

您没有显示相关的simpleXMLToArray()函数。所以我们无法确定您的代码有什么问题。

But converting a SimpleXML-Object into an array is actually not that hard - here's one way to do it:

但是将SimpleXML-Object转换为数组实际上并不难 - 这是一种方法:

$array = json_decode( json_encode( (array) $xmlData ), true);

converts a given XML to an array. But to make it work in your case you need to make sure you load your data with the LIBXML_NOCDATA flag (see documentation):

将给定的XML转换为数组。但要使其在您的情况下工作,您需要确保使用LIBXML_NOCDATA标志加载数据(请参阅文档):

$xmlData = simplexml_load_file('d:\\xmlfile\\Array2XmlExampleData.xml', 'SimpleXMLElement', LIBXML_NOCDATA);

$ xmlData = simplexml_load_file('d:\\ xmlfile \\ Array2XmlExampleData.xml','SimpleXMLElement',LIBXML_NOCDATA);

Now you just load your XML, iterate through the <item>-tags and convert them to arrays:

现在您只需加载XML,遍历 -tags并将它们转换为数组:

$xmlData = simplexml_load_file(
      'd:\\xmlfile\\Array2XmlExampleData.xml', 
      'SimpleXMLElement', 
      LIBXML_NOCDATA
);

$results = [];

foreach($xmlData->item as $item)
{
  $results[] = json_decode(json_encode((array)$item), true);
}

Here's a working example. Of course you'll need to add your sanitization logic to filter the unwanted elements or do some formatting. But you get the idea.

这是一个有效的例子。当然,您需要添加清理逻辑来过滤不需要的元素或进行一些格式化。但是你明白了。

Also, make sure the xml is loaded properly and that your application has reading permissions for the file.

此外,请确保正确加载xml并确保您的应用程序具有该文件的读取权限。