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, dont you think? At the very least, youll 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.

Heres 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, weve 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, Im 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 youre aware of the topics that will be discussed in the upcoming article, you wont want to miss it!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: