이번 주 PHP를 가르치고 테스트 프로젝트로서 XML 데이터를 사용하여 간단한 게시물 정보를 저장/검색하는 매우 간단한 마이크로 블로그를 작성했습니다. 나는 this question을 참조했으며, 필자가 원했던 것과 유사한 XML 문서를 생성 할 수있게되었습니다.새로운 요소로 XML 생성하기
그러나 나는 스스로 알아낼 수없는 한 가지 문제에 부딪쳤다. 링크 된 솔루션에서, 동일한 개체는 새로운 정보없이 반복해서 업데이트된다가 투입되고 :
예에서 '제 시험 후'
<postslist>
<post>
<name>Third Post</name>
<date>2013-11-05</date>
<time>00:00</time>
<text>There is some more post text here.</text>
</post>
</postslist>
하고 '제 시험 후' :
<postslist>
<post>
<name>Fourth Post</name>
<date>2013-11-05</date>
<time>00:00</time>
<text>There is even more post text here.</text>
</post>
</postslist>
내 PHP는 thusfar이 유사합니다 내가 뭘 바라고하는 것은 여러 만들
$postname = $_POST["name"];
$postdate = $_POST["date"];
$posttime = $_POST["time"];
$posttext = $_POST["posttext"];
$postname = htmlentities($postname, ENT_COMPAT, 'UTF-8', false);
$postdate = htmlentities($postdate, ENT_COMPAT, 'UTF-8', false);
$posttime = htmlentities($posttime, ENT_COMPAT, 'UTF-8', false);
$posttext = htmlentities($posttext, ENT_COMPAT, 'UTF-8', false);
$xml = simplexml_load_file("posts.xml");
$xml->post = "";
$xml->post->addChild('name', $postname);
$xml->post->addChild('date', $postdate);
$xml->post->addChild('time', $posttime);
$xml->post->addChild('text', $posttext);
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('posts.xml');
입니다 "post"요소를 추가하고 하위 요소를 최신 요소에만 추가하십시오.
도움말/도움말을 보내 주시면 감사하겠습니다.
파일을 편집하여 파일을 항상 다시 만들어야합니다. – hakre