>  기사  >  백엔드 개발  >  PHP로 XML 문서 만들기

PHP로 XML 문서 만들기

巴扎黑
巴扎黑원래의
2016-11-23 09:56:33959검색

<?php
// 创建DOM
$dom = new DOMDocument("1.0", "UTF-8");
$dom -> formatOutput = true;
// 创建元素
$phpedu = $dom -> createElement("phpedu");
// 添加元素到父节点
$dom -> appendChild($phpedu);
$title = $dom -> createElement("title", "PHP100");
$phpedu -> appendChild($title);
// 创建元素
$item = $dom -> createElement("item");
// 设置属性
$item -> setAttribute("type", "text");
// 设置属性
$item -> setAttribute("name", "item");
// 添加元素到父节点
$phpedu -> appendChild($item);
$php = $dom -> createElement("contents", "PHP");
$php -> setAttribute("type", "text");
$item -> appendChild($php);
$sql = $dom -> createElement("contents", "SQL");
$item -> appendChild($sql);
$linux = $dom -> createElement("contents", "Linux");
$item -> appendChild($linux);
$apache = $dom -> createElement("contents", "Apache");
$item -> appendChild($apache);
$address = $dom -> createElement("Address", "www.php100.com");
$address -> setAttribute("type", "URL");
$phpedu -> appendChild($address);
echo htmlspecialchars($dom -> saveXML());
?>

설명:

$dom = new DOMDocument("1.0", "UTF-8"); DOM을 생성하고 해당 인코딩 방법을 지정합니다. 기본 인코딩 방법은 UTF-8입니다. 이 샘플 코드 줄은 $dom = new DOMDocument(); 기본 인코딩을 사용하는 것과 동일합니다.

요소 생성: $phpedu = $dom -> createElement("phpedu"); 요소 생성, 이름: $title = $dom -> ) ;요소를 생성하고 그 값을 할당합니다.

상위 노드에 요소 추가: $dom ->appendChild($phpedu);

요소의 속성 설정: $item -> text"); 여러 속성을 요소에 추가할 수 있습니다.

한 가지 더: 도움이 되셨으면 좋겠습니다. 코드 소스: www.php100.com =^_^=


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.