Home > Backend Development > PHP Tutorial > PHP+xml realizes the method of adding entries to the online English dictionary, xml English dictionary_PHP tutorial

PHP+xml realizes the method of adding entries to the online English dictionary, xml English dictionary_PHP tutorial

WBOY
Release: 2016-07-13 10:09:21
Original
1054 people have browsed it

php+xml implements the method of adding entries to the online English dictionary, xml English dictionary

The example in this article describes the method of adding entries to the online English dictionary using php+xml. Share it with everyone for your reference. The details are as follows:

Following the previous article "How to implement online English dictionary query with php+xml", here we will add a function to submit English words and Chinese meanings, and add this information to the xml document.

xml file (database): words.xml

Copy code The code is as follows:



boy
Boy


girl
Girl


teacher
Teacher


beauty
Beauty

Query and add files: words.php

Copy code The code is as follows:

Online English-Chinese Dictionary


Look up English words



Please enter an English word:


Add English words



English word:

Chinese meaning:

Processing file: xmlprocess.php

Copy code The code is as follows:
//Create xml object
$xmldoc = new DOMDocument();
$xmldoc->load("words.xml");
//Query
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 cannot be found";
}
}
echo $cn_word;
}
//Add entry
if(!empty($_POST['add'])){
$en_word = $_POST['en_word'];
$ch_word = $_POST['ch_word'];
//Get the root node
$words = $xmldoc->getElementsByTagName("words")->item(0);
//Add elements and add 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;
//Mounting between elements means connecting child elements to parent elements
$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 be helpful to everyone’s php+XML programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/946732.htmlTechArticlephp+xml implements the method of adding entries to the online English dictionary, xml English dictionary This article describes the php+xml example Implement the method of adding entries to the online English dictionary. Share it with everyone for your reference...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template