How to Read an XML File With PHP

How to Read an XML File With PHP thumbnail
SimpleXML makes reading XML in your PHP scripts easy.

Extensible Markup Language (XML) allows you to organize information in a way that describes the information and is easy for computer programs to read. There are several popular XML dialects (formats), including RSS, Atom, XHTML, and OpenDocument. You can create your own XML dialect to suit your data, or even add on to an existing dialect. The PHP: Hypertext Preprocessor (PHP) programming language, a popular dynamic web page scripting language, features the built-in SimpleXML extension, which simplifies reading and using XML in your PHP scripts. Read an RSS feed, XML office documents, or your own custom XML document.

Things You'll Need

  • XML document<br />PHP script<br />PHP code editor or text editor
Show More

Instructions

    • 1

      Call the PHP \"simplexml_load_file()\" function in your PHP script. Pass the function the path and filename to where your XML file lives on your web server as the function parameter. Assign the function's return value to a variable. The function opens your XML file, reads the XML content, and makes the XML content available to the script by returning an object containing the XML. Using the following XML file, named \"order.xml,\" as an example:<br /><br /><?xml version=\"1.0\"?><br /><order date=\"2010-04-02\"><br /> <shipping><br /> <name>Carol Roberts</name><br /> <street>123 High St.</street><br /> <city>Columbus</city><br /> <state>OH</state><br /> <zip>43210</zip><br /> </shipping><br /> <billing><br /> <name>Alice Johnson</name><br /> <street>321 Main St.</street><br /> <city>Columbus</city><br /> <state>OH</state><br /> <zip>43212</zip><br /> </billing><br /> <comment>Please gift wrap.</comment><br /> <contents><br /> <product part=\"ZF-987\"><br /> <name>Camelhair Paintbrush, Medium</name><br /> <quantity>2</quantity><br /> <price>19.99</price><br /> <shipped>2010-04-13</shipped><br /> </product><br /> <product part=\"ZF-357\"><br /> <name>Watercolor Box Set</name><br /> <quantity>1</quantity><br /> <price>49.99</price><br /> <shipped>2010-04-09</shipped><br /> </product><br /> </contents><br /></order><br /><br /><br />The order.xml file, located in the same directory as the PHP script, is loaded and assigned to $xml:<br /><br />$xml = simplexml_load_file(\"order.xml\") <br />GO<br /><br />simplexml_load_file() reads the XML document, interprets it into a PHP SimpleXML object, and returns the object, where it can be assigned to a variable or passed as a parameter to another function. In the example, $xml references the root (outermost) element of the file, \"order.\"

    • 2

      Access the root element's child elements (the first level of elements nested in the root element) from the variable holding the SimpleXML object by using the arrow operator (->) to reference each element by its element name. Accessing the XML elements allows you to print (echo), assign to variables, or process the element content as needed. For example, the complex \"shipping\" element, along with all of its child elements (elements nested in the \"shipping\" element), are assigned to the $ship_to variable. The text content of the \"comment\" element is assigned to $comment:<br /><br /> $ship_to = $xml->shipping <br />GO<br /> $comment = $xml->comment ;

    • 3

      Access individual elements' attributes (the name=value pairs listed in an element) using associative array syntax, providing each attribute's name to retrieve the attribute's value. For example, the value of the \"date\" attribute of the \"order\" element, referenced by the $xml variable, is accessed as follows:<br /><br />$order_date = $xml['date'] <br />GO<br /><br />$order_date now contains the value \"2010-04-02\".

    • 4

      Process repeating elements in a \"foreach\" loop, providing these elements to the loop's array expression by referencing them using the arrow operator. For example, the order.xml document contains the \"contents\" element, which in turn contains two repeating child elements named \"product.\" The reference \"$xml->contents->product\" provides all the \"product\" elements as an iterable object (an object that can be used to count through a loop) as the loop's array expression. The child elements of each \"product\" are then assigned to variables and displayed using \"echo\":<br /><br />foreach ($xml->contents->product as $item) {<br /> $part = $item['part'] <br />GO<br /> $name = $item->name <br />GO<br /> $qty = $item->quantity <br />GO<br /> $price = $item->price <br />GO<br /><br /> echo \"$name $part: $qty at \\$$price\\n\" <br />GO<br /> }<br /><br />Note the child element references can be stacked (as in $xml->contents->products), allowing you access elements nested deep in the XML file by using the arrow operator.

Tips & Warnings

  • PHP's naming convention does not allow certain characters, such as the hyphen (-). If your XML document contains an element that uses these characters, wrap the element name with apostrophes and braces to access the element. For example, an element named \"publish-date\" is accessed like this:<br /><br />$xml->book->{'publish-date'}->year

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured