PHP 保存 XML 和数组


PHP Saving XML and Arrays

我有一个通过 php 创建的 xml 文件。基本结构为:

    <catgories>         *this is root*
        <category>      *element*
             <title>xy</title>  *attribute*
             <desc>yu</desc>    *attribute*
          <categoryLeaf>        *child element of category*
                 <title>rt</title>  *attribute of categoryLeaf*
          </categoryLeaf>
          <endtvod></endtvod>
       </category>
     </categories>

xml是一次性动态创建的,但categoryLeaf及其属性除外,这些属性是在单独的函数中创建的。XML 已正确创建。为了获得 categoryLeaf,我正在解析一个 html 并提取信息并将该信息存储在具有以下内容的数组中:

      if (!empty($dom)) {     //IF NOT EMPTY FIND CATEGORIES
        $clsort = $xpath->query('//div[@class="sort"]');
        foreach($clsort as $clsorts){
        $li= $clsorts->getElementsByTagName('li');
        foreach ($li as $lis){
        $links=$lis->getElementsByTagname('a');
        foreach ($links as $link){
        $href=$link->getAttribute('href');
        $text=$link->nodeValue;
        if ($text=="#"){
            break;
        }else{
        $textarray=$text;
        ECHO $textarray."<br>";
        CategoryLeafXml($textarray);
        }
        }
        }
           }
           }
         else
           {
         echo "EMPTY CONTAINER". "<br>";
         }

所需的信息通过上面的代码获得并添加到文本数组中。我可以回应它,每个人都出现。我将数组传递给一个函数,该函数将打开 xml,插入 categoryLeaf 以及 title 属性并保存代码:

             Function CategoryLeafXml($textarray){
             $xml = new DOMDocument('1.0', 'UTF-8');
             $xml->formatOutput = true;
             $xpath = new DOMXPath($xml);
             $xml-> loadXML ('.''xml''test.xml');
             $arrcount=count($textarray);
             echo $arrcount."<br>";
             $categoryLeaf=$xml->createElement("categoryLeaf");
            for ($i=0; $i<$arrcount;$i++){
            echo $textarray."<br>";
            $endtvod=$xml->getElementsByTagName('endtvod')->item(0);  //LOCATE tag
            $category=$xml->getElementsByTagName('category');
            $categories=$xml->getElementsByTagName('categories');
            $newleaf=$xml->insertBefore($categoryLeaf, $endtvod);
            $title = $xml->createAttribute("title");
            $title->value= $textarray;
            $categoryLeaf->appendChild($title);
            $xml->save('.''xml''test.xml'); 
            }
            }      

假设发生的事情是创建xml结构,html被解析到textarray中,textarray被传递给在元素tvod之前插入元素categoryLeaf的函数。文本数组是 categoryLeaf/title 的属性值。 我遇到了 2 个问题,(1)打开了 xml 并创建了 categoryLeaf,但是保存后结果为:

      <?xml version="1.0" encoding="UTF-8"?>
      <categoryLeaf title="value of textarray"/>

xml将被覆盖,只留下一个插入的元素和最后一个数组值。(2) 数组计数始终显示 1,for 循环仅运行一次,仅写入数组的最后一个值。我预计会为数组计数的值添加一个 categoryLeaf,但它只将该值视为一个。我知道数组中大约有 25 个条目。

您仅通过以下方式创建单个 categoryLeaf 节点:

$categoryLeaf=$xml->createElement("categoryLeaf")

并更改属性 n 次。

您必须在 for 循环中创建 categoryLeaf。

我也不明白你为什么要这样做:

$category=$xml->getElementsByTagName('category');
$categories=$xml->getElementsByTagName('categories');

这似乎在函数内部没有任何作用。

试试这样的事情:

Function CategoryLeafXml($textarray){
         $xml = new DOMDocument('1.0', 'UTF-8');
         $xml->formatOutput = true;
         $xpath = new DOMXPath($xml);
         $xml-> loadXML ('.''xml''test.xml');
         $endtvod = $xml->getElementsbyTagName('endtvod')->item(0)
        foreach ($textarray as $text){
            $categoryLeaf=$xml->createElement("categoryLeaf");
            $categoryLeaf->setAttribute('title', $text);
            $endtvod->parentNode->insertBefore($categoryLeaf, $enttvod)
        }
        $xml->save('.''xml''test.xml'); 
}