DOM DOMDocument::saveXML

(PHP 5, PHP 7, PHP 8)

DOMDocument::saveXML — 내부 XML 트리를 다시 문자열로 덤프합니다.


설명

public DOMDocument::saveXML(?DOMNode $node = null, int $options = 0): string|false

DOM 표현에서 XML 문서를 만듭니다. 이 함수는 일반적으로 아래 예제와 같이 처음부터 새 dom 문서를 빌드한 후 호출됩니다.


매개변수

node
전체 문서가 아닌 XML 선언 없이 특정 노드만 출력하려면 이 매개변수를 사용합니다.
options
추가 옵션. 현재 LIBXML_NOEMPTYTAG만 지원됩니다.

반환 값

XML을 반환하거나 오류가 발생한 경우 false를 반환합니다.


오류/예외

DOM_WRONG_DOCUMENT_ERR
node가 다른 문서에서 온 경우 발생합니다.

Examples

예제 #1 DOM 트리를 문자열로 저장

                  
<?php

$doc = new DOMDocument('1.0');
// we want a nice output
$doc->formatOutput = true;

$root = $doc->createElement('book');
$root = $doc->appendChild($root);

$title = $doc->createElement('title');
$title = $root->appendChild($title);

$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);

echo "Saving all the document:\n";
echo $doc->saveXML() . "\n";

echo "Saving only the title part:\n";
echo $doc->saveXML($title);

?>
                  
                

위의 예는 다음을 출력합니다.

Saving all the document:
<?xml version="1.0"?>
<book>
  <title>This is the title</title>
</book>

Saving only the title part:
<title>This is the title</title>
                

기타