Accessing elements and attributes using SimpleXML
This recipe will explain how we can get the values of the node and/or attributes from an XML file using SimpleXML methods. We will write an example using our common.xml
file that will be used to get the publication year or list of stories in a selected book.
Getting ready
Create a folder for this recipe in the Chapter3
directory and name it as Recipe2
.
How to do it...
Create a new file named
index.php
inRecipe2
folder. In this file, create a select box and create its options, which will be the names of books in thecommon.xml
file. Next, create two buttons that will get the publication year and list of stories in the selected book. Each of these buttons has an ID attribute that will be used to distinguish between the clicked buttons. After that, create a paragraph element to display the result.<html> <head><title>Accessing node and attribute values</title></head> <body> <p> <select id="bookList...