PHP XML Parsing Basic XML Documents - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript PHP XML Parsing Basic XML Documents - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Friday, June 14, 2019

PHP XML Parsing Basic XML Documents

PHP XML




Parsing Basic XML Documents


Problem

You want to parse a basic XML document that follows a known schema, and you don’tneed access to more esoteric XML features, such as processing instructions.

Solution

Use the SimpleXML extension. Here’s how to read XML from a file:

         $sx = simplexml_load_file(__DIR__ . '/address-book.xml');

         foreach ($sx->person as $person) {
                $firstname_text_value = $person->firstname;
                $lastname_text_value = $person->lastname;

                print "$firstname_text_value $lastname_text_value\n";
         }

         David Sklar
         Adam Trachtenberg

Discussion

SimpleXML has been described as “the mostest bestest thing ever.” Though it’s hard to live up to such grand praise, SimpleXML does do a remarkable job of making it—dare we say—simple to interact with XML. When you want to read a configuration file written in XML, parse an RSS feed, or process the result of a REST request, SimpleXML excels at these tasks. It doesn’t work well for more complex XML-related jobs, such as reading a document where you don’t know the format ahead of time or when you need to access processing instructions or comments.

SimpleXML turns elements into object properties. The text between the tags is assigned to the property. If more than one element with the same name lives in the same place (such as multiple <people>s), then they’re placed inside a list.

Element attributes become array elements, where the array key is the attribute name and the key’s value is the attribute’s value.

To access a single value, reference it directly using object method notation. Let’s use this XML fragment as an example:

         <firstname>David</firstname>

If you have this in a SimpleXML object, $firstname, here’s all you need to do to access David:

         $firstname

SimpleXML assumes that when you have a node that contains only text, you’re interested in the text. Therefore, print $firstname does what you expect it to: it prints David.

Iteration methods, like foreach, are the best choice for cycling through multiple elements. Code for this is shown in later examples.

Attributes are stored as array elements. For example, this prints out the id attribute for the first person element:

         $ab = simplexml_load_file(__DIR__ . '/address-book.xml');

         // the id attribute of the first person
         print $ab->person['id'] . "\n";

which gives you:

         1

Here’s a more complete example based on this simple address book in XML. It’s used in the code examples that follow.

         <?xml version="1.0"?>
         <address-book>
               <person id="1">
                     <!--David Sklar-->
                     <firstname>David</firstname>
                     <lastname>Sklar</lastname>
                     <city>New York</city>
                     <state>NY</state>
                     <email>sklar@php.net</email>
               </person>

               <person id="2">
                     <!--Adam Trachtenberg-->
                     <firstname>Adam</firstname>
                     <lastname>Trachtenberg</lastname>
                     <city>San Francisco</city>
                     <state>CA</state>
                     <email>amt@php.net</email>
               </person>
              </address-book>

Use SimpleXML to pull out all the first and last names:

              $sx = simplexml_load_file(__DIR__ . '/address-book.xml');

              foreach ($sx->person as $person) {
                    $firstname_text_value = $person->firstname;
                    $lastname_text_value = $person->lastname;

                    print "$firstname_text_value $lastname_text_value\n";
              }

              David Sklar
              Adam Trachtenberg

When you use SimpleXML, you can directly iterate over elements using foreach. Here, the iteration occurs over $sx->person, which holds all the person nodes.

You can also directly print SimpleXML objects:

               foreach ($sx->person as $person) {
                    print "$person->firstname $person->lastname\n";
               }

               David Sklar
               Adam Trachtenberg

PHP interpolates SimpleXML objects inside of quoted strings and retrieves the text stored in them.



No comments:

Post a Comment

Post Top Ad