 |
อยากได้ตัวอย่างการสร้าง XML และ การนำ XML กลับมาเป็น DOM ใน php5 DOM ext ครับ |
|
 |
|
|
 |
 |
|
ขอบคุณครับคุณ ++Extension
แต่ผมต้องการสร้าง node โดยใช้ method ใน DOMDocument อ่ะครับ คือไม่ใช่เปิดจากไฟล์ คือการสร้าง และส่งจะเป็น แบบ generate คือไม่มีไฟล์ xml จริงๆเลย แต่ส่งไปทาง webservice ไม่อยาก เขียนแท็ค <> 
คือแค่เพิ่ม node และ child node ลงไป ก็ generate เป็น xml ให้ผมส่งไปให้เครื่อง webservice client น่ะครับ
คับเพิ่งเข้าไปดูในลิงค์ที่ให้มาก็เริ่มเห็นภาพเพิ่มขึ้นแล้วครับ ยังเหลือวิธีอ่านนะครับ คือแกะข้อมูลออกมา(เพื่อให้ client เอาไปแสดงผล แล้ว ส่งกลับมาบันทึกที่แก้ไข)
ปล1.อยากให้มีส่วนการใส่ attribute ด้วยคับ
ปล2.อย่าลืมมาโพสอีกนะครับ 
ปล3.รอให้ท่านอื่นมา โพสด้วยนะครับ 
|
 |
 |
 |
 |
Date :
2009-09-18 12:54:41 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างการสร้าง และเพิ่มข้อมูลเอกสาร XML บน Extension DOMXML API ของ PHP5
Code (PHP)
<?php
function writeXML($xml)
{
$fp = fopen('test.xml', 'w+');
fwrite($fp,$xml);
fclose($fp);
}
//ฟังก์ชั่นการ import node หรือข้อมูลใหม่ ภายใต้ root node (root element)
function importChildNode($node,$name,$count)
{
$child = $node->getElementsbytagName($name)->item($count);
return $child;
}
if(isset($submit))
{
$album = $_POST['album'];
$track = $_POST['track'];
$desc = $_POST['desc'];
$length = $_POST['length'];
$bitrate = $_POST['bitrate'];
$channel = $_POST['channel'];
$xmls = new DOMDocument('1.0','utf-8');
if(@$load = $xmls->load("../test.xml")) //เช็คการเปิดไฟล์ว่าไฟล์มีอยู่จริง
{
header( "content-type: application/xml; charset=utf-8" );
$dom = new DOMDocument('1.0','utf-8');
$nodeList = $xmls->getElementsbytagName('root')->item(0);
$oldnode = $dom->importNode($nodeList, true); //import ข้อมูลจากเอกสารเดิมมาไว้ในเอกสารใหม่
$dom->appendChild($oldnode);
$xml_album = $dom->createElement( "Album" );
$xml_name = $dom->createElement( "Name",$album );
$xml_track = $dom->createElement( "Track", $track );
$xml_note = $dom->createElement( "Description", $desc );
// Set the attributes.
$xml_track->setAttribute( "Length", $length );
$xml_track->setAttribute( "Bitrate", $bitrate );
$xml_track->setAttribute( "Channels",$channel );
// Append the whole bunch.
$xml_album->appendChild( $xml_name );
$xml_album->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );
if($nodeList->hasChildNodes()) //เช็คว่า root มี child node
{
$name = 'Album';
$count = 0;
while($node = importChildNode($nodeList,$name,$count))
{
$node = $dom->importNode($node, true);
$dom->documentElement->appendChild($node);
$count++;
}
}
// Parse the XML.
$text = $dom->saveXML();
print $text;
writeXML($text);
}
else //หากไม่มีไฟล์อยู่จริง จะทำการสร้างเอกสารใหม่
{
// Set the content type to be XML, so that the browser will recognise it as XML.
header( "content-type: application/xml; charset=utf-8" );
// "Create" the document.
$xml = new DOMDocument( "1.0", "utf-8" );
//Create root elemets
$xml_root = $xml->createElement( "root" );
// Create some elements.
$xml_album = $xml->createElement( "Album" );
// Create another element, just to show you can add any (realistic to computer) number of sublevels.
$xml_name = $xml->createElement( "Name",$album );
$xml_track = $xml->createElement( "Track", $track );
$xml_note = $xml->createElement( "Description", $desc );
// Set the attributes.
$xml_track->setAttribute( "Length", $length );
$xml_track->setAttribute( "Bitrate", $bitrate );
$xml_track->setAttribute( "Channels",$channel );
// Append the whole bunch.
$xml_album->appendChild( $xml_name );
$xml_album->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );
$xml_root->appendChild( $xml_album );
// Append the DOMXML.
$xml->appendChild( $xml_root );
$text = $xml->saveXML();
print $text;
writeXML($text);
}
}
else
{
?>
<form method="post" name="formxml" action="<?=$SERVER_['PHP_SELF'] ?>">
<table border="1" cellspacing="0">
<tr><td colspan="2"><center> Set Track Element</center></td></tr>
<tr><td>Album Name : </td><td><input type="text" name="album"></td></tr>
<tr><td>Track Name : </td><td><input type="text" name="track"></td></tr>
<tr><td>Track Description : </td><td><input type="text" name="desc"></td></tr>
<tr><td colspan="2"><center> Set Attribute Track</center></td></tr>
<tr><td>Track Length :</td><td><input type="text" name="length"></td></tr>
<tr><td>Bitrate :</td><td><input type="text" name="bitrate"></td></tr>
<tr><td>Channels :</td><td><input type="text" name="channel"></td></tr>
<tr><td colspan="2"><center><input type="submit" name="submit" value=" Add "></center></td></tr>
</table>
</form>
<?
}
?>
|
 |
 |
 |
 |
Date :
2009-09-19 00:46:59 |
By :
extenser |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ต้องขออภัยท่าน เอี่ยว(Rest) ที่มาผิดเวลา พอดีช่วงนี้ผมใกล้สอบ + กับต้องไปติวหนังสือ + ไปติวให้รุ่นน้องด้วย ก็เลยมาโพสซะดึก
เดี๋ยวพรุ่งนี้ผมจะทำตัวอย่างการ แสดงข้อมูลภายในเอกสาร XML และ การแก้ไขข้อมูล เอกสาร XML ต่อละกันครับ
|
 |
 |
 |
 |
Date :
2009-09-19 00:50:23 |
By :
extenser |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แก้ไขจากที่ได้โพสไป กระทู้ที่3 ครับ
<?php
function writeXML($xml)
{
$fp = fopen('test.xml', 'w+');
fwrite($fp,$xml);
fclose($fp);
}
if(isset($submit))
{
$album = $_POST['album'];
$track = $_POST['track'];
$desc = $_POST['desc'];
$length = $_POST['length'];
$bitrate = $_POST['bitrate'];
$channel = $_POST['channel'];
$xmls = new DOMDocument('1.0','utf-8');
if(@$load = $xmls->load("../Tle/test.xml"))
{
header( "content-type: application/xml; charset=utf-8" );
$dom = new DOMDocument('1.0','utf-8');
$nodeList = $xmls->getElementsbytagName('root')->item(0);
$oldnode = $dom->importNode($nodeList, true);
$dom->appendChild($oldnode);
$xml_album = $dom->createElement( "Album" );
$xml_name = $dom->createElement( "Name",$album );
$xml_track = $dom->createElement( "Track", $track );
$xml_note = $dom->createElement( "Description", $desc );
// Set the attributes.
$xml_track->setAttribute( "Length", $length );
$xml_track->setAttribute( "Bitrate", $bitrate );
$xml_track->setAttribute( "Channels",$channel );
// Append the whole bunch.
$xml_album->appendChild( $xml_name );
$xml_album->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );
$dom->documentElement->appendChild($xml_album);
// Parse the XML.
$text = $dom->saveXML();
print $text;
writeXML($text);
}
else
{
// Set the content type to be XML, so that the browser will recognise it as XML.
header( "content-type: application/xml; charset=utf-8" );
// "Create" the document.
$xml = new DOMDocument( "1.0", "utf-8" );
//Create root elemets
$xml_root = $xml->createElement( "root" );
// Create some elements.
$xml_album = $xml->createElement( "Album" );
// Create another element, just to show you can add any (realistic to computer) number of sublevels.
$xml_name = $xml->createElement( "Name",$album );
$xml_track = $xml->createElement( "Track", $track );
$xml_note = $xml->createElement( "Description", $desc );
// Set the attributes.
$xml_track->setAttribute( "Length", $length );
$xml_track->setAttribute( "Bitrate", $bitrate );
$xml_track->setAttribute( "Channels",$channel );
// Append the whole bunch.
$xml_album->appendChild( $xml_name );
$xml_album->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );
$xml_root->appendChild( $xml_album );
// Append the DOMXML.
$xml->appendChild( $xml_root );
$text = $xml->saveXML();
print $text;
writeXML($text);
}
}
else
{
?>
<form method="post" name="formxml" action="<?=$SERVER_['PHP_SELF'] ?>">
<table border="1" cellspacing="0">
<tr><td colspan="2"><center> Set Track Element</center></td></tr>
<tr><td>Album Name : </td><td><input type="text" name="album"></td></tr>
<tr><td>Track Name : </td><td><input type="text" name="track"></td></tr>
<tr><td>Track Description : </td><td><input type="text" name="desc"></td></tr>
<tr><td colspan="2"><center> Set Attribute Track</center></td></tr>
<tr><td>Track Length :</td><td><input type="text" name="length"></td></tr>
<tr><td>Bitrate :</td><td><input type="text" name="bitrate"></td></tr>
<tr><td>Channels :</td><td><input type="text" name="channel"></td></tr>
<tr><td colspan="2"><center><input type="submit" name="submit" value=" Add "></center></td></tr>
</table>
</form>
<?
}
?>
จากที่โพสไปกระทู้ที่ 3 ผมลองเพิ่มข้อมูลลงไปปรากฎว่ามันนำข้อมูลจากการ Import ไฟล์เดิมมาเพิ่มข้อมูลแทน
ส่วนอันนี้แก้ให้นำข้อมูลจาก Form มาเพิ่มลง XML ได้ละครับ
|
 |
 |
 |
 |
