Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php excel如何读取XML文件?_Php_Xml_Excel - Fatal编程技术网

Php excel如何读取XML文件?

Php excel如何读取XML文件?,php,xml,excel,Php,Xml,Excel,我已经研究了很多将xml文件转换为2d数组的方法,就像excel在excel中打开xml文件时尝试使用的算法一样 abc 1 一本书 42 1 页数1 123 1 作者1 罗布·杜德1 约翰1 瑞安1 12345 abc 2 一本书2 42 2 页数2 123 2 作者2 罗布·杜德2 约翰2 瑞安2 6789 我想把它转换成二维数组,就像如果你在Excel中打开同一个文件,它会像这样显示你 我想像Excel一样转换为二维数组。到目前为止,我可以像Excel一样提取标签 函数getCol

我已经研究了很多将xml文件转换为2d数组的方法,就像excel在excel中打开xml文件时尝试使用的算法一样


abc 1
一本书
42 1
页数1
123 1
作者1
罗布·杜德1
约翰1
瑞安1
12345
abc 2
一本书2
42 2
页数2
123 2
作者2
罗布·杜德2
约翰2
瑞安2
6789
我想把它转换成二维数组,就像如果你在Excel中打开同一个文件,它会像这样显示你


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

函数getColNames($array){ $cols=array(); foreach($key=>$val的数组){ if(is_数组($val)){ 如果($val['type']=='complete'){ if(在数组中($val['tag'],$cols)){ }否则{ $cols[]=$val['tag']; } } } } 返回$cols; } $p=xml_parser_create(); xml_将_解析为_结构($p、$simple、$vals、$index); 无xml解析器($p); 目标 我想让它像这样生成

数组(
0=>数组(
“sku”=>“abc 1”,
“书名”=>“一本书1”,
“价格”=>“42 1”,
'名称'=>'页数1',
'值'=>'123 1',
“isbn”=>12345
),
1=>数组(
“sku”=>“abc 1”,
“书名”=>“一本书1”,
“价格”=>“42 1”,
'name'=>'Author 1',
'value'=>'Rob dude 1',
“isbn”=>12345
),
2=>数组(
“sku”=>“abc 1”,
“书名”=>“一本书1”,
“价格”=>“42 1”,
“贡献者”=>“约翰1”,
“isbn”=>12345
),
3=>数组(
“sku”=>“abc 1”,
“书名”=>“一本书1”,
“价格”=>“42 1”,
“参与者”=>“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>

abc 1
一本书
42 1
页数1
123 1
作者1
罗布·杜德1
约翰1
瑞安1
12345a
12345b
abc 2
一本书2
42 2
页数2
123 2
作者2
罗布·杜德2
约翰2
瑞安2
6789a
6789b
示例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>

abc 1
一本书
42 1
页数1
123 1
作者1
罗布·杜德1
约翰1
瑞安1
isbn 1
12345a
isbn 2
12345b
abc 2
一本书2
42 2
页数2
123 2
作者2
罗布·杜德2
约翰2
瑞安2
isbn 3
6789a
isbn 4
6789b

要从给定的xml文件中获取所需的数组,必须这样做。这不是太有趣,所以我希望这确实是你想要的

给定您给出的关于它的确切XML,将生成作为最终结果的输出

这是用PHP5.6编写的,我相信如果您的环境中遇到问题,您必须将函数调用移到自己的行中,并将[]替换为array()

$items = simplexml_load_file("items.xml");

$items_array = [];

foreach($items as $item) {

    foreach($item->attributes->attribute as $attribute) {
        array_push($items_array, itemsFactory($item, (array) $attribute));
    }

    foreach((array) $item->contributors->contributor as $contributer) {
        array_push($items_array, itemsFactory($item, $contributer));
    }

}

function itemsFactory($item, $vars) {

    $item = (array) $item;

    return [
        "sku" => $item['sku'],
        "title" => $item['title'],
        "price" => $item['price'],
        "name" => (is_array($vars) ? $vars['name'] : ""),
        "value" => (is_array($vars) ? $vars['name'] : ""),
        "contributer" => (is_string($vars) ? $vars : ""),
        "isbn" => $item['isbn']
    ];

}

var_dump($items_array);
以下是在XML文件上运行时的结果

array(8) {
  [0]=>
  array(7) {
    ["sku"]=>
    string(5) "abc 1"
    ["title"]=>
    string(8) "a book 1"
    ["price"]=>
    string(4) "42 1"
    ["name"]=>
    string(17) "Number of pages 1"
    ["value"]=>
    string(17) "Number of pages 1"
    ["contributer"]=>
    string(0) ""
    ["isbn"]=>
    string(5) "12345"
  }
  [1]=>
  array(7) {
    ["sku"]=>
    string(5) "abc 1"
    ["title"]=>
    string(8) "a book 1"
    ["price"]=>
    string(4) "42 1"
    ["name"]=>
    string(8) "Author 1"
    ["value"]=>
    string(8) "Author 1"
    ["contributer"]=>
    string(0) ""
    ["isbn"]=>
    string(5) "12345"
  }
  [2]=>
  array(7) {
    ["sku"]=>
    string(5) "abc 1"
    ["title"]=>
    string(8) "a book 1"
    ["price"]=>
    string(4) "42 1"
    ["name"]=>
    string(0) ""
    ["value"]=>
    string(0) ""
    ["contributer"]=>
    string(6) "John 1"
    ["isbn"]=>
    string(5) "12345"
  }
  [3]=>
  array(7) {
    ["sku"]=>
    string(5) "abc 1"
    ["title"]=>
    string(8) "a book 1"
    ["price"]=>
    string(4) "42 1"
    ["name"]=>
    string(0) ""
    ["value"]=>
    string(0) ""
    ["contributer"]=>
    string(6) "Ryan 1"
    ["isbn"]=>
    string(5) "12345"
  }
  [4]=>
  array(7) {
    ["sku"]=>
    string(5) "abc 2"
    ["title"]=>
    string(8) "a book 2"
    ["price"]=>
    string(4) "42 2"
    ["name"]=>
    string(17) "Number of pages 2"
    ["value"]=>
    string(17) "Number of pages 2"
    ["contributer"]=>
    string(0) ""
    ["isbn"]=>
    string(4) "6789"
  }
  [5]=>
  array(7) {
    ["sku"]=>
    string(5) "abc 2"
    ["title"]=>
    string(8) "a book 2"
    ["price"]=>
    string(4) "42 2"
    ["name"]=>
    string(8) "Author 2"
    ["value"]=>
    string(8) "Author 2"
    ["contributer"]=>
    string(0) ""
    ["isbn"]=>
    string(4) "6789"
  }
  [6]=>
  array(7) {
    ["sku"]=>
    string(5) "abc 2"
    ["title"]=>
    string(8) "a book 2"
    ["price"]=>
    string(4) "42 2"
    ["name"]=>
    string(0) ""
    ["value"]=>
    string(0) ""
    ["contributer"]=>
    string(6) "John 2"
    ["isbn"]=>
    string(4) "6789"
  }
  [7]=>
  array(7) {
    ["sku"]=>
    string(5) "abc 2"
    ["title"]=>
    string(8) "a book 2"
    ["price"]=>
    string(4) "42 2"
    ["name"]=>
    string(0) ""
    ["value"]=>
    string(0) ""
    ["contributer"]=>
    string(6) "Ryan 2"
    ["isbn"]=>
    string(4) "6789"
  }
}
如果您确实可以访问excel文件而不是xml,那么这可能会容易得多。如果是这样的话,我们可以使用php excel呈现完全相同的内容,但它适用于任何数据集,而不仅仅是指定的数据集。如果不是这样,我想不出任何其他方法来将xml文件转换为您想要的内容

编辑:


这也可能给这个主题带来更多的启发,这是PHPExcel的开发者自己提出的。正如你所能做的那样,我认为如果你没有掌握一些优秀的源代码,或者没有花很长时间在这方面的工作,你就无法编写一些能够解析你抛出的任何XML文件的东西。。时间远远超出了这个问题的范围。然而,如果你要写一些东西来解析任何XML文件,我有一种感觉,它看起来像上面的一样,但有大量的条件。

根据你模糊的问题,你所谓的“Excel”用我自己的话来说,它做了以下事情:它将每个
/items/item
元素作为一行。根据文档顺序,列名是每个叶元素节点的标记名,如果有重复的名称,则位置是第一个

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

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

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 |
+-----+--------+-----+-----------------+----------+-----------+-----+