hi all, i’m having a bear of a time working with the dom in order to manipulate xml files. the php5 functionality is relatively new and online tutorials are sorely lacking. the php5 documentation and examples are valuable, but often limited in scope.
so, i want to create a thread to share what i’ve learned (through blood sweat and tears - at least for the DOM stuff) and ask others to contribute their knowledge to help me, and others, learn more about DOM, XML, etc…
a one stop shop for php5 simplexml, dom, xpath, if you will. i’ll probably have to learn xslt eventually, so feel free to chime in with your expertise there.
i’m going to use a simplified version of php5’s simplexml sample xml here:
http://us2.php.net/manual/en/ref.simplexml.php
in this first post, i’ll show some exampls of using simplexml -the simplest way to get data out of an xml file (the downside is that it can’t write to the xml file). here’s an example of the syntaxt:
<?PHP
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
<movie>
<title>PHP: Behind the Parser</title>
<characters>
<character>
<name>Ms. Coder</name>
<actor>Onlivia Actora</actor>
</character>
</characters>
</movie>
<movie>
<title>PHP and the DOM</title>
<rating type='thumbs'>7</rating>
<rating type='stars'>5</rating>
</movie>
</movies>
XML;
$xml = simplexml_load_string($xmlstr);
// pulls first movie reference.
$title = $xml->movie[0]->title;
// $title = $xml->movie[0]->title; will also pull the first [0] element value.
echo 'First Title: '.$title.'<br />';
// works with only one instance of title.
$title = $xml->movie[1]->title;
echo 'Second Title: '.$title.'<br />';
echo '<br />';
//you can loop through all movie values and pull the title:
echo 'The Loop Method: <br />';
foreach ($xml->movie as $movie) {
echo $movie->title, '<br />';
}
?>
$xml = simplexml_load_string($xmlstr);
// pulls first movie reference.
$title = $xml->movie[0]->title;
// $title = $xml->movie[0]->title; will also pull the first [0] element value.
echo 'First Title: '.$title.'<br />';
// works with only one instance of title.
$title = $xml->movie[1]->title;
echo 'Second Title: '.$title.'<br />';
echo '<br />';
//you can loop through all movie values and pull the title:
echo 'The Loop Method: <br />';
foreach ($xml->movie as $movie) {
echo $movie->title, '<br />';
}
?>
i use a forms class and stick $title as the value of my form. for some reason, it seems that this syntax outputs an object instead of a string. in order to get it to work right, i had to add the following after getting the value of $title using simplexml:
function to_string_trim($var){
// forms class inconsistency requires string conversion
// trim white space off beginning and end of xml data. simplexml
// apparently doesn't have a method to eliminate this white space.
$var = trim((string) $var);
return $var;
}
$dns_3 = to_string_trim($dns_3);
the function casts to string (required for display in forms class or to compar to another sting) and trims the white space (which can cause problems, especially when comparing user input to the xml file data).
this covers the basics of pulling data out of an xml file using simplexml.
in the next post, we’ll take a look at incorporating xpath with simplexml.