Date :
2009-09-19 01:15:09 |
By :
extenser |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไฟล์แสดงข้อมูลโดยอ่านข้อมูลจากเอกสาร XML
<?php
$xml = new DOMDocument('1.0','utf-8');
$xml->load( '../test.xml' );
$root = $xml->getElementsByTagName( "root" );
$count =1;
echo "Show Detail<p>";
echo "<table border='1' cellspacing='0'>";
echo "<tr><td>Name</td><td>Description</td><td>Track Length</td><td>Bitrate</td><td>Channels</td><td>Edit</td></tr><tr>";
foreach( $root as $child )
{
$album = $child->getElementsByTagName( "Album" );
foreach($album as $nodealbum)
{
//Stand at <Name>...</Name>
$name = $nodealbum->getElementsByTagName( "Name" );
foreach($name as $nodename)
{
echo "<td>".$nodename->nodeValue."</td>";
}
//Stand at <Description>...</Description>
$desc = $nodealbum->getElementsByTagName( "Description" );
foreach($desc as $nodedesc)
{
echo "<td>".$nodedesc->nodeValue."</td>";
}
//Stand at <Track>...</Track>
$track = $nodealbum->getElementsByTagName( "Track" );
foreach($track as $nodetrack)
{
echo "<td>".$nodetrack->getAttribute('Length')."</td>";
echo "<td>".$nodetrack->getAttribute('Bitrate')."</td>";
echo "<td>".$nodetrack->getAttribute('Channels')."</td>";
}
echo "<td><a href='edit.php?id=$count'> แก้ไข </a></td></tr>";
$count++;
}
}
echo "</table>"
?>
ส่วนไฟล์ แก้ไข และ ลบข้อมูลจะค่อยๆ ตามมานะครับ
|
 |
 |
 |
 |
