PHP, DOM, XML questions..

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John VanRyn

    PHP, DOM, XML questions..

    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->documentElemen t;

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

    $newNode = $doc->appendChild($g raph);
    ---------------------
    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

    #2
    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 DomImplementati on class
    $impl = new DomImplementati on;

    // Create an instance of the DomDocumentType class by
    // calling the DomImplementati on::createDocum entType() method
    // (arguments: doctype_name, doctype_public, doctype_system)
    $dtd = $impl->createDocument Type("graph", "", "graph.dtd" );

    // Create an instance of the DomDocument class by
    // calling the DomImplementati on::createDocum ent 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($e lement);

    // Retrieve and print the document
    echo "<pre>", htmlentities($d om->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->getElementsByT agName("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



    Comment

    • John VanRyn

      #3
      Re: PHP, DOM, XML questions..

      "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message news:<414b0c0c$ 0$7149$d5255a0c @news.euronet.n l>...[color=blue]
      > John VanRyn wrote:[color=green]
      > > #1 How do you set "<!DOCTYPE graph SYSTEM "graph.dtd" >"?
      > >[/color]
      >
      > <?php
      >
      > // Create an instance of the DomImplementati on class
      > $impl = new DomImplementati on;
      >
      > // Create an instance of the DomDocumentType class by
      > // calling the DomImplementati on::createDocum entType() method
      > // (arguments: doctype_name, doctype_public, doctype_system)[/color]
      Line 14:> $dtd = $impl->createDocument Type("graph", "", "graph.dtd" );[color=blue]
      >
      > // Create an instance of the DomDocument class by
      > // calling the DomImplementati on::createDocum ent 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($e lement);
      >
      > // Retrieve and print the document
      > echo "<pre>", htmlentities($d om->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::creat eElement() 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?

      Comment

      Working...