Home arrow PHP arrow Page 4 - Working with Multiple Document Nodes with the DOM XML Extension in PHP 5

Reading data from a specified XML string with the loadXML() method - PHP

Welcome to the fourth article of the series "A quick overview of the DOM XML extension in PHP 5." By means of easy-to-grasp hands-on examples, this series equips you with a friendly guide to using the most relevant methods that come bundled with the DOM XML PHP extension to help you work with XML documents in a truly painless way.

TABLE OF CONTENTS:
  1. Working with Multiple Document Nodes with the DOM XML Extension in PHP 5
  2. Getting a collection of XML nodes
  3. Loading XML data from a specified text file with the load() method
  4. Reading data from a specified XML string with the loadXML() method
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 18, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I noted in the previous section, the DOM XML extension offers a specific method whose function is based on reading XML data straight from a given string. The method is called “loadXML()”, and an example of how to use is listed below, so take a look at it, please:

// example on loading XML data from an existing string using the 'loadXML()' method

$xml='<?xml version="1.0" encoding="iso-8859-1"?><headlines><headline id="economics"><image>image1.jpg</image><url>Link for headline 1 goes here</url><text>Text for headline 1 goes here</text></headline><headline id="sports"><image>image2.jpg</image><url>Link for headline 2 goes here</url><text>Text for headline 2 goes here</text></headline></headlines>';

$dom=new DOMDocument();

// load XML data from string

$dom->loadXML($xml);

// tell the browser the output is XML via the 'Content-Type' HTTP header

header('Content-Type: text/xml');

// display DOM document

echo $dom->saveXML();


/* displays the following

<?xml version="1.0" encoding="iso-8859-1"?>

<headlines>

<headline id="economics">

<image>image1.jpg</image>

<url>Link for headline 1 goes here</url>

<text>Text for headline 1 goes here</text>

</headline>

<headline id="sports">

<image>image2.jpg</image>

<url>Link for headline 2 goes here</url>

<text>Text for headline 2 goes here</text>

</headline>

</headlines>

*/

Definitely, reading XML data from a specified string by using the previous “loadXML()” method is not only simple, but pretty fun, don’t you think? At the very least, you’ll have to agree with me that the DOM XML extension makes reading XML data either from a specific file or from a plain string a no-brainer process.

Here’s my final suggestion: feel free to tweak all of the code samples included here to acquire a better understanding of working with the DOM XML PHP extension. Have fun!

Final thoughts

As I said before, we’ve come to a close. However, hopefully this fourth chapter of the series has opened your eyes a bit more about the use of the methods reviewed earlier. As I showed you, they can be used to handle collections of XML nodes, and even complete XML files and strings.

In the next part of the series, I’m going to show you how to utilize the DOM XML library to read plain HTML files, as well as build HTML documents by using their respective DOM-based representation.

Now that you’re aware of the topics that will be discussed in the upcoming article, you won’t want to miss it!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap

Dev Shed Tutorial Topics: