Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP, DOM, XML questions..

John VanRyn
Guest
 
Posts: n/a
#1: Jul 17 '05
Ok, I have XML like this (XML Exhibit A) that I read in from a file
ala .. $doc->load("./Template.xml"); because I could not figure out
how to set the !DOCTYPE element.

------ XML Exhibit A-----
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE graph SYSTEM "graph.dtd">
<graph/>

--------------

I would like to change <graph/> to be like <graph start="e-2h" end="N"
width="600" height="300"/> But when I try it (using Code Exhibit A) I
get "<graph/><graph start="e-2h" end="N" wi...."

------ Code Exhibit A ------
$doc = new DomDocument('1.0','UTF-8');
$doc->load("./Template.xml");

// create root node
$graph = $doc->documentElement;

$graph->setAttribute('start', 'e-2h');
$graph->setAttribute('end', 'N');
$graph->setAttribute('width', '600');
$graph->setAttribute('height', '300');

$newNode = $doc->appendChild($graph);
---------------------
So I guess in the end I have two questions.

#1 How do you set "<!DOCTYPE graph SYSTEM "graph.dtd">"?

#2 How to you modify an existing node?

Janwillem Borleffs
Guest
 
Posts: n/a
#2: Jul 17 '05

re: PHP, DOM, XML questions..


John VanRyn wrote:[color=blue]
> #1 How do you set "<!DOCTYPE graph SYSTEM "graph.dtd">"?
>[/color]

<?php

// Create an instance of the DomImplementation class
$impl = new DomImplementation;

// Create an instance of the DomDocumentType class by
// calling the DomImplementation::createDocumentType() method
// (arguments: doctype_name, doctype_public, doctype_system)
$dtd = $impl->createDocumentType("graph", "", "graph.dtd");

// Create an instance of the DomDocument class by
// calling the DomImplementation::createDocument method
// (arguments: NS_URL, NS_PREFIX, DomDocumentType_instance)
$dom = $impl->createDocument("", "", $dtd);

// Set other properties
$dom->encoding = "UTF-8";
$dom->standalone = "no";

// Create an empty element
$element = $dom->createElement("graph");

// Append the element
$dom->appendChild($element);

// Retrieve and print the document
echo "<pre>", htmlentities($dom->saveXML()), "</pre>";

?>
[color=blue]
> #2 How to you modify an existing node?[/color]

This can be done with the replaceChild method:

// Get the node list
$nodelist = $dom->getElementsByTagName("graph");

// Get the node and copy it
$old_element = $nodelist->item(0);
$new_element = $old_element;

// Set the attribute for the new element
$new_element->setAttribute("foo","bar");

// Replace it
$dom->replaceChild($old_element, $new_element);


HTH;
JW



John VanRyn
Guest
 
Posts: n/a
#3: Jul 17 '05

re: PHP, DOM, XML questions..


"Janwillem Borleffs" <jw@jwscripts.com> wrote in message news:<414b0c0c$0$7149$d5255a0c@news.euronet.nl>...[color=blue]
> John VanRyn wrote:[color=green]
> > #1 How do you set "<!DOCTYPE graph SYSTEM "graph.dtd">"?
> >[/color]
>
> <?php
>
> // Create an instance of the DomImplementation class
> $impl = new DomImplementation;
>
> // Create an instance of the DomDocumentType class by
> // calling the DomImplementation::createDocumentType() method
> // (arguments: doctype_name, doctype_public, doctype_system)[/color]
Line 14:> $dtd = $impl->createDocumentType("graph", "", "graph.dtd");[color=blue]
>
> // Create an instance of the DomDocument class by
> // calling the DomImplementation::createDocument method
> // (arguments: NS_URL, NS_PREFIX, DomDocumentType_instance)
> $dom = $impl->createDocument("", "", $dtd);
>
> // Set other properties
> $dom->encoding = "UTF-8";
> $dom->standalone = "no";
>
> // Create an empty element
> $element = $dom->createElement("graph");
>
> // Append the element
> $dom->appendChild($element);
>
> // Retrieve and print the document
> echo "<pre>", htmlentities($dom->saveXML()), "</pre>";
>
> ?>[/color]

Warning: Couldn't fetch DOMDocumentType in
/var/www/html/TocGrapher/test7.php on line 14

Fatal error: Call to undefined method stdClass::createElement() in
/var/www/html/TocGrapher/test7.php on line 21

I tried looking up the solution, can't find it. could some one lend
some help please?
Closed Thread