gpt4 book ai didi

php - excel如何读取XML文件?

转载 作者:可可西里 更新时间:2023-11-01 13:26:16 26 4
gpt4 key购买 nike

我研究了很多将 xml 文件转换为二维数组的方法,就像在 excel 中打开 xml 文件时 excel 尝试制作与 excel 相同的算法一样。

<items>
<item>
<sku>abc 1</sku>
<title>a book 1</title>
<price>42 1</price>
<attributes>
<attribute>
<name>Number of pages 1</name>
<value>123 1</value>
</attribute>
<attribute>
<name>Author 1</name>
<value>Rob dude 1</value>
</attribute>
</attributes>
<contributors>
<contributor>John 1</contributor>
<contributor>Ryan 1</contributor>
</contributors>
<isbn>12345</isbn>
</item>
<item>
<sku>abc 2</sku>
<title>a book 2</title>
<price>42 2</price>
<attributes>
<attribute>
<name>Number of pages 2</name>
<value>123 2</value>
</attribute>
<attribute>
<name>Author 2</name>
<value>Rob dude 2</value>
</attribute>
</attributes>
<contributors>
<contributor>John 2</contributor>
<contributor>Ryan 2</contributor>
</contributors>
<isbn>6789</isbn>
</item>
</items>

我想让它把它转换成二维数组,就像你在 Excel 中打开同一个文件时它会像这样显示

enter image description here


我想像 Excel 一样转换为二维数组。到目前为止,我可以像 Excel 一样提取标签

function getColNames($array) {
$cols = array();
foreach($array as $key=>$val) {
if(is_array($val)) {
if($val['type']=='complete') {
if(in_array($val['tag'], $cols)) {

} else {
$cols[] = $val['tag'];
}
}
}
}
return $cols;
}

$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);

目标

我想让它像这样生成..

array (
0 => array (
'sku'=>'abc 1',
'title'=>'a book 1',
'price'=>'42 1',
'name'=>'Number of Pages 1',
'value'=>'123 1',
'isbn'=>12345
),
1 => array (
'sku'=>'abc 1',
'title'=>'a book 1',
'price'=>'42 1',
'name'=>'Author 1',
'value'=>'Rob dude 1',
'isbn'=>12345
),
2 => array (
'sku'=>'abc 1',
'title'=>'a book 1',
'price'=>'42 1',
'contributor'=>'John 1',
'isbn'=>12345
),
3 => array (
'sku'=>'abc 1',
'title'=>'a book 1',
'price'=>'42 1',
'contributor'=>'Ryan 1',
'isbn'=>12345
),
)

示例 2 XML..

 <items>
<item>
<sku>abc 1</sku>
<title>a book 1</title>
<price>42 1</price>
<attributes>
<attribute>
<name>Number of pages 1</name>
<value>123 1</value>
</attribute>
<attribute>
<name>Author 1</name>
<value>Rob dude 1</value>
</attribute>
</attributes>
<contributors>
<contributor>John 1</contributor>
<contributor>Ryan 1</contributor>
</contributors>
<isbns>
<isbn>12345a</isbn>
<isbn>12345b</isbn>
</isbns>
</item>
<item>
<sku>abc 2</sku>
<title>a book 2</title>
<price>42 2</price>
<attributes>
<attribute>
<name>Number of pages 2</name>
<value>123 2</value>
</attribute>
<attribute>
<name>Author 2</name>
<value>Rob dude 2</value>
</attribute>
</attributes>
<contributors>
<contributor>John 2</contributor>
<contributor>Ryan 2</contributor>
</contributors>
<isbns>
<isbn>6789a</isbn>
<isbn>6789b</isbn>
</isbns>
</item>
</items>

示例 3 XML..

