Use DomIt! Simple XML processing

Source: Internet
Author: User
Once I got a job to process XML files. This XML file is used for communication with the Flash MP3 player, including the music address and description. All I need to do is to allow the administrator to add, delete music items, and update XML files using a form. It sounds simple, right? But I was in trouble when doing the delete function "> <LINKhref =" http://www.ph

Once I got a job to process XML files. This XML file is used for communication with the Flash mp3 player, including the music address and description. All I need to do is to allow the administrator to add, delete music items, and update XML files using a form. It sounds simple, right? However, I encountered a problem in the deletion function. DomIt when I spent countless times scratching my head, searching, and reading the DOM XML content in the official PHP documentation! Appeared in time (in fact, I found it in time ). DomIt! It is very powerful, easy to use (at least for simple processing), compatible with PHP4, and helps you learn dom xml quickly when you are willing to read its source code.

 

The XML file is in the following format:

<? Xmlversion = "1.0"?>
>
>
>Music file location >
>Music file title >
>Music recording information >
>
>

Of course, this XML file was provided in advance when I took the job. But I want to use DomIt! Create such a file as the first part of this series, and also to display the DomIt! Powerful and easy to use.

 

The first thing we need to do is call DomIt! Library File (here, download and decompress the DomIt! Library and put all the files in the same directory as the program files to be created below), and create a DomIt! Document example:

Require_once ('XML _ domit_shortde.php ');
$ XmlDoc = &NewDOMIT_Document ();// Document instance

Note that the "&" symbol is used to be compatible with PHP4.

 

In this way, an instance can be ready for use as needed. Have you seen the first line in the XML file content? Yes, we want to add the XML file declaration:

$xmlDecl=&$xmlDoc->createProcessingInstruction('xml','version="1.0"');
$xmlDoc->appendChild($xmlDecl);

Here we can see that the declaration information is also treated as a subnode, which is also a reasonable definition. But here we can find that the createProcessingInstruction () method has a major disadvantage-there are only two declared parameters (we may also define XML declaration information such as encoding ). Fortunately, we use an open-source library, that is, we can easily customize it to meet the requirements. If you really need help to modify this specific method to add enough XML file declaration information (such as encoding), I will introduce it in the last part of this series.
 

 

Let's go back to the topic. After the XML file declaration is complete, the "audio" tag is displayed in the XML file content. It is the root element (root node) in the XML file ). Let's create this part:

$rootElement=&$xmlDoc->createElement('audio');
$xmlDoc->appendChild($rootElement);

You don't need to worry about tag disabling, DomIt! It's done for you. Using a similar method, we will create the "file" sub-node of "audio". within the "file" element, it also contains several sister elements "track", "caption", and "record" and their text content. Since a major method appendChild () is used to create them, we can sum them up:

// "File" element
$ FileElement = & $ xmlDoc-> createElement ('file ');

// "Track" element
$ TrackElement = & $ xmlDoc-> createElement ('track ');
// Add the text content of the "track" element
$ TrackElement-> appendChild ($ xmlDoc-> createTextNode ('Music file location '));
// Add it to the "file" element
$ FileElement-> appendChild ($ trackElement );

// "Caption" element, others are the same as "track"
$ CaptionElement = & $ xmlDoc-> createElement ('Caption ');
$ CaptionElement-> appendChild ($ xmlDoc-> createTextNode ('Music file title '));
$ FileElement-> appendChild ($ captionElement );

// "Record" element, others are the same as "track"
$ RecordElement = & $ xmlDoc-> createElement ('record ');
$ RecordElement-> appendChild ($ xmlDoc-> createTextNode ('Music recording information '));
$ FileElement-> appendChild ($ recordElement );

As mentioned earlier, the core part is the appendChild () method. Calling it on different instances can play different roles (this is object-oriented ). If you use appendChild () to add the "track" element to "audio", then "file" and "track" will become sister nodes.
 

 

After creating all nodes, we need to add the content to the "audio" element, you may also want to output the final content to the screen for display and save it to a real XML file:

// Add all content in the "file" element to "audio"
$ RootElement-> appendChild ($ fileElement );
// Print it out on the screen (webpage)
Echo $ xmlDoc-> toNormalizedString (True);
// Save the document instance to a real XML file
$ XmlDoc-> saveXML ('audio. XML ',True);

Then on the screen (in the browser), you can see:

<? Xmlversion = "1.0"?>
>
>
>Music file location >
>Music file title >
>Music recording information >
>
>

The content of the file is exactly the same as that of the file. This is "use DomIt! The end of the first part of simple XML processing. Next, I will talk about how to modify the XML file we just created, including adding, deleting, and editing the content.
 

 

Oh, I forgot to give you a DomIt! Obvious download link (I know many of you may have Google out): http://sourceforge.net/projects/domit-xmlparser/

Download a copy of DomIt here !, You can also read its documentation before the next release.

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.