0

I want to replace third table in php dom object with a new table. I can select it by

$table = $dom->getElementsByTagName('table')->item(3);

I tried

$table->parentNode->appendChild($new_table);

and it says

Catchable fatal error: Argument 1 passed to DOMNode::appendChild() must be an instance of DOMNode, string given in C:\xampp\htdocs\index.php on line 73

Can someone please explain what is wrong with the code or how I can correct it?

$new_table = "<table width='100%' bgcolor='#000'>$table_rows</table>";

3 Answers 3

1

You'll have to use createElement to create the table fragment before appending it.

You could also create a fragment from the XML using appendXML and then append that fragment using appendChild

$fragment = $dom->createDocumentFragment();
$fragment->appendXML("<table width='100%' bgcolor='#000'>$table_rows</table>");
// now append the fragment
$table->parentNode->appendChild($fragment);

Here's a working example: http://ideone.com/0Lx742

Sign up to request clarification or add additional context in comments.

4 Comments

it says: Warning: DOMNode::appendChild(): Document Fragment is empty $table->parentNode->appendChild($fragment);
Not sure what else you're doing wrong but here's a working example: ideone.com/0Lx742
@Azee Care to share? What turned out to be the problem?
Sure thing :) I just removed extra attributes from each row in $table_rows array for example "colspan" and it worked. Thanks a lot for your help.
0

Miky's answer is step 1: you must construct your element as a DOM node.

$new_table = $dom->createElement("table");
$new_table->setAttribute("width","100%");
$new_table->setAttribute("bgcolor","#000");
foreach($table_rows as $row) $new_table->appendChild($row);
// the above assumes $table_rows is an array of TR nodes, not strings!

Step 2 is:

$table->parentNode->replaceChild($new_table,$table);

3 Comments

Can't I just append html from string instead of creating table and rows?
No. Well... I suppose you could create another document, use loadHTML, then import the root node into the first document...
Yeah it makes sense but this approach will not work in my scenario. Let me tell you, I am just reading an html email ($message) that contains a table of products. In order to sort product's order in the table I am reading table rows into an array, sorting the array and recreating the table and appending it back to $message dom object. Am I following the right track?
0

I got it working by extracting the idea from answers below and some more research.

$table = $dom->getElementsByTagName('table')->item(3);
$new_table = $dom->createDocumentFragment();
$new_table->appendXML("<table width='100%' bgcolor='#EEEEEE'>$table_rows</table>");
$table->parentNode->replaceChild($new_table,$table);

The actual problem was with my $table_rows array that each row contained some extra attributes like "colspan", $nbsp; and some additional tags.

Thank you guys for your interest and support.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.