Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 20th, 2005, 03:42 AM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to .... Dom Document

Hi All


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.When i use phpinfo() it shows that dom xml is enabled on php

And here is the code.

<?php
$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

<?php
$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

Please help me solve this problem

 
Old June 29th, 2005, 07:34 PM
Authorized User
 
Join Date: Dec 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Do you have a reason not to use domxml_new_doc() instead? The following works on PHP 4.3.0:

$doc = domxml_new_doc("1.0");
$root = $doc->create_element("HTML");
$root = $doc->append_child($root);
$head = $doc->create_element("HEAD");
$head = $root->append_child($head);
$title = $doc->create_element("TITLE");
$title = $head->append_child($title);
$text = $doc->create_text_node("This is the title");
$text = $title->append_child($text);
echo "<PRE>";
echo htmlentities($doc->dump_mem(true));
echo "</PRE>";

It produces:

<?xml version="1.0"?>
<HTML>
  <HEAD>
    <TITLE>This is the title</TITLE>
  </HEAD>
</HTML>



Philibuster
 
Old June 30th, 2005, 11:28 PM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply. It worked fine.But it produced out put to browser only. I need to save that content in an xml file.

 Likewise if we edited existing xml file using xml dom it should edit that file and save it.

How can i do this

Thank You

 
Old July 2nd, 2005, 05:53 PM
Authorized User
 
Join Date: Dec 2004
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To save to a file, simply use the string buffer produced by $doc->dump_mem(true) in the example to your file using fopen/fwrite/fclose.

Here is an example with this change:

$doc = domxml_new_doc("1.0");
$root = $doc->create_element("HTML");
$root = $doc->append_child($root);
$head = $doc->create_element("HEAD");
$head = $root->append_child($head);
$title = $doc->create_element("TITLE");
$title = $head->append_child($title);
$text = $doc->create_text_node("This is the title");
$text = $title->append_child($text);
$xml_data = $doc->dump_mem(true);

$filename = 'test.xml'; //change this to the filepath/name you like

// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {

    if (!$handle = fopen($filename, 'wb')) {
         echo "Cannot open file ($filename)";
         exit;
    }

    // Write $xml_data to our opened file.
    if (fwrite($handle, $xml_data) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }

    echo "Success, wrote ($somecontent) to file ($filename)";

    fclose($handle);

} else {
    echo "The file $filename is not writable";

A more complete solution to your problem would involve more extensive PHP code. Several complete implementations can be found on other PHP sites (for example, take a look at www.phpclasses.org).








Philibuster
 
Old July 7th, 2005, 12:17 AM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thankx for your reply.

Here we can save the file.But what if we have to edit the file. I mean an xml file looks like this
<proudctdetails>
 <product>
  <name>Ball</name>
  <price>$4.5</price>
 </product>
 <product>
  <name>Doll</name>
  <price>$44.5</price>
 </product>
</productdetails>

Now i want to add a product let's say Teddy bear in this xml file.How can i do this?

Thank You






Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to Click Element in DOM document [email protected] C# 2005 1 October 18th, 2007 03:38 AM
How to Click Element in DOM document [email protected] C# 1 October 18th, 2007 03:03 AM
Click event in DOM document [email protected] C# 2005 0 October 18th, 2007 12:43 AM
DOM Perseus Java Basics 3 May 4th, 2006 08:50 AM
XPath API and DOM Document object nnravi XML 10 May 2nd, 2006 12:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.