By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,795 Members | 1,235 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

$dom->get_elements_by_tagname('employee')->item(0);

P: 2
Hello all,

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
  1. <?php
  2.  
  3. function result($dom)
  4. {
  5. echo "<HTML>";
  6. echo "<Head>";
  7. echo "<title>Example of DOMNode->replaceChild</title>";
  8. echo "<Head>";
  9. echo "<body>";
  10. echo "<PRE>";
  11. echo htmlentities($dom->dump_mem(true));
  12. echo "</PRE>";
  13. echo "</body>";
  14. echo "</HTML>";
  15. }
  16.  
  17.  
  18. // Load the XML source
  19. $dom = domxml_open_file("test.xml");
  20.  
  21. result($dom);
  22.  
  23.  
  24. //retrieve the first element with name employee
  25. $employee = $dom->get_elements_by_tagname('employee')->item(0);
  26.  
  27. //retrieve the first element with name employeeaddress
  28. $employeeaddress = $dom->get_elements_by_tagname('employeeaddress')->item(0);
  29.  
  30. //create a new element
  31. $newChild = $dom->create_element('newNode');
  32.  
  33. //create a text node
  34. $textNode = $dom->create_text_node('newNode');
  35.  
  36. //append the text to the  newly created element
  37. $newChild->append_child($textNode);
  38.  
  39. //replace the employeeaddress element by newly created element
  40. $employee->replace_child($newChild,$employeeaddress);
  41.  
  42. result($dom);
  43.  
  44. ?>
  45.  
The XML file is (test.xml):
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <employee>
  3. <employeename id="myId">test1</employeename>
  4. <employeeaddress>testAddress1</employeeaddress>
  5. <SSN>11111</SSN>
  6. <company>XYZ Corporation</company>
  7. </employee>
Aug 4 '08 #1
Share this Question
Share on Google+
1 Reply


P: 2
Found the solution, sorry for the inconvenience. Perhaps it helps someone:

PHP4 code should be:
Expand|Select|Wrap|Line Numbers
  1. //retrieve the first element with name employee
  2. $root = $dom->document_element();
  3.  
  4. //retrieve the first element with name employeeaddress
  5. $employeeaddress = $dom->get_elements_by_tagname('employeeaddress');
  6.  
  7. //create a new element
  8. $newChild = $dom->create_element('newNode');
  9.  
  10. //create a text node
  11. $textNode = $dom->create_text_node('newNode');
  12.  
  13. //append the text to the  newly created element
  14. $newChild->append_child($textNode);
  15.  
  16. //replace the employeeaddress element by newly created element
  17. $root->replace_child($newChild,$employeeaddress[0]);
  18.  
regards
Aug 4 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.