This paper illustrates the method of Php+xml to realize online English dictionary query. Share to everyone for your reference. Specifically as follows:
The XML here is equivalent to a database. Implementation: Query An English word, output its Chinese meaning.
XML file (database): Words.xml is as follows:
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>
Query file: word.php
Copy Code code as follows:
<form action= "xmlprocess.php" method= "POST" >
Please input English words: <input type= "text" name= "Enword"/>
<input type= "Submit" value= "Query" Name= "sub" >
</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;
?>
I hope this article will help you with your PHP operating XML program.