在以错误的方式向xml添加对象之前


insertBefore adding objects to xml the wrong way

基本上,当我试图使用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");
}
}

首先你要做

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

调用后,$item1成为$channels的第一个子节点。因此当你在下一行输入

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

$item1之前插入$item2

相反,您应该将$channels->firstChild返回的第一个值缓存在一个变量中,然后将insertBefore缓存为:
$insertPoint = $channels->firstChild;
$channels->insertBefore($item1, $insertPoint);
$channels->insertBefore($item2, $insertPoint);
$channels->insertBefore($item3, $insertPoint);

现在$insertPoint总是$channels的第一个子元素,在你开始插入新项之前。