note 34338 added to ref.domxml
| From: | php at theovis dot com | Date: | Thu, 24 Jul 2003 11:15:41 +0000 |
| Subject: | note 34338 added to ref.domxml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-52546@lists.php.net to get a copy of this message | ||
<?php
/*
// I tried to get some functionality out of the the dom_xml functions (it's my first dom_xml
script)
// i created a little script to parse or to add/edit a xml file.
// It's probabaly not the most efficient but it seams to work
//
// It 's based on a (HR) xml file, and xsl file - you can get them here http://theovis.com/sample
//
// Used win32 - apache 1.3.26 - php 4.3.0 - Theo Vis php@theovis.com
// You are free to e-mail if you have comment on the code
// PS. Change the path your XML-FILES
///
// Default var - Standaard Variabelen
///
// Path is necessary to use domxml_open_file function
// In tried to use file_get_contents but it didn't seams to work
*/
$path = "C:/PATH/";
// Get file name from url input - Variabelen ophalen
$xmldoc = $_REQUEST['xmldoc'];
$xsldoc = $_REQUEST['xsldoc'];
$add = $_REQUEST['add'];
$edit = $_REQUEST['edit'];
// New-file
$xmlnieuw = $path."sample_personalia-new.xml";
// Start script
if (!$xmldoc){
// Set default xml-file - if there is nog xmldoc set
$xmldoc = "sample_personalia.xml";
// $xsldoc = "sample_personalia.xsl"; // View
$xsldoc = "edit_personalia.xsl";
}
$xmlfile = $path.$xmldoc;
$xslfile = $path.$xsldoc;
// Open XML - File
$dom = domxml_open_file($xmlfile);
////
// Edit (replace content)
////
if($edit){
// Walk trough the post vars (all elements within the form)
// Alle elementen doorlopen die gepost worden
while (list ($tagname, $tagcontent) = each ($_POST)) {
// Count how many childs there are
// Tellen hoeveel kinderen er zijn (for-each elementen)
for($i=0;$i < count($tagcontent);$i++){
$h = $i+1;
// Get content
// Content ophalen
$content = $tagcontent[$h];
// Get the elements
$elements = $dom->get_elements_by_tagname($tagname);
// Replace the content (first use have to set the element, else it would be added instead of
replace
// De inhoud vervangen
$element = $elements[$i];
$child = $element->first_child();
$child->set_content($content);
}
}
// Store the new content into a new-file
$result = $dom ->dump_file($xmlnieuw);
}
////
// Add (Add node (value (not attribute)
////
if ($add){
// To add a node (it's working, but the code it's buddy)
// It's first add's a parant then i will look for child node's
// It's working buttom up
$elements = $dom->get_elements_by_tagname($newelement);
$element = $elements[0];
// Get the parant
$parent = $element->parent_node();
$parent_name = $parent-> node_name();
$parent_elements = $dom->get_elements_by_tagname($parent_name);
$parent_element=$parent_elements[0];
// Get the grand parant
$grand_parent = $parent_element -> parent_node();
$grand_parent_name = $grand_parent -> node_name();
// Create Parent
$createparentnode = $dom -> create_element($parent_name);
$grand_parent -> append_child($createparentnode);
// Create child nodes
$parent_elements = $dom->get_elements_by_tagname($parent_name);
$countparents = count($parent_elements);
$parent_element = $parent_elements[0];
// The last element is the new element
$new_parant = $countparents - 1;
// Set new parant
$new_parent_element = $parent_elements[$new_parant];
$childnodes = $parent_element -> child_nodes();
$aantal = count($childnodes);
for($i=0;$i < count($childnodes);$i++){
$child = $childnodes[$i];
$child_name = $child -> node_name();
$type = $child ->node_type();
if ($type == 1){ // Not a text node
$createchildnode = $dom -> create_element($child_name);
$new_parent_element-> append_child($createchildnode);
}
//$createchildnode -> set_attribute("","");
}
$result = $dom ->dump_file($xmlnieuw);
}
////
// Display
////
// Add the xls-file to a parameter
$xslt_parameters[xsldocu] = $xsldoc;
$xslt_parameters[xmldocu] = $xmldoc;
// XSL Doc inladen - XML Document is all ready loaded
$xsldoc = domxml_xslt_stylesheet_file($xslfile);
// XML en XLS processen
$result = $xsldoc->process($dom,$xslt_parameters);
print $xsldoc->result_dump_mem($result);
?>
----
Manual Page -- http://www.php.net/manual/en/ref.domxml.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34338
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34338&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34338&report=yes