PHP Domdocument

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
namitjung
Forum Commoner
Posts: 42
Joined: Mon Jun 20, 2005 3:17 am

PHP Domdocument

Post by namitjung »

Hi all

I am using php 4.3.11 and try to execute following code.But gives me the error.This code works fine on php 5.0.4 but i have to use it on php 4.3.11 because my web server doesn't support php 5.0.4. Please help me out I got dom enabled on php 4.3.11.

And here is the code.

Code: Select all

$doc=new DomDocument;
$doc->save("test.xml");
Gives me following error:

Warning: domdocument() expects at least 1 parameter, 0 given in c:\inetpub\wwwroot\test.php

on line 2

Code: Select all

$doc=new DomDocument("1.0");
$doc->save("test.xml");
Gives me following error

Warning: domdocument(): Start tag expected, '<' not found in c:\inetpub\wwwroot\test.php on

line 2
Last edited by namitjung on Mon Jun 20, 2005 5:24 am, edited 2 times in total.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Hi, namitjung,

welcome to the board. Three things:

1. The appropriate forum for your question is PHP Code
2. Use [ php ]-tags around your code
3. Read up on DOM XML functions, especially the user-notes in the manual tend to have very useful examples.

XML management has been completely overhauled for PHP5, if you want to create a new XML document in PHP4x, use

Code: Select all

$this->dom				=	domxml_new_xmldoc($doctype);
$this->xml				=	$this->dom->append_child($this->dom->create_element($root_node));
in a class.
namitjung
Forum Commoner
Posts: 42
Joined: Mon Jun 20, 2005 3:17 am

Thanks a lot but ...

Post by namitjung »

Thanks a lot.But how can i save the output in a xml file.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Code: Select all

$xml_document	=	$xml->dom->dump_mem(true);
P.S.: Could you edit your first post and put

Code: Select all

tags around the code? Thanks.

P.P.S.: if you cross-post again, we will be having a problem.
namitjung
Forum Commoner
Posts: 42
Joined: Mon Jun 20, 2005 3:17 am

Post by namitjung »

I used Following Code but it gave me the error.

Code: Select all

$doctype="1.0";
$root_node="product";
$this->dom=domxml_new_xmldoc($doctype);
$this->xml=$this->dom->append_child($this->dom->create_element($root_node));
$xml_document=$xml->dom->dump_mem(true);
Fatal error: Call to a member function on a non-object in c:\inetpub\wwwroot\test.php on line 6

Please help me to solve this problem
Last edited by namitjung on Mon Jun 20, 2005 5:26 am, edited 1 time in total.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I won't say it again: use

Code: Select all

tags around your code. Edit your posts, please.

What's wrong with your code? It's actually very straighforward: you are trying to run before you can walk. If you are not familiar with classes and/or variables, don't use them or read up on them. Google $this and PHP or, even better, read up on it in the manual.
namitjung
Forum Commoner
Posts: 42
Joined: Mon Jun 20, 2005 3:17 am

Oops!sorry

Post by namitjung »

I m really sorry that i didn't review the rule for posting the code first.now i edited my post. I want to save the output into xml file please post the code to save file if possible.I read the entire manual but didn't find to save the file.What i find is save method of domdocument. As i alread said while creating instance of DomDocument it gives me the error. Please help me out.

Thanks A lot
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Code: Select all

$doctype="1.0";
$root_node="product";
$dom=domxml_new_xmldoc($doctype);
$xml=$dom->append_child($dom->create_element($root_node));
$xml_document=$dom->dump_mem(true);
The difference is: the code I coded previously presupposed that you were using a class. The code above doesn't.

I'd advise strongly to read up on classes and PHP's object orientation. It's a very important development technique.

P.S.: Thanks for editing your posts. You will need to open the [ php ] tag and close it later at the end of the code with [ /php ] (without the spaces between the square brackets) to have it all displayed as PHP-code. I re-edited your posts so that it shows correctly.
namitjung
Forum Commoner
Posts: 42
Joined: Mon Jun 20, 2005 3:17 am

Didn't work

Post by namitjung »

I guess i m posting the code in well format. please assume that there is question mark tag at the begining and at the end of this code.that's why error is showind at line 5( while appending child)

The code didn't work.Even this works how can i save the file named "test.xml".Please put a demo code for that

Code: Select all

$doctype="1.0";
$root_node="product";
$dom=domxml_new_xmldoc($doctype);
$xml=$dom->append_child($this->dom->create_element($root_node));
$xml_document=$dom->dump_mem(true);
gives me the following error.

Fatal error: Call to a member function on a non-object in c:\inetpub\wwwroot\test.php on line 5

Thanks a lot
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

use the code from my post above. And make yourself familiar with what "$this" means and in what context it is being used.
namitjung
Forum Commoner
Posts: 42
Joined: Mon Jun 20, 2005 3:17 am

My Problem is not solved.

Post by namitjung »

Dear patrikG

Thank you very much for your help.I tried all code but couldn't solve my problem. I don't know when $this will used and how can i use it. I just needed a 5-7 lines of code to save an xml file. Please provide me the code which works fine on my computer if possible. Since i am new to php and xml i got frustated with this php and xml.

Thanks a lot
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Have you tried the code I have posted above? The code you have posted last is not what I posted there.

Try it out.
namitjung
Forum Commoner
Posts: 42
Joined: Mon Jun 20, 2005 3:17 am

Didn't work yet

Post by namitjung »

Tried following code

Code: Select all

$doctype="1.0";$root_node="product";$dom=domxml_new_xmldoc($doctype);$xml=$dom->append_child

($this->dom->create_element($root_node));$xml_document=$dom->dump_mem(true);
Gives the following error

Fatal error: Call to a member function on a non-object in c:\inetpub\wwwroot\test.php on line 3
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

I am getting slightly frustrated in my efforts to teach you to use

Code: Select all

tags around your code.

it works as described earlier: you open them with [php] and close them after the code with [/php]

Apart from that let me repeat myself: copy the code from the my post above. IF you think you've done it, copy and paste it again regardless.
Post Reply