<items>
<item>
<sku>abc 1</sku>
<title>a book 1</title>
<price>42 1</price>
<attributes>
<attribute>
<name>Number of pages 1</name>
<value>123 1</value>
</attribute>
<attribute>
<name>Author 1</name>
<value>Rob dude 1</value>
</attribute>
</attributes>
<contributors>
<contributor>John 1</contributor>
<contributor>Ryan 1</contributor>
</contributors>
<isbns>
<isbn>
<name>isbn 1</name>
<value>12345a</value>
</isbn>
<isbn>
<name>isbn 2</name>
<value>12345b</value>
</isbn>
</isbns>
</item>
<item>
<sku>abc 2</sku>
<title>a book 2</title>
<price>42 2</price>
<attributes>
<attribute>
<name>Number of pages 2</name>
<value>123 2</value>
</attribute>
<attribute>
<name>Author 2</name>
<value>Rob dude 2</value>
</attribute>
</attributes>
<contributors>
<contributor>John 2</contributor>
<contributor>Ryan 2</contributor>
</contributors>
<isbns>
<isbn>
<name>isbn 3</name>
<value>6789a</value>
</isbn>
<isbn>
<name>isbn 4</name>
<value>6789b</value>
</isbn>
</isbns>
</item>
</items>

最佳答案

根据你模糊的问题,你所谓的“Excel”用我自己的话来说它做了以下事情:它将每个 /items/item 元素作为一行。从文档顺序来看,column-name是每个leaf-element-nodes的tag-name,如果有重名,则位置在第一个。

然后它每行创建一行,但前提是所有子元素都是叶元素。否则,该行将作为该行之外的行的基础,并且内插包含非叶元素的元素。例如。如果这样的条目确实有两倍的两个额外的同名叶子,这些叶子就会被插入到两行中。然后将它们的子值放入列的位置,名称遵循第一段中描述的逻辑。

您的问题并不清楚遵循这种逻辑的深度。所以我只把它保持在那个水平。否则插值将需要递归到树的更深处。为此,概述的算法可能不再适用。

要在 PHP 中构建它,您可以特别受益于 XPath,并且插值作为生成器可以产生奇迹。

function tree_to_rows(SimpleXMLElement $xml)
{
$columns = [];

foreach ($xml->xpath('/*/*[1]//*[not(*)]') as $leaf) {
$columns[$leaf->getName()] = null;
}

yield array_keys($columns);

$name = $xml->xpath('/*/*[1]')[0]->getName();

foreach ($xml->$name as $source) {
$rowModel = array_combine(array_keys($columns), array_fill(0, count($columns), null));
$interpolations = [];

foreach ($source as $child) {
if ($child->count()) {
$interpolations[] = $child;
} else {
$rowModel[$child->getName()] = $child;
}
}

if (!$interpolations) {
yield array_values($rowModel);
continue;
}

foreach ($interpolations as $interpolation) {
foreach ($interpolation as $interpolationStep) {
$row = $rowModel;
foreach ($interpolationStep->xpath('(.|.//*)[not(*)]') as $leaf) {
$row[$leaf->getName()] = $leaf;
}
yield array_values($row);
}
}
}
}

然后使用它可以像这样直接:

$xml  = simplexml_load_file('items.xml');
$rows = tree_to_rows($xml);
echo new TextTable($rows);

给出模范输出:

+-----+--------+-----+-----------------+----------+-----------+-----+
|sku |title |price|name |value |contributor|isbn |
+-----+--------+-----+-----------------+----------+-----------+-----+
|abc 1|a book 1|42 1 |Number of pages 1|123 1 | |12345|
+-----+--------+-----+-----------------+----------+-----------+-----+
|abc 1|a book 1|42 1 |Author 1 |Rob dude 1| |12345|
+-----+--------+-----+-----------------+----------+-----------+-----+
|abc 1|a book 1|42 1 | | |John 1 |12345|
+-----+--------+-----+-----------------+----------+-----------+-----+
|abc 1|a book 1|42 1 | | |Ryan 1 |12345|
+-----+--------+-----+-----------------+----------+-----------+-----+
|abc 2|a book 2|42 2 |Number of pages 2|123 2 | |6789 |
+-----+--------+-----+-----------------+----------+-----------+-----+
|abc 2|a book 2|42 2 |Author 2 |Rob dude 2| |6789 |
+-----+--------+-----+-----------------+----------+-----------+-----+
|abc 2|a book 2|42 2 | | |John 2 |6789 |
+-----+--------+-----+-----------------+----------+-----------+-----+
|abc 2|a book 2|42 2 | | |Ryan 2 |6789 |
+-----+--------+-----+-----------------+----------+-----------+-----+

TextTablehttps://gist.github.com/hakre/5734770 的略微修改版本允许在 Generators 上运行 - 如果您正在寻找该代码。

关于php - excel如何读取XML文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25992326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com