By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,722 Members | 1,227 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,722 IT Pros & Developers. It's quick & easy.

PHP, DOM, XML questions..

P: n/a
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?
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
John VanRyn wrote:
#1 How do you set "<!DOCTYPE graph SYSTEM "graph.dtd">"?

<?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>";

?>
#2 How to you modify an existing node?


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

Jul 17 '05 #2

P: n/a
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message news:<41**********************@news.euronet.nl>...
John VanRyn wrote:
#1 How do you set "<!DOCTYPE graph SYSTEM "graph.dtd">"?

<?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)

Line 14:> $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>";

?>


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?
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.