Khi làm việc với các ứng dụng dựa trên XML, các nhà phát triển thường thấy mình phải đối mặt với yêu cầu tạo ra các cấu trúc dữ liệu được mã hóa XML một cách nhanh chóng. Hướng dẫn này giới thiệu cho bạn các chức năng chính trong API DOM của PHP, chỉ cho bạn cách tạo lập trình một tài liệu XML hoàn chỉnh được định dạng tốt từ đầu và lưu nó vào đĩa
Bài viết này cũng có sẵn dưới dạng bản tải xuống của TechRepublic, bao gồm danh sách mã ví dụ ở định dạng tệp văn bản dễ quản lý hơn
Dưới đây là mã để tạo tệp XML. Tôi đã đặt tên cho tệp đầu ra là 'ví dụ. xml'. Để hiểu rõ hơn, tôi đã viết bình luận cho hầu hết mọi dòng mã
<?php // Create a new DOMDocument object $dom = new DomDocument("1.0", "ISO-8859-1"); // optionally the following can be done // $dom->version = "1.0"; // $dom->encoding = "ISO-8859-1"; // Create new element node 'mobiles' $mobiles = $dom->createElement('mobiles'); // add $mobiles to the main dom $dom->appendChild($mobiles); // Suppose we have the following array of data $items = array(array('name'=>'samsung','price'=>'21000'), array('name'=>'sony','price'=>'25000')); foreach($items as $item) { // Create new element node 'brand' $brand = $dom->createElement('brand'); // Create new element node 'name' with the node value $name = $dom->createElement('name',$item['name']); // Create new element node 'price' with the node value $price = $dom->createElement('price',$item['price']); // add 'name' node to 'brand' node $brand->appendChild($name); // add 'price' node to 'brand' node $brand->appendChild($price); // add brand to mobile $mobiles->appendChild($brand); } // add spaces, new lines and make the XML more readable format $dom->formatOutput = true; // SET HEADER TO OUTPUT DATA header('HTTP/1.1 200 OK'); header("Pragma: no-cache"); header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate'); header('Content-type', 'text/xml'); header('Content-Disposition: attachment; filename="example_dom.xml"'); // Dump the internal XML tree back into a string echo $dom->saveXML(); ?>Tệp xml sau được tạo. Tệp xml có tên là example_dom. xml
<?xml version="1.0" encoding="ISO-8859-1"?> <mobiles> <brand> <name>samsung</name> <price>21000</price> </brand> <brand> <name>sony</name> <price>25000</price> </brand> </mobiles>Đây là mã để đọc tệp xml. Tôi đã đề cập đến hai cách để đọc tệp xml. Cái đầu tiên tải tệp xml, chuyển đổi dữ liệu thành chuỗi và in nó
PHP Tạo tệp XML - Phương thức SimpleXML, DOM, Reader/WriterTạo tệp XML động bằng cách sử dụng ba phương pháp khác nhau của PHP,
- DOM
- Trình đọc/ghi XML
- SimpleXML
Bảng sau giải thích phương pháp nào tốt để tuân theo tiêu chuẩn và phương pháp nào không
OperationDOMXML Reader/WriterSimpleXMLReadHỗ trợ kémWrite
Hỗ trợ kémThao tác
Hỗ trợ kém
Poorly supportFull ControlNamespace Support
Poorly supportXPATH SupportValidate
Tạo tệp XML trong Phương thức PHP SimpleXML, DOM, Reader/Writer
Phương thức XML DOM (Mô hình đối tượng tài liệu)
Ưu điểm của DOM
Rất nhanh đối với tài liệu nhỏ, tải toàn bộ tài liệu vào bộ nhớ
Giao diện PHP DOM đơn giản
Truy cập bất cứ thứ gì trong cây, đi qua nhiều cách
Thư viện phân tích cú pháp XML, sử dụng libXML2 cho DOM
Nhược điểm của DOM
Tải toàn bộ tài liệu vào bộ nhớ
Chỉ truy cập dữ liệu sau khi tài liệu được phân tích cú pháp đầy đủ
Hỗ trợ phiên bản PHP 5+ trở lên
Tạo XML bằng DOM
<?php $dom = new DOMDocument('1.0','UTF-8'); $dom->formatOutput = true; $root = $dom->createElement('student'); $dom->appendChild($root); $result = $dom->createElement('result'); $root->appendChild($result); $result->setAttribute('id', 1); $result->appendChild( $dom->createElement('name', 'Opal Kole') ); $result->appendChild( $dom->createElement('sgpa', '8.1') ); $result->appendChild( $dom->createElement('cgpa', '8.4') ); echo '<xmp>'. $dom->saveXML() .'</xmp>'; $dom->save('result.xml') or die('XML Create Error'); ?>
Đầu ra kết quả
<?xml version="1.0" encoding="UTF-8"?> <student> <result id="1"> <name>Opal Kole</name> <sgpa>8.1</sgpa> <cgpa>8.4</cgpa> </result> </student>
Thao tác XML bằng DOM
Thao tác với tệp XML đã tạo ở trên
<?php $dom = new DOMDocument(); $dom->formatOutput = true; $dom->load('result.xml', LIBXML_NOBLANKS); $root = $dom->documentElement; $newresult = $root->appendChild( $dom->createElement('result') ); $newresult->setAttribute('id', 2); $newresult->appendChild( $dom->createElement('name','Max Miller') ); $newresult->appendChild( $dom->createElement('sgpa','8.7') ); $newresult->appendChild( $dom->createElement('cgpa','8.2') ); echo ''. $dom->saveXML() .''; $dom->save('new_result.xml') or die('XML Manipulate Error'); ?>
Đầu ra kết quả
kết quả. xml
<?xml version="1.0" encoding="UTF-8"?> <student> <result id="1"> <name>Opal Kole</name> <sgpa>8.1</sgpa> <cgpa>8.4</cgpa> </result> <result id="2"> <name>Max Miller</name> <sgpa>8.7</sgpa> <cgpa>8.2</cgpa> </result> </student>
Đọc XML bằng DOM
<?php
$dom = new DOMDocument();
$dom->load('new_result.xml');
$root = $dom->documentElement;
echo "This document is store " . $root->tagName . " Result Information" . "
";
$results = $root->getElementsByTagName( 'result' );
foreach( $results as $result){
echo 'Result Id: ' . $result->getAttribute('id');
foreach( $result->getElementsByTagName('name') as $name ){
echo ' Student ' . $name->nodeValue;
}
foreach( $result->getElementsByTagName('sgpa') as $sgpa ){
echo ' SGPA ' . $sgpa->nodeValue;
}
foreach( $result->getElementsByTagName('cgpa') as $cgpa ){
echo ' CGPA ' . $cgpa->nodeValue . "
";
}
}
?>
Đầu ra kết quả
new_result. xml
This document is store student Result Information Result Id: 1 Student Opal Kole SGPA 8.1 CGPA 8.4 Result Id: 2 Student Max Miller SGPA 8.7 CGPA 8.2
Phương thức đọc/ghi XML
Trình đọc/ghi XML Ưu điểm
Sử dụng ít bộ nhớ để đọc và viết
Cho phép truy cập ngay lập tức
Hỗ trợ PHP4+ và PHP5+
Trình đọc/ghi XML Nhược điểm
Chỉ đọc dữ liệu tuần tự
Tất cả các phương pháp đều chậm để truy cập dữ liệu
Ví dụ trình đọc XML
________số 8
Đầu ra kết quả
Result Id: 1 Student Opal Kole SGPA 8.1 CGPA 8.4 Result Id: 2 Student Max Miller SGPA 8.7 CGPA 8.2
Ví dụ về trình soạn thảo XML
<?xml version="1.0" encoding="ISO-8859-1"?> <mobiles> <brand> <name>samsung</name> <price>21000</price> </brand> <brand> <name>sony</name> <price>25000</price> </brand> </mobiles> 0
Đầu ra kết quả
kết quả. xml
<?xml version="1.0" encoding="UTF-8"?> <student> <result id="1"> <name>Opal Kole</name> <sgpa>8.1</sgpa> <cgpa>8.4</cgpa> </result> </student>
Đọc/ghi SimpleXML
Viết XML đơn giản
<?xml version="1.0" encoding="UTF-8"?> <student> <result id="1"> <name>Opal Kole</name> <sgpa>8.1</sgpa> <cgpa>8.4</cgpa> </result> </student>0
Đầu ra kết quả
đơn giản_xml_create. xml
<?xml version="1.0" encoding="UTF-8"?> <student> <result id="1"> <name>Opal Kole</name> <sgpa>8.1</sgpa> <cgpa>8.4</cgpa> </result> </student>1
Đọc XML đơn giản
<?xml version="1.0" encoding="UTF-8"?> <student> <result id="1"> <name>Opal Kole</name> <sgpa>8.1</sgpa> <cgpa>8.4</cgpa> </result> </student>2
Đầu ra kết quả
<?xml version="1.0" encoding="UTF-8"?> <student> <result id="1"> <name>Opal Kole</name> <sgpa>8.1</sgpa> <cgpa>8.4</cgpa> </result> </student>3
- " Trước
- Tiếp theo "