Re: SimpleXML and documentation
| From: | Tobias Bradtke | Date: | Fri, 02 Jan 2004 23:46:18 +0000 |
| Subject: | Re: SimpleXML and documentation | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-173658@lists.php.net to get a copy of this message | ||
hm.. attachement didn't work.
here is the script:
<?php
echo "<pre>";
// at first:
// sorry for my bad english! =)
// i'm using php5beta3 on win32
// some interessting links:
//
// http://www.w3.org/TR/DOM-Level-2-Core/core.html
//
$xml_string = "
<books>
<book id='22'>
<title attr='attr'>The Grapes of Wrath</title>
<author>John Steinbeck</author>
</book>
<book id='23'>
<title>The Pearl</title>
<author>John Steinbeck</author>
</book>
<test>
<dog>
<snout>
<cat>
<stomach>
<mouse />
</stomach>
</cat>
</snout>
</dog>
</test>
<test />
<test />
<test />
</books>
";
$sxe = simplexml_load_string($xml_string);
// i can't call sxe_dump() twice in this script
// causes crash.. (?)
//echo sxe_dump($sxe);
//echo sxe_dump($sxe);
// you can read and write the content of a node
echo $sxe->book[0]->title;
$sxe->test[0]->dog->snout->cat->stomach = "cat food";
// you can add/set an attribute
$sxe->test[0]->dog['mood'] = "evil";
// and remove it again
unset($sxe->test[0]->dog['mood']);
// but i don't know how to add another dog:
// $sxe->test[0]->dog[1]-> "test";
// ^conflicts with adding/setting attributes, i think
// and i can't add another element:
// (no error, but no effect too)
$sxe->newelement = "text_text";
// error; thinks 'newelement' is an attribute
$sxe->newelement = simplexml_load_string("<text_text>bla</text_text>");
// no error, no effect:
$sxe->newelement[] = simplexml_load_string("<text_text>bla</text_text>");
// to add a new element you can workaround with this:
// first you export the simplexml to dom.
// then you can append (or whatever) a new created child
// this immediatly takes effect on the $sxe! :) great!
//
// the create_node_from_string() is listed below
// only one line, unfortunately you have to pass
// the 'ownerDocument' or the new document-part
// can't be imported correctly..
// comment these two lines
// if you want to manipulate the search-result below..
$dom = dom_import_simplexml($sxe);
$dom->appendChild(create_node_from_string($dom->ownerDocument,
"<book><title>new_book</title><isbn>456</isbn></book>"));
// you also can manipulate xsearch-results directly:
$xsearch = $sxe->xsearch('*[@id="23"]');
print_r($xsearch);
// for me this doesn't work if i did append the child above
// you can uncomment these two lines
// if you comment out the lines above
//
// :(
/*
$dom = dom_import_simplexml($xsearch[0]);
$dom->appendChild(create_node_from_string($dom->ownerDocument,
"<isbn>123abc</isbn>"));
*/
echo "<br /><br />";
echo sxe_dump($sxe);
// mayby some interesting output..
echo "<br /><br />";
print_r(get_class_methods('simplexml_element'));
print_r(get_class_methods('domelement'));
print_r(get_class_methods('domnode'));
exit;
function create_node_from_string($document, $xml_string)
{
return
$document->importNode(dom_import_simplexml(simplexml_load_string($xml_string)),
true);
}
function sxe_dump($sxe)
{
// tag_name
$tagName = dom_import_simplexml($sxe)->nodeName;
$echo = "<span style='color:#007700'><</span><span
style='color:#0000BB'>$tagName</span>";
// attributes
foreach ($sxe->attributes() as $attributeName => $attributeValue)
$echo .= " <span
style='color:#0000BB'>$attributeName=</span><span
style='color:#DD0000'>\"$attributeValue\"</span>";
$count = count(get_object_vars($sxe));
if ($count or (string)$sxe)
{
// nodeValue
$echo .= "<span
style='color:#007700'>></span>".trim($sxe);
// child_nodes
if ($count)
{
$echo .= "<div style='margin:0 1em'>";
foreach ($sxe as $childNode)
$echo .= sxe_dump($childNode)."<br />";
$echo .= "</div>";
}
// close tag_name
$echo .= "<span style='color:#007700'></</span><span
style='color:#0000BB'>$tagName</span><span
style='color:#007700'>></span>";
}
else
{
// close empty tag_name
$echo .= " <span style='color:#007700'>/></span>";
}
return $echo;
}
?>