2013-11-01 3 views
2

이번 주 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"요소를 추가하고 하위 요소를 최신 요소에만 추가하십시오.

도움말/도움말을 보내 주시면 감사하겠습니다.

+0

파일을 편집하여 파일을 항상 다시 만들어야합니다. – hakre

답변

1

먼저, simplexml_DOMDocument 기능을 함께 사용하지 마십시오. 전자는 후자에 대한 래퍼입니다 (내 의견으로는 특히 좋은 것은 아닙니다). 내가 너라면, DOMDocument을 사용하겠다.

$doc = new DOMDocument('1.0'); 
$doc->formatOutput = true; 
$doc->preserveWhiteSpace = true; 

$doc->load('posts.xml', LIBXML_NOBLANKS); // load the posts file with DOMDocument 

$newPost = $doc->createElement('post'); // create a new <post> element 
$newPost->appendChild($doc->createElement('name', $postname)); 
$newPost->appendChild($doc->createElement('date', $postdate)); 
$newPost->appendChild($doc->createElement('time', $posttime)); 
$newPost->appendChild($doc->createElement('text', $posttext)); 

$document->documentElement->appendChild($newPost); // add the new <post> to the document 

$doc->save('posts.xml'); 
+0

팁과 솔루션을 제공해 주셔서 감사합니다. 이것은 매력처럼 작동했습니다! – DesignedMoodily

0

파일을 편집 할 수 있도록 먼저 열어야합니다. 그렇지 않으면 파일을 추가하는 대신 항상 전체 문서를 교체해야합니다. 여기

이가 지금까지 SimpleXML을 작동 계속 작업 할 수있을만큼 간단 방법에 대한 간단한 예입니다

$file = 'posts.xml'; 

$xml = simplexml_load_file($file); // load existing file 
$post = $xml->addChild('post'); // add new post child 

// assign values to the post object: 
$post->name = $_POST["name"]; 
$post->date = $_POST["date"]; 
$post->time = $_POST["time"]; 
$post->text = $_POST["posttext"]; 

$xml->saveXML($file); //save file with changes 

을 ... 그리고 그것에 완벽하게 호환 자매 라이브러리에있는 DOMDocument이 경우의 몇 가지가 필요합니다 기능을 제공합니다. 동일한 메모리 개체를 공유합니다.

관련 문제