Php + xml: how to add an entry to an online English dictionary

Source: Internet
Author: User
Php + xml: the method for adding entries to an online English dictionary. Php + xml: how to add an entry to an online English Dictionary. This document describes how to add an entry to an online English dictionary using php + xml. We will share with you how to add entries to the online English dictionary using php + xml.

This example describes how to add an entry to an online English dictionary using php + xml. Share it with you for your reference. The details are as follows:

Next, let's go to the php + xml method for online English dictionary query. here we need to add a function, submit English words and Chinese meanings, and add the information to the xml document.

Xml file (database): words. xml

The code is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?>


Boy
Boy


Girl
Girl


Teacher
Instructor


Beauty
Beauty

Query and add files: words. php

The code is as follows:

Online English-Chinese Dictionary
Query English words

Add an English word

File processing: xmlprocess. php

The code is as follows:

<? Php
// Create an 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 an entry
If (! Empty ($ _ POST ['add']) {
$ En_word = $ _ POST ['en _ word'];
$ Ch_word = $ _ POST ['Ch _ word'];
// Obtain the root node
$ Words = $ xmldoc-> getElementsByTagName ("words")-> item (0 );
// Add elements and 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;
// Attach the child element 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 php + XML programming.

Examples in this article describes how to add an entry to an online English dictionary using php + xml. Share it with you for your reference...

Related Article

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.