2016-10-02 3 views
0

저는 이것이 간단해야한다는 것을 알고 있습니다. 그러나 저는 PHP를 처음 접했고 필자가 만났던 문서를 이해하지 못했습니다. 나는 간단한 설명이 필요하다.PHP appendChild to XML 루트

노드를 추가 할 XML 문서가 있습니다. 문서에 노드를 추가 할 수 있습니다. 노드는 루트 노드 외부에 표시되어 이후 시도에서 오류가 발생합니다. 여기

<?xml version="1.0" encoding="UTF-8"?> 
<root> 
</root> 

내 PHP는 다음과 같습니다 :

여기 내 XML입니다

$playerID = "id_" . $_POST['player']; 

//load xml file to edit 
$xml = new DOMDocument(); 
$xml->load('../../saves/playerPositions.xml') or die("Error: Cannot load file."); 

//check if the player is already in the database 
if(isset($xlm->$playerID)){ 
    echo "Player ID was found."; 
} 
else{ 
    echo "Player ID was not found."; 

    //so we want to create a new node with this player information 
    $playerElement = $xml->createElement($playerID, ""); 
    $playerName = $xml->createElement("name", "John Doe"); 

    //add the name to the playerElement 
    $playerElement->appendChild($playerName); 

    //add the playerElement to the document 
    //THIS IS WHERE THE ERROR OCCURS 
    $xml->root->appendChild($playerElement); 

    //save and close the document 
    $xml->formatOutput = true; 
    $xml->save("../../saves/playerPositions.xml"); 
} 

echo "done"; 

난 그냥 나는이 문서를 수정할 수 있습니다 $xml->appendChild()를 사용하지만, 새로운 텍스트가 <root></root>의 외부에 표시되는 경우.

정확한 오류는 다음과 같습니다 $xmlDOMDocument의 인스턴스 ($xml 대신 SimpleXMLElement 있다면 그것은 작동)이기 때문에

Notice: Undefined property: DOMDocument::$root

+0

실제 코드,'$ xml-> root -> ...'또는'$ xml -> $ root -> ...'? – har07

+0

$ xml-> root-> appendChild()를 사용하면 DOMDocument :: $ root가됩니다. $ xml -> $ root-> appendChild()를 사용하면 DOMDocument :: root 오류가 발생합니다. 하지만 "루트"를 사용하는 것이 더 정확한 방법입니다.OP는 정확한 코드입니다. – user1544522

+0

그래도 오류 메시지가 코드와 일치하지 않으므로 혼란 스럽습니다. '$ xml-> root'에 오류 메시지가 표시됨 : * 치명적 오류 : null에 'appendChild()'멤버 함수 호출 * – har07

답변

1

$xml->root는이 컨텍스트에서 루트 요소에 액세스 할 수있는 올바른 방법이 아닙니다. 당신은 documentElement 재산에서 DOMDocument 객체의 루트 요소를 얻을 수 있습니다 :

$xml->documentElement->appendChild($playerElement); 

eval.in demo 1

또는 더 일반적으로, 당신은 getElementsByTagName() 사용하는 이름으로 요소를 얻을 수 있습니다 :

$xml->getElementsByTagName("root")->item(0)->appendChild($playerElement); 

eval.in demo 2

추가 읽기 : PHP DOM: How to get child elements by tag name in an elegant manner?


코드의이 제품은 또한 같은 이유로 올바르지 않습니다 말했다 그것은 (플러스 오타?) :

if(isset($xlm->$playerID)){ 
    echo "Player ID was found."; 
} 

getElementsByTagName()로 교체 :

if($xml->getElementsByTagName($playerID)->length > 0){ 
    echo "Player ID was found."; 
} 
+0

코드를 시작했을 때 SimpleXMLElement를 사용하려고 할 때' 파일을 읽지 마라. DOMDocument로 끝났습니다. DOMDocument가 다른 구조임을 깨닫지 못했습니다. 이 구문은 더 길지만 JavaScript와 XML과 같기 때문에이 구문이 더 편합니다. 당신의 도움을 주셔서 감사합니다. – user1544522

0

Standar Object처럼 dom을 처리하려고 시도하지만 그렇지 않습니다.

$xml = new DOMDocument(); 
    $xml->loadXML('<?xml version="1.0" encoding="UTF-8"?> 
<root> 
</root>') or die("Error: Cannot load file."); 

    $len = $xml->getElementsByTagName('player')->length; 
    if ($len > 0) { 

    } else { 
     $player = $xml->createElement('player', ''); 
     $playerName = $xml->createElement('playerName', "Jhon Doe"); 
     $player->appendChild($playerName); 

     $root = $xml->getElementsByTagName('root'); 

     if ($root->length > 0) { 
     $root[0]->appendChild($player); 
     } 

     $xml->formatOutput = true; 
     echo $xml->saveXML(); 
    } 

이 코드는 생산 : 요소를 찾아하려면 다음과 같이 함수 getElementsByTagName을 사용하고, 노드의 컬렉션처럼 DOM을 처리 할 필요가

당신이해야 할 일
<?xml version="1.0" encoding="UTF-8"?> 
<root> 
<player><playerName>Jhon Doe</playerName></player></root>