[phpXML] Documentation and examples
Brought to you by:
bs_php,
nigelswinson
From: <nig...@us...> - 2001-10-09 13:11:46
|
> I am new to phpxml and trying to implement a PHP / XML CSM system. I > cant seem to get some of the methods to work, no matter how hard I > try. And I don't seem to get any helpful error msgs. I have gone > through everything I can find on the internet about phpxml, but I cant > find any examples of some methods being used (for example add_node, > remove_node, set_content etc). I was wondering if you have any real > world examples of how these methods being used, or know where I could > find any? if anybody can point me to some sources of a working system > that uses these and other methods, especially to edit and write XML > files I would really be pleased. If you are new to phpxml, then the problem is probably that you are = trying to use add_node, remove_node, etc with a $context parameter that = is not a "Full path of the parent". If you pass "//Element" instead of = "/Root[1]/Element[4]" then the call isn't going to work. In general you = need to use evaluate() in order to get an array of "Full paths" that you = then use with each of the other functions. You can't just use the other = functions with any old XPath expression, even if it would evaluate to = only one node. As for examples and documentation, the most recent documentation is the = stuff that ships with phpxml 1.0. Also at: = http://www.phpxml.org/scripts/documentation/ but there is a more recent = version of the class, available at = http://sourceforge.net/projects/phpxmldb/ as 1.N.X releases. I'm still = waiting and hoping for "approval" from Michael for this "fork" so that = it can be considered 1.X releases not 1.N.X releases. I have no = immediate plans to update the html documentation, but will continue to = commit to the source documentation. The release notes describe the = changes. If anyone wants to volunteer to take on the task of maintaining the html = documentation given the comments in the 1.N.X releases, then that would = be great. :o) Examples wise the best I can offer is the phpxmldb project, that is = built upon the phpxml work also, not surprisingly, available from = http://sourceforge.net/projects/phpxmldb/ The code is well documented, = and might help you with how to use the phpxml class. As a short example = below is the private AddRecord function that shows use of add_node() and = add_content() functions. I hope that this post helps flo...@co... and = lu...@sa... with their request too. Nigel -------------------------------------------------------------------------= ------- = /////////////////////////////////////////////////////////////////////////= /////////////////// // Internal version of AddRecord. =20 function _AddRecord($TableTag, $RecordTag, $aElementData) { =20 // If you are having difficulty using this function. Then set this to = true and=20 // you'll get diagnostic info displayed to the output. $bDebugThisFunction =3D false; =20 if ($bDebugThisFunction) { $aStartTime =3D $this->BeginDebugFunction("AddRecord"); echo "TableTag: $TableTag\n";=20 echo "RecordTag: $RecordTag\n";=20 echo "ElementData:\n"; print_r($aElementData); } =20 // Numpty check if (!$TableTag || !$RecordTag) { trigger_error("The TableTag and RecordTag arguments must have a = value."); return false; } =20 ////////////////////////////////////////////// =20 // Get the table path, creating the table if it doesn't exist. $TablePath =3D $this->_FindTable($TableTag, true); =20 // Obtain a new record id. $NewRecordId =3D $this->_GetNewRecordId($TablePath); if ($bDebugThisFunction) echo "New record id is $NewRecordId\n"; =20 ////////////////////////////////////////////// =20 // Add the record if ($bDebugThisFunction) echo "Adding new record at $TablePath\n"; $RecordPath =3D $this->XmlDb->add_node($TablePath, $RecordTag); $this->XmlDb->add_attributes($RecordPath, array("created" =3D> = $this->GetDbTime(), "RecordId" =3D> $NewRecordId)); if ($bDebugThisFunction) echo "New record now at $RecordPath\n"; =20 ////////////////////////////////////////////// // Add the content. foreach($aElementData as $Name =3D> $Value) { $EntryPath =3D $this->XmlDb->add_node($RecordPath, "$Name"); if ($bDebugThisFunction) echo "Added new entry $EntryPath\n";=20 =20 // Prepare the value by stripping \r's and calling htmlspecialchars. $Value =3D htmlspecialchars($Value); $Value =3D str_replace("\r", "", $Value); =20 $this->XmlDb->add_content($EntryPath, $Value); if ($bDebugThisFunction) echo "Added new content to $EntryPath: = ".$this->XmlDb->get_content($EntryPath)."\n";=20 } =20 if ($bDebugThisFunction) echo "\n"; =20 $bResult =3D true; =20 ////////////////////////////////////////////// =20 if ($bDebugThisFunction) $this->CloseDebugFunction($aStartTime, = $bResult); return $bResult; } -------------------------------------------------------------------------= ------- |