미니옵빠의 code stubs

DOM함수로 XML 만들기 - DOMDocument() 본문

Language/PHP

DOM함수로 XML 만들기 - DOMDocument()

미니옵빠 2011. 8. 28. 19:50
출처 What you see is what you get | 아킨토스
원문 http://blog.naver.com/etwas0227/60027897049

XXIV. DOM Functions

소개

The DOM extension is the replacement for the domxml extension from PHP 4. The extension still contains many old functions, but they should no longer be used. In particular, functions that are not object-oriented should be avoided.

The extension allows you to operate on an XML document with the DOM API.

 

 

참고 함수들: http://au.php.net/manual/kr/ref.dom.php

 

 

 

-----------------------------------------------------------------------------------

 

 

 

 

< 테스트 참고 소스 >

 

 

// DOM 객체 및 루트앨리먼트 생성
$doc = new DOMDocument();
$doc->loadXML("");


// 앨리먼트 생성. 생성 후 추가해야 함
$bookNode = $doc->createElement("book"); // 노드 생성
$doc->documentElement->appendChild($bookNode); // 노드 추가

$isbnNode = $doc->createAttribute("isbn"); // 새로생성된 book노드에 isbn 속성 생성 및 추가
$isbnNode->value="00000001";
$bookNode->setAttributeNode($isbnNode);


// 앨리먼트 생성. 생성 후 추가해야 함
$bookNode = $doc->createElement("book"); // 노드 생성
$doc->documentElement->appendChild($bookNode); // 노드 추가

$isbnNode = $doc->createAttribute("isbn"); // 새로생성된 book노드에 isbn 속성 생성 및 추가
$isbnNode->value="00000002";
$bookNode->setAttributeNode($isbnNode);


// 앨리먼트 생성. 생성 후 추가해야 함
$subjectNode = $doc->createElement("subject","Test Book01"); // 노드 생성
$doc->documentElement->firstChild->appendChild($subjectNode); // 노드 추가


// 앨리먼트 생성. 생성 후 추가해야 함
$authorsNode = $doc->createElement("authors"); // 노드 생성
$doc->documentElement->firstChild->appendChild($authorsNode); // 노드 추가

$authorsNode->appendChild(new DOMElement("author","Craig Walls"));
$authorsNode->appendChild(new DOMElement("author","Norman Richards")); 


echo "

";
echo htmlentities($doc->saveXML());
echo "
";

echo "nodeName : ".$doc->documentElement->nodeName;
echo "
";
echo "nodeName : ".$doc->documentElement->firstChild->nodeName;
echo "
";
echo "nodeName : ".$doc->documentElement->firstChild->firstChild->nextSibling->parentNode->nodeName;
echo "
";
echo "nodeName : ".$doc->documentElement->firstChild->firstChild->nextSibling->previousSibling->nodeName;
echo "
";
echo "nodeValue : ".$doc->documentElement->firstChild->firstChild->nodeValue;
echo "
";
echo "nodeValue : ".$doc->documentElement->firstChild->firstChild->nextSibling->firstChild->nodeValue;
echo "
";
echo "nodeValue : ".$doc->documentElement->firstChild->firstChild->nextSibling->lastChild->nodeValue;
echo "
";

echo "nodeName : ".$bookNode->nodeName;
echo "
";
echo "nodeName : ".$subjectNode->nodeName;
echo "
";
echo "nodeName : ".$authorsNode->nodeName;
echo "
";
echo "nodeName : ".$authorsNode->nodeName;
echo "
";

echo "nodeType : ".$doc->documentElement->firstChild->nodeType;
echo "
";
echo "getAttribute : ".$bookNode->getAttribute("isbn");
echo "
";
echo "getNamedItem isbn : ".$doc->documentElement->firstChild->attributes->getNamedItem("isbn")->nodeValue;
echo "
";

echo "------------------------
";

$params = $doc->getElementsByTagName("book");

foreach ($params as $param) {
       echo $param -> getAttribute("isbn")."
";
       echo $param -> nodeValue."
";
}

echo "------------------------
";

$params = $doc->getElementsByTagName("author");

foreach ($params as $param) {
       echo $param -> nodeValue."
";
}

echo "------------------------
";

 


/* 결과 : tab 적용없이 element 가 모두 붙여서 출력됨, 아래쪽은 보기좋게 만들었음


 Test Book01
 

  
Craig Walls
  
Norman Richards
 


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
------------------------

*/

 

?>

 

 

 


 

$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 "

";
echo htmlentities($doc->saveXML());
echo "
";

/* 결과

*/

 

?>

 

 

 


 

//$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 "

";
echo htmlentities($doc->saveXML());
echo "
";


/*결과


 
This is the text 01
 
This is the text 02


 
 


 
 


*/

 

?>


 

 

 


$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 이 되어야 파일을 생성하고 저장할 수 있다.

 

?>