-
php xml 다루기Full-Stack/Back-end 2008. 9. 5. 14:00<?php
// DOM 객체 및 루트앨리먼트 생성
$doc = new DOMDocument();
$doc->loadXML("<books></books>");
//<book> 앨리먼트 생성. 생성 후 추가해야 함
$bookNode = $doc->createElement("book"); // 노드 생성
$doc->documentElement->appendChild($bookNode); // 노드 추가
$isbnNode = $doc->createAttribute("isbn"); // 새로생성된 book노드에 isbn 속성 생성 및 추가
$isbnNode->value="00000001";
$bookNode->setAttributeNode($isbnNode);
//<book> 앨리먼트 생성. 생성 후 추가해야 함
$bookNode = $doc->createElement("book"); // 노드 생성
$doc->documentElement->appendChild($bookNode); // 노드 추가
$isbnNode = $doc->createAttribute("isbn"); // 새로생성된 book노드에 isbn 속성 생성 및 추가
$isbnNode->value="00000002";
$bookNode->setAttributeNode($isbnNode);
//<subject> 앨리먼트 생성. 생성 후 추가해야 함
$subjectNode = $doc->createElement("subject","Test Book01"); // 노드 생성
$doc->documentElement->firstChild->appendChild($subjectNode); // 노드 추가
//<authors> 앨리먼트 생성. 생성 후 추가해야 함
$authorsNode = $doc->createElement("authors"); // 노드 생성
$doc->documentElement->firstChild->appendChild($authorsNode); // 노드 추가
$authorsNode->appendChild(new DOMElement("author","Craig Walls"));
$authorsNode->appendChild(new DOMElement("author","Norman Richards"));
echo "<PRE>";
echo htmlentities($doc->saveXML());
echo "</PRE>";
echo "nodeName : ".$doc->documentElement->nodeName;
echo "<br>";
echo "nodeName : ".$doc->documentElement->firstChild->nodeName;
echo "<br>";
echo "nodeName : ".$doc->documentElement->firstChild->firstChild->nextSibling->parentNode->nodeName;
echo "<br>";
echo "nodeName : ".$doc->documentElement->firstChild->firstChild->nextSibling->previousSibling->nodeName;
echo "<br>";
echo "nodeValue : ".$doc->documentElement->firstChild->firstChild->nodeValue;
echo "<br>";
echo "nodeValue : ".$doc->documentElement->firstChild->firstChild->nextSibling->firstChild->nodeValue;
echo "<br>";
echo "nodeValue : ".$doc->documentElement->firstChild->firstChild->nextSibling->lastChild->nodeValue;
echo "<br>";
echo "nodeName : ".$bookNode->nodeName;
echo "<br>";
echo "nodeName : ".$subjectNode->nodeName;
echo "<br>";
echo "nodeName : ".$authorsNode->nodeName;
echo "<br>";
echo "nodeName : ".$authorsNode->nodeName;
echo "<br>";
echo "nodeType : ".$doc->documentElement->firstChild->nodeType;
echo "<br>";
echo "getAttribute : ".$bookNode->getAttribute("isbn");
echo "<br>";
echo "getNamedItem isbn : ".$doc->documentElement->firstChild->attributes->getNamedItem("isbn")->nodeValue;
echo "<br>";
echo "------------------------<br>";
$params = $doc->getElementsByTagName("book");
foreach ($params as $param) {
echo $param -> getAttribute("isbn")."<br>";
echo $param -> nodeValue."<br>";
}
echo "------------------------<br>";
$params = $doc->getElementsByTagName("author");
foreach ($params as $param) {
echo $param -> nodeValue."<br>";
}
echo "------------------------<br>";
/* 결과 : tab 적용없이 element 가 모두 붙여서 출력됨, 아래쪽은 보기좋게 만들었음
<books>
<book isbn="00000001">
<subject>Test Book01</subject>
<authors>
<author>Craig Walls</author>
<author>Norman Richards</author>
</authors>
</book>
<book isbn="00000002"/>
</books>
nodeName : books
nodeName : book
nodeName : book
nodeName : subject
nodeValue : Test Book01
nodeValue : Craig Walls
nodeValue : Norman Richards
nodeName : book
nodeName : subject
nodeName : authors
nodeName : authors
nodeType : 1
getAttribute : 00000002
getNamedItem isbn : 00000001
------------------------
00000001
Test Book01Craig WallsNorman Richards
00000002
------------------------
Craig Walls
Norman Richards
------------------------
*/
?>
<?php
$doc = new DOMDocument("1.0");
$root = $doc->createElement("HTML");
$root = $doc->appendChild($root);
$head = $doc->createElement("HEAD");
$head = $root->appendChild($head);
$title = $doc->createElement("TITLE");
$title = $head->appendChild($title);
$text = $doc->createTextNode("This is the title");
$text = $title->appendChild($text);
echo "<PRE>";
echo htmlentities($doc->saveXML());
echo "</PRE>";
/* 결과
<HTML><HEAD><TITLE>This is the title</TITLE></HEAD></HTML>
*/
?>
<?php
//$doc = new DOMDocument("1.0");
$doc = new DOMDocument('1.0', 'euc-kr');
$root = $doc->createElement("root");
$root = $doc->appendChild($root);
// a
$a = $doc->createElement("a"); //createElement()
$a = $root->appendChild($a); //appendChild()
$aaa = $doc->createElement("aaa");
$aaa = $a->appendChild($aaa);
$aaatext = $doc->createTextNode("This is the text 01"); //createTextNode()
$aaatext = $aaa->appendChild($aaatext);
$aaa = $doc->createElement("aaa");
$aaa = $a->appendChild($aaa);
$aaatext = $doc->createTextNode("This is the text 02");
$aaatext = $aaa->appendChild($aaatext);
$root->setAttribute("num","1.0"); //setAttribute()
$root->setAttribute("order","1.0"); //setAttribute()
$root->firstChild->setAttribute("order","2.0"); //setAttribute()
$a->setAttribute("num","2.0");
$aaa->setAttribute("num","3.0");
$aaa_attri = $doc->createAttribute("order"); //속성 생성 및 노드에 속성 추가
$aaa_attri->value="3.0";
$aaa->setAttributeNode($aaa_attri);
// b
$b = $doc->createElement("b");
$b = $root->appendChild($b);
$bbb = $doc->createElement("bbb");
$bbb = $b->appendChild($bbb);
$bbb = $doc->createElement("bbb");
$bbb = $b->appendChild($bbb);
// c
$c = $doc->createElement("c");
$c = $root->appendChild($c);
$ccc = $doc->createElement("ccc");
$ccc = $c->appendChild($ccc);
$ccc = $doc->createElement("ccc");
$ccc = $c->appendChild($ccc);
echo "<PRE>";
echo htmlentities($doc->saveXML());
echo "</PRE>";
/*결과
<root num="1.0" order="1.0">
<a order="2.0" num="2.0">
<aaa>This is the text 01</aaa>
<aaa num="3.0" order="3.0">This is the text 02</aaa>
</a>
<b>
<bbb/>
<bbb/>
</b>
<c>
<ccc/>
<ccc/>
</c>
</root>
*/
?>
<?php
$doc = new DOMDocument("1.0");
$root = $doc->createElement("HTML");
$root = $doc->appendChild($root);
$head = $doc->createElement("HEAD");
$head = $root->appendChild($head);
$title = $doc->createElement("TITLE");
$title = $head->appendChild($title);
$text = $doc->createTextNode("This is the title");
$text = $title->appendChild($text);
//$doc->save("/tmp/test.xml"); // xml 파일로 생성하기
// xml 파일로 저장할 수 있는 함수가 있어서 일단 테스트 해봤는데
// 저장할 폴더의 퍼미션이 777 이 되어야 파일을 생성하고 저장할 수 있다.
?>
<?php
$doc = new DOMDocument();
$doc->load("xml_test01.xml");
echo "<PRE>";
echo htmlentities($doc->saveXML());
echo "</PRE>";
// xml 파일 로딩해서 보여주기
// 참고: http://au.php.net/manual/kr/function.dom-domdocument-load.php
?>
출처 : Tong - BlueSky_08님의 APM통댓글