Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DOM/XML: append_child() expects parameter 1 to be object 1

Status
Not open for further replies.

artguy

Technical User
Feb 2, 2001
117
US
I'm afraid I can't find an answer to this. I'm just trying this simple PHP DOM/XML example to understand how it should work since I'm new to this.

My error is:

Warning: domnode::append_child() expects parameter 1 to be object, null given in line 6

which is this line:
Code:
$xmlDocument->append_child($documentElement);

Here is the full code:
Code:
<?php
ini_set('display_errors', 1);

$xmlDocument = domxml_new_doc('1.0');
$documentElement = $xmlDocument->create_element('forest');
$xmlDocument->append_child($documentElement);
$tree = $xmlDocument->create_element('tree');
$name = $xmlDocument->create_element('name');
$name->append_child($xmlDocument->create_text_node('Oak'));
$tree->append_child($name);
$documentElement->append_child($tree);
$xmlDocument->dump_file('test2005.xml', FALSE, TRUE);
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test 2</title>
</head>
<body>
<p>
<a href="test2005.xml">test XML file</a>
</p> 
</body>
</html>
 
It sounds to me like this line:

$documentElement = $xmlDocument->create_element('forest');

isn't returning what you expect.

What do you get when you replace:

$documentElement = $xmlDocument->create_element('forest');

with:

$documentElement = $xmlDocument->create_element('forest');
print_r ($documentElement);

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
I get nothing in return.

Doing the same thing for the $xmlDocument I get:

domdocument Object
(
[name] => #document
=> [version] => 1.0 [stan...apparently $documentElement is not an object.
 
PHP Version 5.0.5

System: Windows NT NR2 5.2 build 3790
Build Date: Sep 5 2005 15:50:13

Any other info that would be helpful?
 
Okay, I'm a little confused.

I'm not at all sure about this, but I think you're using an incompatible library. At least, on my Linux box compiling from source, I can't even get domxml_new_doc() to work.

Anyway, take a look at That part of the manual says in part:

The DOM extension is the replacement for the DOM XML extension from PHP 4. The extension still contains many old functions, but they should no longer be used. In particular, functions that are not object-oriented should be avoided.

The extension allows you to operate on an XML document with the DOM API.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Does this script:

Code:
<?php
$xmlDocument = new DOMDocument('1.0', 'iso-8859-1');
$documentElement = $xmlDocument->createElement('forest');
$xmlDocument->appendChild($documentElement);
$tree = $xmlDocument->createElement('tree');
$name = $xmlDocument->createElement('name');
$name->appendChild($xmlDocument->createTextNode('Oak'));
$tree->appendChild($name);
$documentElement->appendChild($tree);
$xmlDocument->save('test2005.xml');
print $xmlDocument->saveHtml();
?>

Work for you?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That gives me:

Warning: domdocument::domdocument() expects parameter 2 to be long, string given on line 2

Fatal error: Call to undefined method domdocument::createElement() on line 3

So, I've just been trying to do something with outdated methods this whole time, haven't I? That's what you get when you try to learn stuff yourself. I apologize if I've had you running in the wrong direction to figure out my problem.

Basically, all I'm trying to do is learn to interact with XML files in as simple way as possible so I can apply it to pages I'm developing. I've done it before with ASP without an issue and now I'm just trying to figure out the best way to use XML with PHP to things such as creating an XML file, populating the file using a standard HTML form, and recall info from existing ones.

What should I be looking at on the net to learn to do such a thing so I don't run into deprecated stuff again?

Again, I am sincerely sorry for troubling you with this problem if it was just the fact I was using old methods.
 
I dunno. I'm probably not the best person to ask about all the nitty-gritty specifics of handling XML with PHP. You see, I'm not a big fan of XML.

<soapbox>
I mean, XML is all right when the data your code is going to receive is going to have an unknown format. The metadata built into XML gives a programmer the ability to handle strangeness fairly readily.

The problem is, XML is overused. I've seen two programmers debate for a week over the exact format of the data that will be transmitted between their repective pieces of code. And then wrap all that excruciatingly-carefully formatted data in XML, which increased the overall data transmission load by 40%. If the data format is static, of what use is XML to the transmission process?
</soapbox>



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
That's alright. Again, very sorry for troubling you about this.

The problem is the person I'm trying to help doesn't have any form of database in their hosting package and one of the stipulations was to use PHP for the code. So I turned to XML for the only thing I knew to accomplish the task.

Now, I've decided to try to talk them into using ASP (am I allowed to say that in this forum?) since they are on a Windows server and I already wrote some code that I can use pieces from to create the little app using XML files.

Next time, I'm just making them go to a Linux server which has a MySQL database included and be done with it.

Thanks again for all your time. Keep up the good work!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top