Page 337 - Open Soource Technologies 304.indd
P. 337

Unit 13: Extensible Markup Language



            <comment>It really does fit in your pocket</comment>                                  Notes
            </book>
            <book>

            <title>Indian Cookbook</title>
            <authors>

            <author> xyz</author>
            <author> ABC</author>
            </authors>
            <isbn>1-xxxx0-2</isbn>

            <comment>Hundreds  of useful techniques, most just as applicable to PHP as to Indian </
            comment>

            </book>
            </library>
            The PHP application parses the file and presents the user with a list of books, showing just the
            titles and authors.
            We define a class, BookList, whose constructor parses the XML file and builds a list of records.
            There are two methods on a BookList that generate output from that list of records. The show_
            menu(  ) method  generates the book menu,  and  the show_book( ) method  displays  detailed
            information on a particular book.
            Parsing the file involves keeping track of the record, which element we are in, and which elements
            correspond to records (book) and fields (title, author, isbn, and comment). The $record property
            holds the current record as its being built, and $current_field holds the name of the field we
            are currently processing (e.g., ‘title’). The $records property is an array of all the records we
            have read so far.
            Two  associative  arrays,  $field_type  and  $ends_record,  tell  us  which  elements  correspond  to
            fields in a record and which closing element signals the end of a record. Values in $field_type
            are either 1 or 2, corresponding to a simple scalar field (e.g., title) or an array of values (e.g.,
            author) respectively. We initialize those arrays in the constructor.
            The handlers themselves are fairly straightforward. When we see the start of an element, we
            work out whether it corresponds to a field we are interested in. If it is, we set the current_field
            property to be that field name so when we see the character data (e.g., the title of the book) we
            know which field it is the value for. When we get character data, we add it to the appropriate
            field of the current record if current_field says we are in a field. When we see the end of an
            element, we check to see if it is the end of a record—if so, we add the current record to the
            array of completed records.
            One PHP script, given in Example, handles both the book menu and book details pages. The
            entries in the book menu link back to the URL for the menu, with a GET parameter identifying
            the ISBN of the book whose details are to be displayed.
                          Bookparse.xml

            <html>
            <head>
            <title>My Library</title>


                                             LOVELY PROFESSIONAL UNIVERSITY                                   331
   332   333   334   335   336   337   338   339   340   341   342