editing a XML file

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
reeseslover531
Forum Newbie
Posts: 5
Joined: Sun May 14, 2006 9:50 am

editing a XML file

Post by reeseslover531 »

Hello, I am trying to edit an xml document inside of PHP5.
Here is the xml doc

Code: Select all

<?xml version="1.0"?>
<Start>
   <item name="Test1">
      <category>Game</category>
      <serial>432-543-345</serial>
   </item>
   <item name="Test2">
      <category>Other</category>
      <serial>432-543-654</serial>
   </item>
</Start>
What I am trying to do is allow someone to use a form to edit that xml file
so I want when somebody put Test3 in the name, Other in the category and 1234 in the serial, the PHP would write to the XML doc and the XMl doc would turn into this

Code: Select all

<?xml version="1.0"?>
<Start>
   <item name="Test1">
      <category>Game</category>
      <serial>432-543-345</serial>
   </item>
   <item name="Test2">
      <category>Other</category>
      <serial>432-543-654</serial>
   </item>
<item name="Test3">
      <category>Other</category>
      <serial>1234</serial>
   </item>
</Start>
See how another item is added, but inside the Start root element. How would I do that. I know how to add tuff to the end of the XMl, so outside of the Start element, but that is not what I want to do?
I appreciate any help!

thanks,
Joe
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

You could open the file get its contents using fopen () or even file_get_contents(), use a str_replace to find the </start> in the contents variable and replace it with your extra tags and the add </start> on at the end again...

Code: Select all

$contents = file_get_contents("example.xml");  // Get current XML file
$new_tags = "   <item name=\"" . $_POST['item'] . "\">
      <category>" . $_POST['catergory'] . "</category>
      <serial>" . $_POST['serial'] . "</serial>
   </item>\n</start>";                               // Create new tags including end start tag
str_replace ("</start>", $new_tags, $contents);  // Replace end start tag with new tags

$fp = fopen("example.xml", 'w'); // Open file for writing
fwrite ($fp, $contents);               // Write new XML
fclose($fp);                                  // Close
That should work, untested though so don't use it on a live file.
reeseslover531
Forum Newbie
Posts: 5
Joined: Sun May 14, 2006 9:50 am

Post by reeseslover531 »

I was thinking of something like that, but it wouldn't be the fastest way. Once the xml file got larger, it would get slower. I am probably going to end up with that, and thank you very much, but does anyone else have an idea, maybe using like the DOM system?
alex-weej
Forum Newbie
Posts: 19
Joined: Sun May 14, 2006 11:20 am

Post by alex-weej »

Holy crap. Don't do that.

Code: Select all

$doc = DOMDocument::load('yourfile.xml');
$root = $doc->documentElement;
$item = $doc->createElement('item');
$item->setAttribute('name', 'Test3');
$item->appendChild($doc->createElement('category', 'Other'));
$item->appendChild($doc->createElement('serial', '1234'));
$root->appendChild($item);
$doc->save('yourfile.xml');
Read http://www.php.net/DOM for more info on XML Document Object Model programming.
User avatar
$phpNut
Forum Commoner
Posts: 40
Joined: Tue May 09, 2006 5:13 pm

Post by $phpNut »

Ah, didn't know about that way, or the XML DOM thingy.
alex-weej
Forum Newbie
Posts: 19
Joined: Sun May 14, 2006 11:20 am

Post by alex-weej »

It's OK, learn something new every day. :)
reeseslover531
Forum Newbie
Posts: 5
Joined: Sun May 14, 2006 9:50 am

Post by reeseslover531 »

Thank you all, I was fidlling with the DOM stuff, but couldn't get it to work, and you helped thanks!!
reeseslover531
Forum Newbie
Posts: 5
Joined: Sun May 14, 2006 9:50 am

Post by reeseslover531 »

ok so I got that working now, if I wanted to edit something. How would you edit or dare I say it, delete an item. I am guess the editing has to do with replaceNode(). How would I get the oldnode value though? I am not sure, and I this probably is a very newb question but I am asking it anyways. Thanks!
reeseslover531
Forum Newbie
Posts: 5
Joined: Sun May 14, 2006 9:50 am

Post by reeseslover531 »

can anyone help me?
alex-weej
Forum Newbie
Posts: 19
Joined: Sun May 14, 2006 11:20 am

Post by alex-weej »

Many ways to do it, I'd suggest using the DOM as a storage structure and manipulating arrays of objects instead.

Assuming PHP 5, 4 won't be much different.

Code: Select all

<?php

class Item
{
    public $category;
    public $serial;

    public function __construct($category, $serial)
    {
        $this->category = $category;
        $this->serial = $serial;
     }
}

$document = DOMDocument::load('yourfile.xml');
$xpath = new DOMXPath($document);
$items_nodelist = $xpath->query('/Start/item');

$items = array();
foreach ($items_nodelist as $element) {
    $item_name = $element->getAttribute('name');
    $item_category = $element->getElementsByTagName('category')->item(0)->nodeValue;
    $item_serial = $element->getElementsByTagName('serial')->item(0)->nodeValue;
    $items[$item_name] = new Item($item_category, $item_serial);
}


// Delete an item
unset($items['Test1']);

// Change an item
$items['Test2']->category = 'Video';


// Build our new document
$document = DOMImplementation::createDocument(null, 'Start');
$root = $document->documentElement;

foreach ($items as $item_name => $item) {
    $item_element = $document->createElement('item');
    $item_element->setAttribute('name', $item_name);
    $item_element->appendChild($document->createElement('category', $item->category));
    $item_element->appendChild($document->createElement('serial', $item->serial));
    $root->appendChild($item_element);
}

echo $document->saveXML();

// To save, uncomment
//$document->save('yourfile.xml');

?>
See if you can learn some new things from that. :) Feel free to ask about anything you can't figure out.
Post Reply