Date :
2009-09-19 02:24:57 |
By :
extenser |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตัวอย่างไฟล์แก้ไขข้อมูล โดยอ่านจากไฟล์ XML โดย Extension DOM/XML API บน PHP5
<?php
function writeXML($xml)
{
$fp = fopen('test.xml', 'w+');
fwrite($fp,$xml);
fclose($fp);
}
function PositionChildNode($node,$name,$count)
{
$child = $node->getElementsbytagName($name)->item($count);
return $child;
}
if(isset($edit))
{
$id = $_GET['id']-1;
$album = $_POST['album'];
$track = $_POST['track'];
$desc = $_POST['desc'];
$length = $_POST['length'];
$bitrate = $_POST['bitrate'];
$channel = $_POST['channel'];
$dom = new DOMDocument( "1.0", "utf-8" );
$dom->load("../XMLPHP5/test.xml");
$root = $dom->documentElement; //root element
$xml_album = $dom->createElement( "Album" );
$xml_name = $dom->createElement( "Name",$album );
$xml_track = $dom->createElement( "Track", $track );
$xml_note = $dom->createElement( "Description", $desc );
//Set the attributes.
$xml_track->setAttribute( "Length", $length );
$xml_track->setAttribute( "Bitrate", $bitrate );
$xml_track->setAttribute( "Channels",$channel );
// Append the whole bunch.
$xml_album->appendChild( $xml_name );
$xml_album->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );
//Old Node and change content
$oldnode = $root->getElementsbytagName('Album')->item($id);
//Import NewNode
$newnode = $dom->importNode($xml_album, true);
$oldnode->parentNode->replaceChild($newnode, $oldnode);
$text = $dom->saveXML();
writeXML($text);
echo "<meta http-equiv=\"refresh\" content=\"0;URL=show.php\" />";
}
else
{
if(isset($_GET['id']))
$id = $_GET['id']-1;
else
$id = 0;
$xml = new DOMDocument('1.0','utf-8');
$xml->load( '../XMLPHP5/test.xml' );
$root = $xml->documentElement;
$count =0;
echo "Edit<p>";
$current = $root->getElementsByTagName( "Album" );
foreach($current as $nodealbum)
{
if($count<=$id)
{
//Stand at <Name>...</Name>
$name = $nodealbum->getElementsByTagName( "Name" );
foreach($name as $nodename)
{
$n = $nodename->nodeValue;
}
//Stand at <Description>...</Description>
$desc = $nodealbum->getElementsByTagName( "Description" );
foreach($desc as $nodedesc)
{
$d = $nodedesc->nodeValue;
}
//Stand at <Track>...</Track>
$track = $nodealbum->getElementsByTagName( "Track" );
foreach($track as $nodetrack)
{
$t = $nodetrack->nodeValue;
$L = $nodetrack->getAttribute('Length');
$B = $nodetrack->getAttribute('Bitrate');
$C = $nodetrack->getAttribute('Channels');
}
}
$count++;
}
?>
<form method="post" name="formxml" action="<?=$SERVER_['PHP_SELF'] ?>">
<table border="1" cellspacing="0">
<tr><td colspan="2"><center> Edit Track Element</center></td></tr>
<tr><td>Album Name : </td><td><input type="text" name="album" value="<?=$n?>"></td></tr>
<tr><td>Track Name : </td><td><input type="text" name="track" value="<?=$d?>"></td></tr>
<tr><td>Track Description : </td><td><input type="text" name="desc" value="<?=$t?>"></td></tr>
<tr><td colspan="2"><center> Edit Attribute Track</center></td></tr>
<tr><td>Track Length :</td><td><input type="text" name="length" value="<?=$L?>"></td></tr>
<tr><td>Bitrate :</td><td><input type="text" name="bitrate" value="<?=$B?>"></td></tr>
<tr><td>Channels :</td><td><input type="text" name="channel" value="<?=$C?>"></td></tr>
<tr><td colspan="2"><center><input type="submit" name="edit" value=" Edit "></center></td></tr>
</table>
</form>
<?
}
?>
ผมติดไฟล์การลบไว้ก่อนนะครับ บ่ายๆจะมาโพสต่อ
|
 |
 |
 |
 |
Date :
2009-09-19 07:06:28 |
By :
extenser |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ครับ ขอบคุณอีกครั้งครับ ++
ได้วิธีแกะ xml ละ คือใช้ foreach นี่เอง 
|
 |
 |
 |
 |
Date :
2009-09-19 10:05:20 |
By :
pjgunner |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ที่ต้องใช้ foreach เพราะ XML เป็นเอกสารที่มีโครงสร้างประเภท Tree นั่นเองครับ
|
 |
 |
 |
 |
Date :
2009-09-19 13:12:49 |
By :
extenser |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
คุณ Extension ครับ กลับมาโพส การลบ ต่อได้ไหมครับ
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2010-03-08 12:06:59 |
By :
่justcute |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไฟล์ show.php
<?php
echo "<b>Show Detail</b></br>";
echo "<table border='1' cellspacing='0'>";
echo "<tr><td>Name</td><td>Description</td><td>Track Length</td><td>Bitrate</td><td>Channels</td><td>Edit</td><td>Delete</td></tr><tr>";
$xml = new DOMDocument('1.0','utf-8');
$xml->load( '../XML/test.xml' );
$root = $xml->getElementsByTagName( "root" );
$count =1;
foreach( $root as $child )
{
$album = $child->getElementsByTagName( "Album" );
foreach($album as $nodealbum)
{
//Stand at <Name>...</Name>
$name = $nodealbum->getElementsByTagName( "Name" );
foreach($name as $nodename)
{
echo "<td>".$nodename->nodeValue."</td>";
}
//Stand at <Description>...</Description>
$desc = $nodealbum->getElementsByTagName( "Description" );
foreach($desc as $nodedesc)
{
echo "<td>".$nodedesc->nodeValue."</td>";
}
//Stand at <Track>...</Track>
$track = $nodealbum->getElementsByTagName( "Track" );
foreach($track as $nodetrack)
{
echo "<td>".$nodetrack->getAttribute('Length')."</td>";
echo "<td>".$nodetrack->getAttribute('Bitrate')."</td>";
echo "<td>".$nodetrack->getAttribute('Channels')."</td>";
}
echo "<td><a href='".$_SERVER['PHP_SELF']."?id=$count'> แก้ไข </a></td>";
echo "<td><a href='".$_SERVER['PHP_SELF']."?del=$count' onClick='return confirm(\"Delete Item.\")'> ลบ </a></td></tr>";
$count++;
}
}
echo "</table>";
echo "<br><hr><p>";
if(isset($_GET['id']))
include("edit.php");
elseif(isset($_GET['del']))
include("delete.php");
else
include("add.php");
?>
ไฟล์ add.php
<?php
function writeXML($xml)
{
$fp = fopen('test.xml', 'w+');
fwrite($fp,$xml);
fclose($fp);
}
if(isset($submit))
{
$album = $_POST['album'];
$track = $_POST['track'];
$desc = $_POST['desc'];
$length = $_POST['length'];
$bitrate = $_POST['bitrate'];
$channel = $_POST['channel'];
$xmls = new DOMDocument('1.0','utf-8');
if(@$load = $xmls->load("../XML/test.xml"))
{
$dom = new DOMDocument('1.0','utf-8');
$nodeList = $xmls->getElementsbytagName('root')->item(0);
$oldnode = $dom->importNode($nodeList, true);
$dom->appendChild($oldnode);
$xml_album = $dom->createElement( "Album" );
$xml_name = $dom->createElement( "Name",$album );
$xml_track = $dom->createElement( "Track", $track );
$xml_note = $dom->createElement( "Description", $desc );
// Set the attributes.
$xml_track->setAttribute( "Length", $length );
$xml_track->setAttribute( "Bitrate", $bitrate );
$xml_track->setAttribute( "Channels",$channel );
// Append the whole bunch.
$xml_album->appendChild( $xml_name );
$xml_album->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );
$dom->documentElement->appendChild($xml_album);
// Parse the XML.
$text = $dom->saveXML();
print $text;
writeXML($text);
echo "<meta http-equiv=\"refresh\" content=\"0;URL=show.php\" />";
}
else
{
// Set the content type to be XML, so that the browser will recognise it as XML.
header( "content-type: application/xml; charset=utf-8" );
// "Create" the document.
$xml = new DOMDocument( "1.0", "utf-8" );
//Create root elemets
$xml_root = $xml->createElement( "root" );
// Create some elements.
$xml_album = $xml->createElement( "Album" );
// Create another element, just to show you can add any (realistic to computer) number of sublevels.
$xml_name = $xml->createElement( "Name",$album );
$xml_track = $xml->createElement( "Track", $track );
$xml_note = $xml->createElement( "Description", $desc );
// Set the attributes.
$xml_track->setAttribute( "Length", $length );
$xml_track->setAttribute( "Bitrate", $bitrate );
$xml_track->setAttribute( "Channels",$channel );
// Append the whole bunch.
$xml_album->appendChild( $xml_name );
$xml_album->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );
$xml_root->appendChild( $xml_album );
// Append the DOMXML.
$xml->appendChild( $xml_root );
$text = $xml->saveXML();
print $text;
writeXML($text);
echo "<meta http-equiv=\"refresh\" content=\"0;URL=show.php\" />";
}
}
else
{
?>
<b>Add</b><p/>
<form method="post" name="formxml" action="<?=$SERVER_['PHP_SELF'] ?>">
<table border="1" cellspacing="0">
<tr><td colspan="2"><center> Set Track Element</center></td></tr>
<tr><td>Album Name : </td><td><input type="text" name="album"></td></tr>
<tr><td>Track Name : </td><td><input type="text" name="track"></td></tr>
<tr><td>Track Description : </td><td><input type="text" name="desc"></td></tr>
<tr><td colspan="2"><center> Set Attribute Track</center></td></tr>
<tr><td>Track Length :</td><td><input type="text" name="length"></td></tr>
<tr><td>Bitrate :</td><td><input type="text" name="bitrate"></td></tr>
<tr><td>Channels :</td><td><input type="text" name="channel"></td></tr>
<tr><td colspan="2"><center><input type="submit" name="submit" value=" Add "></center></td></tr>
</table>
</form>
<?
}
?>
ไฟล์ edit.php
<?php
function writeXML($xml)
{
$fp = fopen('test.xml', 'w+');
fwrite($fp,$xml);
fclose($fp);
}
function PositionChildNode($node,$name,$count)
{
$child = $node->getElementsbytagName($name)->item($count);
return $child;
}
if(isset($edit))
{
$id = $_GET['id']-1;
$album = $_POST['album'];
$track = $_POST['track'];
$desc = $_POST['desc'];
$length = $_POST['length'];
$bitrate = $_POST['bitrate'];
$channel = $_POST['channel'];
$dom = new DOMDocument( "1.0", "utf-8" );
$dom->load("../XML/test.xml");
$root = $dom->documentElement; //root element
$xml_album = $dom->createElement( "Album" );
$xml_name = $dom->createElement( "Name",$album );
$xml_track = $dom->createElement( "Track", $track );
$xml_note = $dom->createElement( "Description", $desc );
//Set the attributes.
$xml_track->setAttribute( "Length", $length );
$xml_track->setAttribute( "Bitrate", $bitrate );
$xml_track->setAttribute( "Channels",$channel );
// Append the whole bunch.
$xml_album->appendChild( $xml_name );
$xml_album->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );
//Old Node and change content
$oldnode = $root->getElementsbytagName('Album')->item($id);
//Import NewNode
$newnode = $dom->importNode($xml_album, true);
$oldnode->parentNode->replaceChild($newnode, $oldnode);
$text = $dom->saveXML();
writeXML($text);
echo "<meta http-equiv=\"refresh\" content=\"0;URL=show.php\" />";
}
else
{
if(isset($_GET['id']))
$id = $_GET['id']-1;
else
$id = 0;
$xml = new DOMDocument('1.0','utf-8');
$xml->load( '../XML/test.xml' );
$root = $xml->documentElement;
$count =0;
echo "<b>Edit</b><br>";
$current = $root->getElementsByTagName( "Album" );
foreach($current as $nodealbum)
{
if($count<=$id)
{
//Stand at <Name>...</Name>
$name = $nodealbum->getElementsByTagName( "Name" );
foreach($name as $nodename)
{
$n = $nodename->nodeValue;
}
//Stand at <Description>...</Description>
$desc = $nodealbum->getElementsByTagName( "Description" );
foreach($desc as $nodedesc)
{
$d = $nodedesc->nodeValue;
}
//Stand at <Track>...</Track>
$track = $nodealbum->getElementsByTagName( "Track" );
foreach($track as $nodetrack)
{
$t = $nodetrack->nodeValue;
$L = $nodetrack->getAttribute('Length');
$B = $nodetrack->getAttribute('Bitrate');
$C = $nodetrack->getAttribute('Channels');
}
}
$count++;
}
?>
<form method="post" name="formxml" action="<?=$SERVER_['PHP_SELF'] ?>">
<table border="1" cellspacing="0">
<tr><td colspan="2"><center> Edit Track Element</center></td></tr>
<tr><td>Album Name : </td><td><input type="text" name="album" value="<?=$n?>"></td></tr>
<tr><td>Track Name : </td><td><input type="text" name="track" value="<?=$d?>"></td></tr>
<tr><td>Track Description : </td><td><input type="text" name="desc" value="<?=$t?>"></td></tr>
<tr><td colspan="2"><center> Edit Attribute Track</center></td></tr>
<tr><td>Track Length :</td><td><input type="text" name="length" value="<?=$L?>"></td></tr>
<tr><td>Bitrate :</td><td><input type="text" name="bitrate" value="<?=$B?>"></td></tr>
<tr><td>Channels :</td><td><input type="text" name="channel" value="<?=$C?>"></td></tr>
<tr><td colspan="2"><center><input type="submit" name="edit" value=" Edit "></center></td></tr>
</table>
</form>
<?
}
?>
ไฟล์ delete.php
<?
function writeXML($xml)
{
$fp = fopen('test.xml', 'w+');
fwrite($fp,$xml);
fclose($fp);
}
if(isset($_GET['del']))
{
$id = $_GET['del']-1;
$dom = new DOMDocument( "1.0", "utf-8" );
$dom->load("../XML/test.xml");
$root = $dom->documentElement;
$oldnode = $root->getElementsbytagName('Album')->item($id);
$root = $root->removeChild($oldnode);
$text = $dom->saveXML();
writeXML($text);
echo "<meta http-equiv=\"refresh\" content=\"0;URL=show.php\" />";
}
?>
ผมรวมไว้ให้ทั้งหมดแล้วนะครับ show,add,edit,delete
|
 |
 |
 |
 |
