Page 1 of 1
PHP Domdocument
Posted: Mon Jun 20, 2005 3:26 am
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
Posted: Mon Jun 20, 2005 4:05 am
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.
Thanks a lot but ...
Posted: Mon Jun 20, 2005 4:39 am
by namitjung
Thanks a lot.But how can i save the output in a xml file.
Posted: Mon Jun 20, 2005 4:59 am
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.
Posted: Mon Jun 20, 2005 5:15 am
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
Posted: Mon Jun 20, 2005 5:19 am
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.
Oops!sorry
Posted: Mon Jun 20, 2005 5:33 am
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
Posted: Mon Jun 20, 2005 5:39 am
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.
Didn't work
Posted: Mon Jun 20, 2005 5:48 am
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
Posted: Mon Jun 20, 2005 5:51 am
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.
My Problem is not solved.
Posted: Mon Jun 20, 2005 5:57 am
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
Posted: Mon Jun 20, 2005 6:00 am
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.
Didn't work yet
Posted: Mon Jun 20, 2005 6:04 am
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
Posted: Mon Jun 20, 2005 6:08 am
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.