, php.
(, , persons.xml). DomNode->clone_node(). , <person> ( ).
- . , , .. DOM DomNode->append_child(new_node), ( ).
PHP 4.3 . , , , . PHP , . W3C.
, , , .. DomNode->unlink_node().
//
// , ,
// .
// person,
//
$elements = $dom->get_elements_by_tagname("person");
$element = $elements[0];
//
$parent = $element->parent_node();
$children = $element->child_nodes();
// person
$person = $element->clone_node();
//
//
$attr = $person->set_attribute("id", "30");
// ,
//
foreach ($children as $child){
//
$node = $child->clone_node();
//
$grand_children = $child->child_nodes();
// ,
//.. ,
if (count($grand_children)<>1){
//
//
//
foreach($grand_children as $grand_child){
$lastnode = $grand_child->clone_node();
//
//
if ($grand_child->tagname=="first")
$cont = $lastnode->set_content("Nina");
if ($grand_child->tagname=="last")
$cont = $lastnode->set_content("Saveljeva");
if ($grand_child->tagname=="day")
$cont = $lastnode->set_content("7");
if ($grand_child->tagname=="month")
$cont = $lastnode->set_content("06");
if ($grand_child->tagname=="year")
$cont = $lastnode->set_content("1981");
$newlastnode = $node->append_child($lastnode);
|
|
}
}
if ($child->tagname=="email") {
$cont = $node->set_content("[email protected]");
}
$newnode2 = $person->append_child($node);
}
$newnode = $parent->append_child($person);
//dump_mem XML- dom
//
echo "<PRE>";
$xmlfile = $dom->dump_mem(true);
// ,
//
echo htmlentities($xmlfile);
echo "</PRE>";
// XML-
// "test.xml"
$h = fopen("test.xml","a");
if (!fwrite($h, $xmlfile)) {
print "Cannot write ". "to file ($filename)";
exit;
}
}
14.5.
, , , XML -. , , . PHP5 DOM. .