This page is dedicated to the discussion on using the Perl module XML::DOM to read and write XML files.

Links on this page:

Reading an XML file with only elements using XML::DOM
Reading an XML file with elements and one attribute
Creating an XML file with one book and one attribute.
Creating an XML file by reading a text file.
Adding a child node,
Deleting a child node.
Checking whether a file is a valid XML file

Reading an XML file with only elements using XML::DOM

Here we show how to use the XML::DOM module to read an XML file.

The name of the file below is GetBookValuesXML_DOM_1.pl which may be loaded from here. This reads the file Book1.xml. If you want to display error messages, then replace the first line with "#!perl -w".

You cannot copy and paste the code below as it is an image file obtained from a screenshot of a notepad++ (a free download).

GetBookValuesXML_DOM_1.pl

Some of the code seems obtuse but the only tricky part is to remember that if you have multiple children of the same element (e.g., the <author> elements are children of <author>, and then use item($count) to iterate through all the children.
Back to top.

Reading an XML file with elements and one attribute.

Below is the Perl program GetBookValuesXML_DOM_2.xml which reads the file Book2.xml and may be loaded from here. In this case, we have to read the attribute of <condition> which is "price".

GetBookValuesXML_DOM_2.jpg
Back to top.

Creating an XML file with one book and one attribute.

The file below is called CreateBooksXML_DOMSmall.pl and it creates one new XML file called NewBooksSmall_2.xml with elements and one attribute "price". There is a header file created (line 5). For printing, you can use lines 58 and 59 which outputs to your default output device (but can be redirected to a file with an .xml extension). Alternately, you can just use line 67 (but this does not output $xml_pi). This program may be obtained from here.

CreateBooksSmall_1.jpg

CreateBooksSmall_2.jpg
CreateBooksSmall_3.jpg
Back to top.

Creating an XML file by reading a text file.

In this case we read a text file called Book.txt that has a list of titles, authors and descriptions loaded from here. As seen below, the first line has the title of the book, the second line has the authors separated by a comma and the third line has the sets of descriptions, each set separated by a semi-color, and the condition and price separated by a comma. The fourth line starts with another title.

Book.txt.jpg

Program ReadBookFileAndCreateXMLUsingXML_DOM.pl is below and may be loaded from here. It outputs a file called NewBooksXML_DOM_2.xml.

CreateXMLFromText.jpg
CreateXMLFromText_2.jpg
CreateXMLFromText_3.jpg
Back to top

Adding a child node:

Sometimes it is necessary to a XML file and add another entry. Load the program AddToBooksXML_DOM.pl from here. The program is below:

AddToBooks_1.jpg
AddtoBooks_2.jpg
Back to top

Deleting a child node.

A program to delete a child node when the "condition" is "fair" may be loaded from here. It produces a new file called FairRemovedChildXML_DOM.xml:

Remove_1.jpg
Remove_2.jpg
Back to top.

Before you read a file that supposedly is a valid XML file, you should check if it is so:

ValidXML_DOMfile.jpg

Back to top