|
|
|
PHP แจกโค้ดตัวอย่างการอ่าน/เขียน/ลบ node บน XML |
|
|
|
|
|
|
|
คือผมทำเป็นตัวต้นแบบไว้สำหรับทดสอบการทำงาน อ่าน/เขียน/ลบ ข้อมูลบน XML รวมทั้งการกำหนด attribute ด้วย เพื่อที่จะนำไปใช้งานจริงต่อไป
เห็นว่าน่าจะเป็นประโยชน์ไว้ใช้อ้างอิงได้สำหรับคนที่อาจจะยังไม่คุ้นเคยหรืออาจจะลืมๆไป
ส่วนของการหา element โดย ID ผมคิดว่าโค้ดของผมยังทำได้ไม่ดี (ดีคือเร็วและใช้ทรัพยากรน้อยสุด) หากท่านใดเห็นทางที่ดีกว่าก็รบกวนแนะนำด้วยครับ
หวังว่าจะเป็นประโยชน์บ้างละมั้ง
Code (PHP)
<?php
$text_xml_file = 'test-domxml.xml';
$act = (isset($_REQUEST['act']) ? trim($_REQUEST['act']) : '');
// first detect that xml file exists, if not just create new xml file.
detectFileAndCreate();
if ($act == 'write') {
writeXmlData();
} elseif ($act == 'delete') {
deleteXmlDataOrFile();
}
/**
* Delete XML data or file.
*
* @return boolean Return true on success deletion, false for otherwise.
*/
function deleteXmlDataOrFile()
{
global $text_xml_file;
$ids = (isset($_REQUEST['id']) ? $_REQUEST['id'] : []);
$delete = (isset($_REQUEST['delete']) ? trim($_REQUEST['delete']) : '');
if ($delete === 'all') {
unset($delete, $ids);
if (is_file($text_xml_file) && is_writable($text_xml_file)) {
unlink($text_xml_file);
detectFileAndCreate();
return true;
}
return false;
} elseif ($delete === 'selected') {
unset($delete);
if (!empty($ids)) {
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->validateOnParse = true;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($text_xml_file);
if (is_array($ids)) {
$delete_count = 0;
foreach ($ids as $id) {
$repo_list = $dom->getElementsByTagName('list');
foreach ($repo_list as $repository) {
$items = $repository->getElementsByTagName('item');
if ($items != null) {
foreach ($items as $item) {
if ($item->getAttribute('id') == $id) {
$item->parentNode->removeChild($item);
$delete_count++;
}
}// endforeach; $items
unset($item);
}
unset($items);
}// endforeach; $repo_list
}// endforeach; $ids
unset($id);
if ($delete_count > 0) {
$dom->save($text_xml_file, LIBXML_NOEMPTYTAG);
unset($delete_count, $dom, $ids);
return true;
}
unset($delete_count);
}
}// endif !empty($ids)
}
unset($delete, $dom, $ids);
return false;
}// deleteXmlDataOrFile
/**
* Detect XML file and create if not exists.
*/
function detectFileAndCreate()
{
global $text_xml_file;
if (!file_exists($text_xml_file)) {
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$root = $dom->createElement('root');
$root = $dom->appendChild($root);
// plugins xml list
$plugins = $dom->createElement('plugins');
$plugins = $root->appendChild($plugins);
$title = $dom->createElement('title');
$title = $plugins->appendChild($title);
$title_text = $dom->createTextNode('Plugins list');
$title_text = $title->appendChild($title_text);
$list = $dom->createElement('list');
$list = $plugins->appendChild($list);
// themes xml list
$themes = $dom->createElement('themes');
$themes = $root->appendChild($themes);
$title = $dom->createElement('title');
$title = $themes->appendChild($title);
$title_text = $dom->createTextNode('Themes list');
$title_text = $title->appendChild($title_text);
$list = $dom->createElement('list');
$list = $themes->appendChild($list);
$dom->save($text_xml_file, LIBXML_NOEMPTYTAG);
unset($dom, $list, $plugins, $root, $themes, $title, $title_text);
}
if (file_exists($text_xml_file) && !is_file($text_xml_file)) {
throw new \Exception('The file "' . $text_xml_file . '" is not a file, please delete it manually at ' . realpath($text_xml_file) . '.');
}
}// detectFileAndCreate
/**
* Display XML data.
*
* @param string $section The section value is plugins or themes.
*/
function displayXmlData($section)
{
global $text_xml_file;
if ($section != 'plugins' && $section != 'themes') {
$section = 'plugins';
}
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($text_xml_file);
$repo_list = $dom
->getElementsByTagName($section)->item(0)
->getElementsByTagName('list');
if (!empty($repo_list)) {
foreach ($repo_list as $repository) {
$items = $repository->getElementsByTagName('item');
if ($items != null) {
foreach ($items as $item) {
$name = $item->getElementsByTagName('name')->item(0);
$version = $item->getElementsByTagName('version')->item(0);
echo '<label>'."\n";
echo ' <input type="checkbox" name="id[]" value="' . $item->getAttribute('id') . '"> ';
echo 'ID ' . $item->getAttribute('id') . ': ' . $name->nodeValue . ' ' . $version->nodeValue . "\n";
echo '</label><br>'."\n";
unset($name, $version);
}// endforeach;
unset($item);
}
unset($items);
}// endforeach;
unset($repository);
}
unset($dom, $repo_list);
}// displayXmlData
/**
* Write XML data to exists XML file.
*/
function writeXmlData()
{
global $text_xml_file;
$section = (isset($_REQUEST['section']) ? trim($_REQUEST['section']) : 'plugins');
$id = (isset($_REQUEST['id']) ? trim($_REQUEST['id']) : '');
$name = (isset($_REQUEST['name']) ? trim($_REQUEST['name']) : '');
$version = (isset($_REQUEST['version']) ? trim($_REQUEST['version']) : '');
if ($section != null && $name != null && $version != null && $id != null && is_numeric($id)) {
$dom = new \DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load($text_xml_file);
$repo_list = $dom
->getElementsByTagName($section)->item(0)
->getElementsByTagName('list')->item(0);
$repository = $dom->createElement('item');
$repository_attribute = $dom->createAttribute('id');
$repository_attribute->value = $id;
$repository->appendChild($repository_attribute);
$name_element = $dom->createElement('name');
$name_element = $repository->appendChild($name_element);
$name_text = $dom->createTextNode($name);
$name_text = $name_element->appendChild($name_text);
$version_element = $dom->createElement('version');
$version_element = $repository->appendChild($version_element);
$version_text = $dom->createTextNode($version);
$version_text = $version_element->appendChild($version_text);
$repo_list->appendChild($repository);
$dom->save($text_xml_file, LIBXML_NOEMPTYTAG);
}
unset($dom, $name, $name_element, $name_text, $repo_list, $repository, $section, $version, $version_element, $version_text);
}// writeXmlData
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test DOM XML</title>
<style type="text/css">
h1, h2, h3, h4, h5, h6, p {
margin: 0 0 10px 0;
padding: 0;
}
hr {
border: none;
border-top: 1px solid #ccc;
}
pre {
background-color: #333;
color: #ddd;
margin: 0 0 10px;
overflow: hidden;
overflow-wrap: break-word;
overflow-y: auto;
padding: 10px;
white-space: pre-wrap;
word-break: break-all;
word-wrap: break-word;
}
.form-result-placeholder {
display: none;
margin: 0 0 20px;
}
.container::after {
clear: both;
content: '';
display: table;
}
.container {
margin: 0 auto;
width: 1000px;
}
.xml-read {
float: left;
margin: 0 10px 0 0;
width: 490px;
}
.xml-write-delete {
float: left;
margin: 0 0 0 10px;
width: 490px;
}
</style>
</head>
<body>
<div class="form-result-placeholder">
</div><!--.form-result-placeholder-->
<div class="container">
<h1>Test read/write XML using DOM</h1>
<div class="xml-read">
<h2>XML content</h2>
<pre><?php echo htmlspecialchars(file_get_contents($text_xml_file), ENT_QUOTES); ?></pre>
</div><!--.xml-read-->
<div class="xml-write-delete">
<h3>Write</h3>
<form method="post">
<input type="hidden" name="act" value="write">
Section:
<select name="section">
<option value="plugins">Plugins</option>
<option value="themes">Themes</option>
</select><br>
ID:
<input type="number" name="id" autocomplete="off"><br>
Name:
<input type="text" name="name" autocomplete="off"><br>
Version:
<input type="text" name="version" autocomplete="off"><br>
<button type="submit">Submit</button>
</form>
<hr>
<h3>Delete</h3>
<form method="post">
<input type="hidden" name="act" value="delete">
<h4>Plugins</h4>
<?php displayXmlData('plugins'); ?>
<h4>Themes</h4>
<?php displayXmlData('themes'); ?>
<button type="submit" name="delete" value="selected">Delete selected</button>
<button type="submit" name="delete" value="all">Delete all (delete XML file)</button>
</form>
</div><!--.xml-write-delete-->
</div><!--.container-->
</body>
</html>
Tag : PHP
|
ประวัติการแก้ไข 2016-12-05 18:49:54
|
|
|
|
|
Date :
2016-12-05 16:08:06 |
By :
mr.v |
View :
2648 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
จัดไปครับ
|
|
|
|
|
Date :
2016-12-06 08:57:37 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2016-12-08 14:23:23 |
By :
datdit |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|