2012-06-17 4 views
-2

가능한 중복의 또 다른주의 사항 :
A simple program to CRUD node and node values of xml fileSimpleXML을

이 예제 XML 파일입니다 (모든 요소) 다른 note을 추가하는 방법

<?xml version="1.0" encoding="ISO-8859-1"?> 
<notes> 
    <note> 
     <to>Tove</to> 
     <from>Jani</from> 
     <heading>Reminder</heading> 
     <body>Don't forget me this weekend!</body> 
    </note> 
</notes> 

SimpleXML을 (를) 사용 하시겠습니까?
addChild()은 내가 아는 한 기존 트리에 자식 만 추가합니다.

답변

0

음, 우선 나는 더 나은 참조를 얻을 것입니다. 어쩌면 훌륭한 자원 인 PHP 매뉴얼 : 이것은 당신의 notes 목록에 노드를 추가

http://www.php.net/manual/en/simplexml.examples-basic.php#example-5118

:

<?php 

$notesxml = <<<XML 
<?xml version="1.0" encoding="ISO-8859-1"?> 
<notes> 
    <note> 
     <to>Tove</to> 
     <from>Jani</from> 
     <heading>Reminder</heading> 
     <body>Don't forget me this weekend!</body> 
    </note> 
</notes> 
XML; 

$notes = new SimpleXMLElement($notesxml); 

$note = $notes->addChild('note'); 
$note->addChild('to', 'Yourself'); 
$note->addChild('from', 'Billy Brown'); 
$note->addChild('heading', 'Another note'); 
$note->addChild('body', 'This is the body'); 

echo $notes->asXML(); 

?> 

http://codepad.org/QrlU5CYm

는 당신이 바로 그 일을 찾고 있습니다. 어떤 형식 으로든 XML을 먼저로드해야합니다.

+0

감사합니다.하지만 기존 파일이 있으므로 새 파일을 만드는 대신로드해야합니다. 당신은 또한 예제가 있습니까? – user1170330

+1

와우 자레드 (Wow Jared)는 당신에게 멋진 답변을 쓸 시간을 가졌으며, 계속 더 많은 코드를 요구하고 있습니다. @ 제러드 : 왜 대답을 삭제하지? 그리고 나에게 현상금을 넣을 수있는 이전 답변 중 하나에 대한 링크를 건네 줘? – hakre

+0

@ user1170330 - [이 기능들] (http://www.php.net/manual/en/ref.simplexml.php) 중 하나가 올바른 것이라는 3 가지 추측이 있지만, 하나의 추측으로 충분합니다. –