Hi,
I'm trying this example but i keep getting: Fatal error: Call to a member function on a non-object in /xml4.php on line 44
Here's my code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Nyt dokument</title>
<meta name="generator" content="TSW WebCoder">
</head>
<body>
<?php
make an example xml document to play with
$xmlstr = "<" . "?" . "xml version=\"1.0\"" . "?" . ">";
$xmlstr .=
"
<employee>
<name>Matt</name>
<position type=\"contract\">Web Guy</position>
</employee>
";
load xml data ($doc becomes an instance of
the DomDocument object)
$doc = xmldoc($xmlstr);
get root node "employee"
$employee = $doc->root();
get employee's children ("name","position")
$nodes = $employee->children();
let's play with the "position" node
so we must iterate through employee's
children in search of it
while ($node = array_shift($nodes))
{
if ($node->name == "position")
{
$position = $node;
break;
}
}
get position's type attribute
$type = $position->getattr("type");
get the text enclosed by the position tag
shift the first element off of position's children
$text_node = array_shift($position->children());
access the content property of the text node
$text = $text_node->content;
echo out the position and type
echo "position: $text<BR>";
echo "type: $type";
?>
</body>
</html>