This article is an example of the php+xml to implement the online English dictionary to add the entry method. Share to everyone for your reference. Specifically as follows:
Then on a "php+xml to implement online English Dictionary Query method", here to add a feature, submit English words and Chinese meaning, add this information to the XML document.
XML file (database): Words.xml
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<words>
<word>
<en>boy</en>
<ch> Boy </ch>
</word>
<word>
<en>girl</en>
<ch> Girls </ch>
</word>
<word>
<en>teacher</en>
<ch> Teacher </ch>
</word>
<word>
<en>beauty</en>
<ch> Beauty </ch>
</word>
</words>
Querying and adding files: words.php
Copy Code code as follows:
<H2 style= "Color:green" > Online English-Chinese dictionary <form action= "xmlprocess.php" method= "POST" >
Please input English words: <input type= "text" name= "Enword"/>
<input type= "Submit" value= "Query" name= "sub"/>
</form>
<form action= "xmlprocess.php" method= "POST" >
English words: <input type= "text" name= "En_word"/><br/>
<input type= "text" name= "Ch_word"/>
<input type= "Submit" value= "adding" name= "Add" >
</form>
Working with files: xmlprocess.php
Copy Code code as follows:
<?php
Creating XML objects
$xmldoc = new DOMDocument ();
$xmldoc->load ("Words.xml");
Inquire
if (!empty ($_post[' Sub ')) {
$en _word = $_post[' Enword '];
$word = $xmldoc->getelementsbytagname ("en");
For ($i =0 $i < $word->length; $i + +) {
if ($en _word== $word->item ($i)->nodevalue) {
$CN _word = $xmldoc->getelementsbytagname ("ch")->item ($i)->nodevalue;
Break
}else{
$CN _word = "The word you entered is not found";
}
}
echo $CN _word;
}
Add entry
if (!empty ($_post[' Add ')) {
$en _word = $_post[' En_word '];
$ch _word = $_post[' Ch_word '];
Get root node
$words = $xmldoc->getelementsbytagname ("words")->item (0);
Add elements, adding content
$new _word = $xmldoc->createelement ("word");
$new _word_en = $xmldoc->createelement ("en");
$new _word_en->nodevalue = $en _word;
$new _word_ch = $xmldoc->createelement ("ch");
$new _word_ch->nodevalue = $ch _word;
Elements, meaning that the child elements are connected to the parent element
$new _word->appendchild ($new _word_en);
$new _word->appendchild ($new _word_ch);
$words->appendchild ($new _word);
Save
$xmldoc->save ("Words.xml");
}
?>
I hope this article will help you with the Php+xml program design.