The crud operation of XML in PHP operation XML programming

Source: Internet
Author: User
Tags php file tagname

HTML code page

The code is as follows Copy Code

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8" >
<body>
<form action= "wordprocess.php" method= "POST" >
<input type= "text" name= "Enword" >
<input type= "hidden" name= "type" value= "Query" >
<input type= "Submit" value= "Query" >
</form>
<span> Add Words </span>
<form action= "wordprocess.php" method= "POST" >
English: <input type= "text" name= "Enword" ><br>
Chinese: <input type= "text" name= "Zhword" ><br>
<!--<input type= "hidden" name= "type" value= "Insert" >
<input type= "hidden" name= "type" value= "Update" >-->
<input type= "Submit" Name= "type" value= "Add" >
<input type= "Submit" Name= "type" value= "Modify" >
</form>
<form action= "wordprocess.php" method= "POST" >
<input type= "text" name= "word" >
<input type= "hidden" name= "type" value= "Delete" >
<input type= "Submit" value= "Delete" >
</form>
</body>

wordpress.php file

The code is as follows Copy Code


<?php

Receive type look at what the user does (query, add ...)
$type =$_request[' type '];
Echo $type;
Exit ();
Creating an XML Document Object
$doc =new DOMDocument ();
$doc->load ("Words.xml");

To judge
if ($type = = "Query") {
Gets the value entered by the user
$enword =$_request[' Enword '];

Determine whether to enter the query
$isEnter =false;
Get all Word nodes
$words = $doc->getelementsbytagname ("word");
Traversing word nodes
For ($i =0 $i < $words->length; $i + +) {
$word _node= $words->item ($i);
Get a different language
$en _word=getnodeval ($word _node, "en");
$zh _word=getnodeval ($word _node, "en");
Inquire
if ($enword = = $en _word) {
$isEnter =true;
echo $enword. " ". Getnodeval ($word _node," en ");
echo "<br/><a href= ' wordview.php ' > Return to continue query </a>";
}else if ($enword = = $zh _word) {
$isEnter =true;
echo $enword. " ". Getnodeval ($word _node," en ");
echo "<br/><a href= ' wordview.php ' > Return to continue query </a>";
}
}

if (! $isEnter) {
echo "Cannot query";
echo "<br/><a href= ' wordview.php ' > Return to continue query </a>";
}
}else if ($type = = "Add") {
Receive
$enword =$_request[' Enword '];
$zhword =$_request[' Zhword '];
if (!empty ($enword) &&!empty ($zhword)) {
Get root node
$root = $doc->getelementsbytagname ("words")->item (0);

$word = $doc->createelement ("word");
$en = $doc->createelement ("en", $enword);
$zh = $doc->createelement ("en", $zhword);

to mount

$root->appendchild ($word);
$word->appendchild ($en);
$word->appendchild ($ZH);

Save XML file
$doc->save ("Words.xml");
echo "Add success <br/><a href= ' wordview.php ' > Return to continue Operation </a>";
}else{

echo "Please enter a word";
echo "<br/><a href= ' wordview.php ' > Return to continue Operation </a>";
Exit ();
}
}else if ($type = = "Delete") {

$word =$_request[' word '];
Get all Word nodes
$words = $doc->getelementsbytagname ("word");
$isEnter =false;
Traversing word nodes
For ($i =0 $i < $words->length; $i + +) {
$word _node= $words->item ($i);
Get a different language
$en _word=getnodeval ($word _node, "en");
$zh _word=getnodeval ($word _node, "en");
Inquire
if ($word = = $en _word | | $word = = $zh _word) {
$isEnter =true;
Find parent Node
$word _node->parentnode->removechild ($word _node);
$doc->save ("Words.xml");
echo "Delete successful <br/><a href= ' wordview.php ' > Return to continue Operation </a>";
}
}

if (! $isEnter) {
Echo "Operation failed";
echo "<br/><a href= ' wordview.php ' > Return to continue Operation </a>";
}
}else if ($type = "Modify") {
Receive
$enword =$_request[' Enword '];
$zhword =$_request[' Zhword '];
if (!empty ($enword) &&!empty ($zhword)) {
Get all Word nodes
$words = $doc->getelementsbytagname ("word");
Traversing word nodes
$isEnter =false;
For ($i =0 $i < $words->length; $i + +) {
$word _node= $words->item ($i);
Get a different language
$en _word=getnodeval ($word _node, "en");
$zh _word=getnodeval ($word _node, "en");
Inquire
if ($enword = = $en _word && $zhword!= $zh _word) {
Modify Chinese
$isEnter =true;
Get ZH node
$zh = $word _node->getelementsbytagname ("en")->item (0);
$zh->nodevalue= $zhword;
$doc->save ("Words.xml");
echo "modified successfully";
echo "<br/><a href= ' wordview.php ' > Return to continue Operation </a>";
}else if ($enword!= $en _word && $zhword = = $zh _word) {
Modified because
$isEnter =true;
$en = $word _node->getelementsbytagname ("en")->item (0);
$en->nodevalue= $enword;
$doc->save ("Words.xml");
echo "modified successfully";
echo "<br/><a href= ' wordview.php ' > Return to continue Operation </a>";
}
}

if (! $isEnter) {
echo "did not make any changes";
echo "<br/><a href= ' wordview.php ' > Return to continue Operation </a>";
}

}else{
echo "Please enter the words you need to modify";
echo "<br/><a href= ' wordview.php ' > Return to continue Operation </a>";
Exit ();
}
}

Get the text value of a node
Function Getnodeval (& $MyNode, $tagName) {
Return $MyNode->getelementsbytagname ($tagName)->item (0)->nodevalue;
}
?>

Words.xml

  code is as follows copy code

<?xml Version= "1.0" encoding= "Utf-8"
<words><word><en>boy</en><zh> boy </zh> </word><word><en>girl</en><zh> Girls </zh></word><word><en> fire</en><zh> Fire </zh></word><word><en>word</en><zh> Thesaurus </zh> </word></words>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.