XML -- Parsing / Updating / Adding to

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

XML -- Parsing / Updating / Adding to

Post by Jonah Bron »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I scanned the google results for XML DOM, and found a couple of tutorials.  One on [url]http://www.ibm.com/developerworks/library//x-xmlphp1.html[/url], and another from [url]http://devzone.zend.com/article/2387-XML-and-PHP-5[/url].  Using a little bit of both, I pieced together what I think should work to create a new XML file:

Code: Select all

<?php
//New XML
$xml = new DOMDocument('1.0');

//specify the root element
$root = $xml->append_child($xml->create_element('users'));

//Add new user
$user = $root->append_child($xml->create_element('user'));

//Add the email
$email = $user->append_child($xml->create_element('email'));

//Add the contents of $email
$email->append_child($xml->create_text_node('jonah@nucleussystems.com'));

//format xml
$root->format_output = true;

//Save xml
$root->save('file.xml');
?>
And this is the error I get when it is uploaded and tested:

Code: Select all

Warning: domdocument(): Start tag expected, '<' not found in /home/content/g/e/i/geinternet/html/nucleussystems/XML/index.php on line 3

Warning: create_element(): Underlying object missing in /home/content/g/e/i/geinternet/html/nucleussystems/XML/index.php on line 6

Warning: create_element(): Cannot fetch DOM object in /home/content/g/e/i/geinternet/html/nucleussystems/XML/index.php on line 6

Fatal error: Call to a member function on a non-object in /home/content/g/e/i/geinternet/html/nucleussystems/XML/index.php on line 9
Why does this error occur?
Any help much appreciated


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]1.[/b] Select the correct board for your query. Take some time to read the guidelines in the sticky topic.[/quote]
rturner
Forum Newbie
Posts: 24
Joined: Sun Nov 04, 2007 1:39 pm

try this

Post by rturner »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

<?php
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;

$root = $doc->createElement("users");
$root = $doc->appendChild($root);

$node = $doc->createElement('user');
$node = $root->appendChild($node);

$text = $doc->createTextNode('Joe Blow');
$text = $node->appendChild($text);

$xml = $doc->saveXML();
echo $xml;

$doc->save("file.xml");

?>
assuming you have php 5 or greater


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Great! :D Thanks, rturner! It works now. Aren't forums great?

Thanks again :wink:
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Another question:
I tried, but couldn't figure out how to access the already-existing root, and an already existing child? (and it's content, and attributes)

thanks:

Code: Select all

<?php
$xml = new DOMDocument('1.0');
$xml->load('file.xml');

$xml->formatOutput = true;

$root = $xml->documentElement;

$node = $xml->createElement('user');
$node = $root->appendChild($node);

$text = $xml->createTextNode('John Johnson');
$text = $node->appendChild($text);

$xml->save("file.xml");

$xml = $xml->saveXML();
echo $xml;
?>
rturner
Forum Newbie
Posts: 24
Joined: Sun Nov 04, 2007 1:39 pm

Post by rturner »

My apology to feyd. I read the rules and will use long start tags for php and php/code tags as appropriate...

Code: Select all

<?php
$xml = new DOMDocument('1.0');
$xml->load('file.xml');

$xml->formatOutput = true;

$root = $xml->documentElement;
$node = $root->firstChild;

while ($node) {
        if (($node->nodeType == XML_ELEMENT_NODE) && ($node->nodeName == 'user')) {
                if ($node->firstChild->nodeValue == "John Johnson") {
                        $node->firstChild->replaceData(2,1,'');
                }
        print "$node->nodeValue;";
        }
        $node = $node->nextSibling;
}

$xml->save("file.xml");
$xml = $xml->saveXML();
echo $xml;
?>
You can delete the node and add a new one or do something like the above to change the text.
This makes John Jon. (removes the h)

To modify attributes use setAttribute

http://phpbuilder.com/manual/en/functio ... ribute.php

Good luck!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Great. Thanks :lol: I'll go through that, and test different actions.

Thanks again, again, and again, rturner :)
rturner
Forum Newbie
Posts: 24
Joined: Sun Nov 04, 2007 1:39 pm

Post by rturner »

No problem!
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Hey,

One more question: (and hes, this is the last question)
I'm trying to use PHP to read a manually made index for my site, and gives an output if the description, title, or keywords include the searched string.

This is the error:

Code: Select all

Warning: domdocument(): Start tag expected, '<' not found in /home/content/g/e/i/geinternet/html/nucleussystems/search/index.php on line 2

Fatal error: Call to undefined function: load() in /home/content/g/e/i/geinternet/html/nucleussystems/search/index.php on line 3
this is the PHP:

Code: Select all

<?php

$xml = new DOMDocument('1.0');//New Document
$xml->load('search.xml');//Load "search.xml"
$xml->formatOutput = true;//format output

//Set tree
$root = $xml->documentElement;//root
$page = $root->firstChild;//each page
$doc = $page->firstChild;//each property of each page

while ($page){//while there is a node to examin
  if ($page->nodeType==XML_ELEMENT_NODE && $page->nodeName=='page'){//if nodetype is correct, and nodename is "page"
  //if any of the properties include the search string,
    if (($page->firstChild->nodeValue.eregi($search)) || $page->firstChild->nextSibling->nodeValue.eregi($search)) || ($page->firstChild->nextSibling->nextSibling->nodeValue.eregi($search))){
      while ($doc){//loop through properties
        if ($doc->nodeType==XML_ELEMENT_NODE){//if nodetype is correct
          if ($doc->nodeName=='name'){//if name is "name"
            //output title, as a link to the "address" property
            echo '<h3><a href="'.$doc->nextSibling->nextSibling->nextSibling->firstChild->nodeValue.'"">'.$doc->firstChild->nodeValue.'</a></h3>';
          }elseif ($doc->nodeName=='desc'){//if name is "desc"
            //output description
            echo '<blockquote>'.$doc->firstChild->nodeValue.'</blockquote><br />';
          }elseif ($page->nodeName=='address'){//if name is "address"
            //output address, as a link
            echo '<a href="'.$doc->firstChild->nodeValue.'">'.$doc->firstChild->nodeValue.'</a>';
          }
        }
        //goto next sibling
        $doc = $doc->nextSibling;
      }
    }
  }
  //goto next sibling
  $page = $page->nextSibling;
}
//close xml doc
$xml->saveXML();
?>
and this is the XML doc (search.xml):

Code: Select all

<?xml version="1.0" ?>
<index>
  <page>
    <name>jBlog home page</name>
    <desc>Learn XML HTML PHP</desc>
    <keys>Learn XML PHP HTML CSS JS web design</keys>
    <address>http://nucleussystems.com/blog</addresss>
  </page>
</index>
Post Reply