Date :
2010-03-10 23:55:05 |
By :
extenser |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณมากครับ คุณExtension
|
 |
 |
 |
 |
Date :
2010-03-11 22:24:21 |
By :
่justcute |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
|
 |
 |
 |
 |
Date :
2010-09-03 16:41:03 |
By :
supernova |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขออนุญาตท่าน Extension ช่วยทำอีกตัวได้หรือเปล่าครับ ทำ search ให้ผมหน่อยครับ ค้นหาแบบเลือกได้ว่าจะค้นหาอะไรอะครับ ขอบพระคุณอย่างสูงครับ
|
ประวัติการแก้ไข 2011-01-10 14:16:20
 |
 |
 |
 |
Date :
2011-01-10 14:09:57 |
By :
sukonkan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ช่วยทำตัว Come Generate XML File ที่มีการเข้ารหัสไฟล์ XML ในส่วนท้ายให้ด้วยน่ะครับ
โดยทำเป็นตัวอย่างเพิ่มเติมในการเข้ารหัส MD5 ของ XML ด้วยครับ ว่าต้องเขียน Code อย่างไรครับ
ในการทำ XML ไฟล์ครับ
ขอบคุณครับ
|
 |
 |
 |
 |
Date :
2013-03-24 18:37:15 |
By :
stanly |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|