2016-11-29 5 views
1

XML 파일에 추가 할 간단한 simplexml PHP 파일을 사용하고 있습니다. 어떤 이유로 XML 파일에 기록을 남기지 않고 내 로그가 표시됩니다.SimpleXML 오류가있는 PHP를 사용하여 XML 파일에 추가

PHP Fatal error: Call to a member function addChild()

누군가 나를 도와 줄 수 있습니까? 정말 간단 할 수도 있습니다. 나는 PHP 코드와 XML 파일을 첨부하고있다.

PHP :

<?php 
$file = 'news.xml'; 

$xml = simplexml_load_file($file); 

$story = $xml->news->story; 

$story->addChild('storyid', '33'); 
$story->addChild('story_pic', 'images/test.jpg'); 
$story->addChild('story_title', 'Your Title'); 
$story->addChild('story', 'This is a test story'); 
$story->addChild('story_date', '01/01/2001'); 

$xml->asXML($file); 
?> 

XML

<?xml version = "1.0" encoding="Windows-1252" standalone="yes"?> 
<news> 
    <story> 
     <storyid>1</storyid> 
     <story_pic>http://portfoliotheme.org/enigmatic/wp-content/uploads/sites/9/2012/07/placeholder1.jpg</story_pic> 
     <story_title>My Test News Story</story_title> 
     <story>fdsfsfsdfdsfdsfsfsdfdsfsfdffsfdssfd</story> 
     <story_date>22/01/2016</story_date> 
    </story> 
    <story> 
     <storyid>2</storyid> 
     <story_pic>http://portfoliotheme.org/enigmatic/wp-content/uploads/sites/9/2012/07/placeholder1.jpg</story_pic> 
     <story_title>My Test News Story2</story_title> 
     <story>fdsfsfsdfdsfdsfsfsdfdsfsfdffsfdssfd</story> 
     <story_date>22/01/2016</story_date> 
    </story> 
</news> 
+0

당신이'$의 story'의 가치를 확인해 봤어? –

답변

2

확실히,이 aswered 된 수천 시간 만 :

$file = './news.xml'; 

$xml = simplexml_load_file($file); 
// `$xml` presents root item (`news`) of your xml tree 

// add new `story` to your root item (`news`) 
$new_story = $xml->addChild('story'); 

// add items to newly added `$new_story` element 
$new_story->addChild('storyid', '33'); 
$new_story->addChild('story_pic', 'images/test.jpg'); 
$new_story->addChild('story_title', 'Your Title'); 
$new_story->addChild('story', 'This is a test story'); 
$new_story->addChild('story_date', '01/01/2001'); 

// save as XML 
$xml->asXML($file); 
+0

건배, 완벽하게 작동합니다. –