저는 이것이 간단해야한다는 것을 알고 있습니다. 그러나 저는 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>
의 외부에 표시되는 경우.
정확한 오류는 다음과 같습니다 $xml
는 DOMDocument
의 인스턴스 ($xml
대신 SimpleXMLElement
있다면 그것은 작동)이기 때문에
Notice: Undefined property: DOMDocument::$root
실제 코드,'$ xml-> root -> ...'또는'$ xml -> $ root -> ...'? – har07
$ xml-> root-> appendChild()를 사용하면 DOMDocument :: $ root가됩니다. $ xml -> $ root-> appendChild()를 사용하면 DOMDocument :: root 오류가 발생합니다. 하지만 "루트"를 사용하는 것이 더 정확한 방법입니다.OP는 정확한 코드입니다. – user1544522
그래도 오류 메시지가 코드와 일치하지 않으므로 혼란 스럽습니다. '$ xml-> root'에 오류 메시지가 표시됨 : * 치명적 오류 : null에 'appendChild()'멤버 함수 호출 * – har07