We help IT Professionals succeed at work.

simple inserting of XML line using PHP

weikelbob
weikelbob asked
on
382 Views
Last Modified: 2008-04-24
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.
Comment
Watch Question

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);

?>

Author

Commented:
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

Author

Commented:
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.

Author

Commented:
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!

Author

Commented:
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

Author

Commented:
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.

Author

Commented:
I'm on a shared hosting package.

I'll run phpinfo

Author

Commented:
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);

Author

Commented:
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

Author

Commented:
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!
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Got it.
That works.

Let me apply this and return.

Author

Commented:
Works great!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.