weikelbob
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.
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.
ASKER
Perfect.
Now how do I edit existing song nodes?
Now how do I edit existing song nodes?
Once you have your $song_list
$song_nodes = $song_list->getElementsByT agName('so ng');
foreach ($song_nodes AS $song) {
$song->setAttribute('displ ay', 'New Display');
}
The easiest way to determine which node you're on would be to do a
$song->getAttribute('displ ay') inside of the foreach and do an "If" on it
$song_nodes = $song_list->getElementsByT
foreach ($song_nodes AS $song) {
$song->setAttribute('displ
}
The easiest way to determine which node you're on would be to do a
$song->getAttribute('displ
ASKER
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/so ng_xml); /*where song_xml is the xml you provided*/
$song_list = $xml->getElementsByTagName ('songs');
$song_node = $xml->createElement('song' );
$song_node->setAttribute(' display',$ _POST['dis play']);
$song_node->setAttribute(' url', $_POST['url']);
$song_list->appendChild($s ong_node);
}
?>
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/so
$song_list = $xml->getElementsByTagName
$song_node = $xml->createElement('song'
$song_node->setAttribute('
$song_node->setAttribute('
$song_list->appendChild($s
}
?>
Since it's a file that you're loading and not a string (I should have asked), you want to do
$xml->load('../music/songL ist.xml');
and then the rest.
$xml->load('../music/songL
and then the rest.
ASKER
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!
Thanks!
no problem. good luck!
ASKER
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/h tdocs/rod_ music2/adm in/add-mus ic-page1.p hp on line 5
Fatal error: Call to undefined method: domdocument->load() in /homepages/31/d143768754/h tdocs/rod_ music2/adm in/add-mus ic-page1.p hp on line 6
code: www.own-designs.com/rod_music2/admin/add-music-page1.txt
Warning: domdocument() expects at least 1 parameter, 0 given in /homepages/31/d143768754/h
Fatal error: Call to undefined method: domdocument->load() in /homepages/31/d143768754/h
code: www.own-designs.com/rod_music2/admin/add-music-page1.txt
ahh... try
$xml = new DOMDocument();
add the parenthesis
$xml = new DOMDocument();
add the parenthesis
ASKER
Made the change, still getting these errors:
Warning: domdocument() expects at least 1 parameter, 0 given in /homepages/31/d143768754/h tdocs/rod_ music2/adm in/add-mus ic-page1.p hp on line 5
Fatal error: Call to undefined method: domdocument->load() in /homepages/31/d143768754/h tdocs/rod_ music2/adm in/add-mus ic-page1.p hp on line 6
Warning: domdocument() expects at least 1 parameter, 0 given in /homepages/31/d143768754/h
Fatal error: Call to undefined method: domdocument->load() in /homepages/31/d143768754/h
Hmm,
Make sure that you have the DOM extension installed for PHP. That's the only guess I have.
Make sure that you have the DOM extension installed for PHP. That's the only guess I have.
ASKER
I'm on a shared hosting package.
I'll run phpinfo
I'll run phpinfo
ASKER
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('song s');
$song_node = $xml->create_element('song ');
$song_node->set_attribute( 'display', $_POST['di splay']);
$song_node->set_attribute( 'url', $_POST['url']);
$song_list->append_child($ song_node) ;
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
$song_list = $xml->get_elements_by_tag_
$song_node = $xml->create_element('song
$song_node->set_attribute(
$song_node->set_attribute(
$song_list->append_child($
ASKER
Now I'm getting this error:
Fatal error: Call to undefined method: domdocument->get_elements_ by_tag_nam e() in /homepages/31/d143768754/h tdocs/rod_ music2/adm in/add-mus ic-page1.p hp on line 6
Fatal error: Call to undefined method: domdocument->get_elements_
ASKER
I'm going a different route.
I'm using:
<?
$doc = new_xmldoc('1.0');
$root = $doc->add_root('songs');
$member = $root->new_child('song','d isplay');
$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!
I'm using:
<?
$doc = new_xmldoc('1.0');
$root = $doc->add_root('songs');
$member = $root->new_child('song','d
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Got it.
That works.
Let me apply this and return.
That works.
Let me apply this and return.
ASKER
Works great!
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
$song_node = $xml->createElement('song'
$song_node->setAttribute('
$song_node->setAttribute('
$song_list->appendChild($s
?>