Link to home
Start Free TrialLog in
Avatar of weikelbob
weikelbobFlag for United States of America

asked on

simple inserting of XML line using PHP

I want to insert a line into an xml file with PHP.

For example, I want to add
<song display="Third Song" url="3.mp3" />
after the first 2 entries of:

<?xml version="1.0" ?>
      <songs>
            <song display="First Song" url="1.mp3" />
            <song display="Second Song" url="2.mp3" />
    </songs>

I also need to edit the existing song entries without saving them to a MySQL database.
Avatar of mpginsburg
mpginsburg

Easiest way to do that would be to use PHP's DOM support
http://us2.php.net/manual/en/ref.dom.php

<?php
$xml = new DOMDocument;
$xml->loadXML($song_xml); /*where song_xml is the xml you provided*/

$song_list = $xml->getElementsByTagName('songs');
$song_node = $xml->createElement('song');
$song_node->setAttribute('display','Third Song');
$song_node->setAttribute('url', '3.mp3');

$song_list->appendChild($song_node);

?>
Avatar of weikelbob

ASKER

Perfect.

Now how do I edit existing song nodes?

Once you have your $song_list

$song_nodes = $song_list->getElementsByTagName('song');
foreach ($song_nodes AS $song) {
  $song->setAttribute('display', 'New Display');
}

The easiest way to determine which node you're on would be to do a
$song->getAttribute('display')  inside of the foreach and do an "If" on it
I'm applying your first comment. The xml file is at ../music/songList.xml

What am I doing wrong (I'm inserting a new node):

 <?php
include('includeme.php');
if(isset($_POST['submit']))
{
$xml = new DOMDocument;
$xml->loadXML($../music/song_xml); /*where song_xml is the xml you provided*/
$song_list = $xml->getElementsByTagName('songs');
$song_node = $xml->createElement('song');
$song_node->setAttribute('display',$_POST['display']);
$song_node->setAttribute('url', $_POST['url']);
$song_list->appendChild($song_node);
}
?>
Since it's a file that you're loading and not a string (I should have asked), you want to do

$xml->load('../music/songList.xml');

and then the rest.
Great. I'm new at XML and not that great with php, so I will apply your suggestions and get back to you if I run into trouble.

Thanks!
no problem.  good luck!
OK. I know it's something I'm overlooking, but I'm getting this:

Warning: domdocument() expects at least 1 parameter, 0 given in /homepages/31/d143768754/htdocs/rod_music2/admin/add-music-page1.php on line 5

Fatal error: Call to undefined method: domdocument->load() in /homepages/31/d143768754/htdocs/rod_music2/admin/add-music-page1.php on line 6

code: www.own-designs.com/rod_music2/admin/add-music-page1.txt   
ahh... try

$xml = new DOMDocument();

add the parenthesis
Made the change, still getting these errors:


Warning: domdocument() expects at least 1 parameter, 0 given in /homepages/31/d143768754/htdocs/rod_music2/admin/add-music-page1.php on line 5

Fatal error: Call to undefined method: domdocument->load() in /homepages/31/d143768754/htdocs/rod_music2/admin/add-music-page1.php on line 6
Hmm,
  Make sure that you have the DOM extension installed for PHP.  That's the only guess I have.
I'm on a shared hosting package.

I'll run phpinfo
Looks OK:

http://own-designs.com/rod_music2/phpinfo1.php

perhaps we should use a MySQL database
Oh php4... that might be causing it as well.

you'll have to use DOMXML instead of just DOM
http://us.php.net/manual/en/ref.domxml.php

TRY:

$xml = domxml_open_file("../music/songList.xml");
$song_list = $xml->get_elements_by_tag_name('songs');
$song_node = $xml->create_element('song');
$song_node->set_attribute('display',$_POST['display']);
$song_node->set_attribute('url', $_POST['url']);
$song_list->append_child($song_node);
Now I'm getting this error:

Fatal error: Call to undefined method: domdocument->get_elements_by_tag_name() in /homepages/31/d143768754/htdocs/rod_music2/admin/add-music-page1.php on line 6
I'm going a different route.
I'm using:

<?
$doc = new_xmldoc('1.0');
$root = $doc->add_root('songs');
$member = $root->new_child('song','display');
$fp = @fopen('members.xml','w');
if(!$fp) {
    die('Error cannot create XML file');
}
fwrite($fp,$doc->dumpmem());
fclose($fp);
?>

which works except for my <song> tags need to read

<song display="string1" url="string2" />

I think I can do it all if you help me with this

Thanks!
ASKER CERTIFIED SOLUTION
Avatar of mpginsburg
mpginsburg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Got it.
That works.

Let me apply this and return.
Works great!