insertBefore以错误的方式向xml添加对象

时间:2013-03-31 20:49:37

标签: php xml dom xml-parsing

基本上,当我尝试使用php将RSS源中的多个项目添加到XML文件时,insertBefore类以错误的方式添加最新项目(即,我希望它将它们添加到文件的顶部,从最旧到最新,但它将它们添加到最新到最旧)我该如何解决这个问题? (抱歉可怕的措辞)这是我的代码:

$dom = new DOMDocument;
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;


$dom->load(myfile);

$channel = $dom->getElementsByTagName('channel');

foreach ($channel as $channels) {

$item1 = $dom->createElement('item');
$item2 = $dom->createElement('item');
$item3 = $dom->createElement('item');

$title1 = $dom->createElement('title', $t1);
$title2 = $dom->createElement('title', $t2);
$title3 = $dom->createElement('title', $t3);

$content1 = $dom->createElement('media:content'); 
$conatt1 = $dom->createAttribute('url');

$content2 = $dom->createElement('media:content'); 
$conatt2 = $dom->createAttribute('url');

$content3 = $dom->createElement('media:content'); 
$conatt3 = $dom->createAttribute('url');

$conatt1->value = $u1;
$conatt2->value = $u2;
$conatt3->value = $u3;

$channels->insertBefore($item1, $channels->firstChild);  
$channels->insertBefore($item2, $channels->firstChild);  
$channels->insertBefore($item3, $channels->firstChild);  

$item1->appendChild($title1); 
$item2->appendChild($title2); 
$item3->appendChild($title3); 

$item1->appendChild($content1); 
$item2->appendChild($content2); 
$item3->appendChild($content3); 

$content1->appendChild($conatt1);
$content2->appendChild($conatt2);
$content3->appendChild($conatt3);

}

及其制作的XML =

<channel>
 <item>
  <title>item3</title>
  <media:content url="url3"/>
 </item>
 <item>
  <title>item2</title>
  <media:content url="url2"/>
 </item>
 <item>
  <title>item1</title>
  <media:content url="url"/>
 </item>

//rest of the RSS 
</channel>

但我想要它,所以它添加了这样的项目:

<channel>
 <item>
  <title>item1</title>
  <media:content url="url1"/>
 </item>
 <item>
  <title>item2</title>
  <media:content url="url2"/>
 </item>
 <item>
  <title>item3</title>
  <media:content url="url3"/>
 </item>

//rest of the RSS 
</channel>

任何建议(再次,抱歉可怕的解释)

修改

这是我的其他代码,我将如何对此进行相同的操作?

$fom = new DOMDocument;
$fom->formatOutput = true;
$fom->preserveWhiteSpace = false;

$fom->load("file");
$channel = $fom->getElementsByTagName('channel');

foreach ($channel as $channels) {

$item1 = $fom->createElement('item');
$title1 = $fom->createElement('title', $titles[$i]);

$content1 = $fom->createElement('media:content'); 
$conatt1 = $fom->createAttribute('url');

$conatt1->value = $links[$i];

$channels->insertBefore($item1, $channels->firstChild);
$item1->appendChild($title1); 
$item1->appendChild($content1);  
$content1->appendChild($conatt1);

$fom->formatOutput = true;
$fom->preserveWhiteSpace = false;
$fom->save("file");
}
}

1 个答案:

答案 0 :(得分:0)

首先你做

$channels->insertBefore($item1, $channels->firstChild);

此次通话后,$item1已成为$channels的第一个孩子。因此,当您在下一行时,

$channels->insertBefore($item2, $channels->firstChild);

它在$item2之前插入$item1

相反,您应该将$channels->firstChild返回的第一个值缓存到变量中,然后insertBefore缓存:

$insertPoint = $channels->firstChild;
$channels->insertBefore($item1, $insertPoint);
$channels->insertBefore($item2, $insertPoint);
$channels->insertBefore($item3, $insertPoint);

现在$insertPoint始终是您开始插入新项目之前$channels的第一个孩子的元素。