I would greatly appreciate suggestions on my following PHP4 problem:
In PHP5 this works:
$employee = $dom->getElementsByTagName('employee')->item(0);
In PHP4 this doesn't work:
$employee = $dom->get_elements_by_tagname('employee')->item(0);
I am trying to travese a XML DOM to replace a textnode.
It works in PHP5 but not in PHP4 because of the "->item(0)" part
The error message thrown in PHP4 is:
Parse error: parse error, unexpected T_OBJECT_OPERATOR in line 25
any suggestions would be greatly appreciated.
Complete PHP4 code is:
Expand|Select|Wrap|Line Numbers
- <?php
- function result($dom)
- {
- echo "<HTML>";
- echo "<Head>";
- echo "<title>Example of DOMNode->replaceChild</title>";
- echo "<Head>";
- echo "<body>";
- echo "<PRE>";
- echo htmlentities($dom->dump_mem(true));
- echo "</PRE>";
- echo "</body>";
- echo "</HTML>";
- }
- // Load the XML source
- $dom = domxml_open_file("test.xml");
- result($dom);
- //retrieve the first element with name employee
- $employee = $dom->get_elements_by_tagname('employee')->item(0);
- //retrieve the first element with name employeeaddress
- $employeeaddress = $dom->get_elements_by_tagname('employeeaddress')->item(0);
- //create a new element
- $newChild = $dom->create_element('newNode');
- //create a text node
- $textNode = $dom->create_text_node('newNode');
- //append the text to the newly created element
- $newChild->append_child($textNode);
- //replace the employeeaddress element by newly created element
- $employee->replace_child($newChild,$employeeaddress);
- result($dom);
- ?>
Expand|Select|Wrap|Line Numbers
- <?xml version="1.0" encoding="UTF-8"?>
- <employee>
- <employeename id="myId">test1</employeename>
- <employeeaddress>testAddress1</employeeaddress>
- <SSN>11111</SSN>
- <company>XYZ Corporation</company>
- </employee>