经过大量的工作,我完成了一个xml读写器... 但我的问题是,我希望我使用我的表单插入的较新数据将添加到xml文件的顶部。 而不是在底部.. 让我给你举个例子: 这是 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<events>
<record>
<event>old event</event>
<eventDate>1/1/2009</eventDate>
<desc>old desc</desc>
</record>
<record>
<event>newer event</event>
<eventDate>12/12/2011</eventDate>
<desc>newer desc</desc>
</record>
</events>
现在.. 我希望 XML 文件如下所示:较新数据,然后旧数据
<?xml version="1.0" encoding="utf-8"?>
<events>
<record>
<event>newer event</event>
<eventDate>12/12/2011</eventDate>
<desc>newer desc</desc>
</record>
<record>
<event>old event</event>
<eventDate>1/1/2009</eventDate>
<desc>old desc</desc>
</record>
</events>
这是我的 add.php (从main.html中的表单获取 POST 信息的文件:
<?php
$record = array(
'event' => $_POST['event'],
'eventDate' => $_POST['eventDate'],
'desc' => $_POST['desc'],
);
$doc = new DOMDocument();
$doc->load( 'events.xml' );
$doc->formatOutput = true;
$r = $doc->getElementsByTagName("events")->item(0);
$b = $doc->createElement("record");
$event = $doc->createElement("event");
$event->appendChild(
$doc->createTextNode( $record["event"] )
);
$b->appendChild( $event );
$eventDate = $doc->createElement("eventDate");
$eventDate->appendChild(
$doc->createTextNode( $record["eventDate"] )
);
$b->appendChild( $eventDate );
$desc = $doc->createElement("desc");
$desc->appendChild(
$doc->createTextNode( $record["desc"] )
);
$b->appendChild( $desc );
$r->appendChild( $b );
$doc->save("events.xml");
header("Location: {$_SERVER['HTTP_REFERER']}");
?>
我需要编辑哪些内容才能根据需要制作* xml ? *
答案 0 :(得分:2)
使用
$r->insertBefore( $b,$r->firstChild );
而不是
$r->appendChild( $b );