을 dom으로 :PHP로 XML 파일을 조작 나는 다음과 같이 XML 파일이
<?xml version="1.0" encoding="UTF-8"?>
<root version="8.0.0.0">
<songs name="Album">
<row>
<song artist="artist_name">Track1</song>
</row>
<row>
<song artist="artist_name">Track2</song>
</row>
<row>
<song artist="artist_name">Track3</song>
</row>
<row>
<song artist="artist_name">Track4</song>
</row>
</songs>
</root>
지금 내가 좀 더 행이이 파일을 업데이트 할를. 기존 행 요소 위에 데이터를 추가하는 방법은 무엇입니까? 또한 새로운 요소를 추가하는 동안 Track1, Track2와 같은 트랙을 확인하려고합니다.
현재이 xml 파일을 php : dom으로 조작하고 있지만, 기존 행 맨 아래에 추가하는 데이터입니다. 위의 일을하는 데 사용
PHP 코드는 내가 그것을 할 수있는 방법을 알려 주시기 바랍니다
<?php
//Creates XML string and XML document using the DOM
$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->Load('C:/wamp/www/xml/test1.xml');
$root = $dom->firstChild;
$list = $root->childNodes->item(1);
if(isset($_POST['submit'])){
$artistName = $_POST['name'];
$track = $_POST['track'];
$row = $dom->createElement('row');
$list->appendChild($row);
$song = $dom->createElement('song');
$row->appendChild($song);
$song->setAttribute('artist', $artistName);
$wcm_node = $dom->createTextNode($track);
$song->appendChild($wcm_node);
}
// Code to format XML after appending data
$outXML = $dom->saveXML(); // put string in outXML
//now create a brand new XML document
$xml = new DOMDocument();
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true; //yup, going to try to make this format again
//pass the output of the first bunch of stuff we did to the new XML document:
$xml->loadXML($outXML);
//now display the cleanly formatted output without using LIBXML_NOBLANKS (which may have undesired consequences
$xml->save('test1.xml'); // save as file
}
?>
입니다.를 붙이는하지만 를 추가하지 것
감사
우리에게 약간의 PHP 코드를 제시해주십시오 – soju
수 DOM도 그 방법을 가지고 있습니다 [PHP XML 삽입 요소 다음에 중복 된 포어) 다른 요소] (http://stackoverflow.com/questions/4723747/php-xml-inserting-element-after-or-before-another-element) – Gordon