Hudzilla.org - the homepage of Paul Hudson
Contents > XML & XSLT > SimpleXML Wish List | Report Bug | About Me ]

12.3.4     Outputting XML: asXML()

This is NOT the latest copy of this book; click here for the latest version.

string asXML ( [void ])

One of the most interesting features about SimpleXML is that it can, at any time, give you a string containing the well-formed XML representation of its data. This essentially does the opposite of simplexml_load_file(), but the best bit about it is that it incorporates any changes you've made to the data whilst it was in SimpleXML form.

For example:

$xml = simplexml_load_file('employees.xml');
    $xml->employee[1]->age = 55;
    echo $xml->asXML();

That loads our XML file, and changes the second employee (remember, it's a zero-based array, so the 1 actually refers to the second element) to have an age of 55. The call to asXML() then outputs the changed data tree, printing this:

<?xml version="1.0"?>
<employees>
        <employee>
                <name>Anthony Clarke</name>
                <title>Chief Information Office</title>
                <age>48</age>
        </employee>

        <employee>
                <name>Laura Pollard</name>
                <title>Chief Executive Officer</title>
                <age>55</age>
        </employee>
</employees>

Note the changed value for Laura's age. However, blindly changing values isn't a smart move: the XML could change quite easily so that Pollard was no longer the second person in there. Instead, you should really combine it with an XPath search, like this:

<?php
    $xml
= simplexml_load_file('employees.xml');
    echo
"\nBefore transformation:\n\n";

    echo
$xml->asXML();

    
$xml->employee[1]->age = 55;

    
$employees = $xml->xpath('/employees/employee[name="Anthony Clarke"]');
    
$employees[0]->title = "Chairman of the Board, Chief Information Office";

    echo
"\n\nAfter transformation:\n\n";
    echo
$xml->asXML();
?>

This time the age is changed by referencing Laura directly, but I've also changed the job title of Anthony Clarke using a smart XPath search for his exact name. Of course, even names can be duplicated by chance, so an employee ID would be even better!





<< 12.3.3 Searching and filtering with XPath   12.4 Transforming XML using XSLT >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
NosyPete@NP.COM - 29 Aug 2008

It is hard to find documentation on how to delete a node by using simpleXML. Given the following xml snippet;
<rootNode>
<customer>
<id>1</id>
</customer>
<customer>
<id>2</id>
</customer>
</rootNode>

I want to retrieve the customer node with value 2 and delete it. Can you say something about how to do this?

regards
Nosy Pete

A PHP User - 29 Aug 2008

php is cool



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow