Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.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 xmlWriter_Php_Xmlwriter - Fatal编程技术网

添加到现有节点的php xmlWriter

添加到现有节点的php xmlWriter,php,xmlwriter,Php,Xmlwriter,我遇到过这样一种情况:有一些旧代码使用xmlWriter流解析文件,并且对于每个文件都使用该文件名编写一个元素,当文件名是唯一的时,这种方法就可以很好地工作 $xml = xmlWriter foreach file in files $xml->startElement($fileName) ....existing logic adding file content to the new element. $xml->endElement

我遇到过这样一种情况:有一些旧代码使用xmlWriter流解析文件,并且对于每个文件都使用该文件名编写一个元素,当文件名是唯一的时,这种方法就可以很好地工作

$xml = xmlWriter
foreach file in files
    $xml->startElement($fileName)
        ....existing logic adding file content to the new element.   
    $xml->endElement
但是现在,需求改变了,我可能有同名的文件,但是对于生成的xml,每个唯一的文件名应该只有一个元素


有没有一种简单的方法可以在不改变现有代码的情况下实现这一点?我知道这有点违背了流的整体意义,但生活很糟糕,对吧?

我可以看到一些解决方案,但所有这些都归结为创建多个节点

第一个是多个文件的多个节点,但每个文件都有一个路径属性。鉴于这些:

$fl1 = '/home/the_spleen/spleen.txt';
$fl2 = '/home/the_bowler/not_enough_beer/spleen.txt';
输出如下内容:

<spleen path='/home/the_spleen/'><!-- spleenerficy --></spleen>
<spleen path='/home/the_bowler/not_enough_beer/'><!-- no. just no. --></spleen>

主要利益?它允许您以几乎完全相同的方式创建和迭代。伤害它去掉了该节点的parent.children中的一个名称和一个节点

另一个选项是创建子元素。给定相同的文件:

<spleen>
   <fl path='/home/the_spleen/'><!-- fl-ify! --></fl>
   <fl path='/home/the_bowler/not_enough_beer/'><!-- drink! --></fl>
</spleen>

好处是什么?唯一文件基名和节点之间存在1:1的关系。损害?您需要添加递归逻辑

考虑到这些选择,我个人会选择第一个。添加递归逻辑似乎会导致各种各样的不好和恼人的重新分解。但我从这里只能看到你的项目的有限视图