Jump to content

writing to xml


corillo181

Recommended Posts

hey i want to know wha tis the best way to write and add to a xml file?

 

if i have a xml page with

 

<music>

 

  <artistID>#</artistID>

  <artistName>name</artistName>

    <pictures>

      <picture>url</picture>

      <picture>url</picture>

      <picture>url</picture>

    </pictures>

 

</music>

 

gaol

1. to add new artist

 

Link to comment
https://forums.phpfreaks.com/topic/66555-writing-to-xml/
Share on other sites

Is this a new xml file that you are creating?

 

I think that you could use XML with the DOM...  But I do not know to much about this yet.

 

I have been working on this a little

 

  <?php
  $books = array();
  $books [] = array(
  'title' => 'PHP Hacks',
  'author' => 'Jack Herrington',
  'publisher' => "O'Reilly"
  );
  $books [] = array(
  'title' => 'Podcasting Hacks',
  'author' => 'Jack Herrington',
  'publisher' => "O'Reilly"
  );
  
  $doc = new DOMDocument();
  $doc->formatOutput = true;
  
  $r = $doc->createElement( "books" );
  $doc->appendChild( $r );
  
  foreach( $books as $book )
  {
  $b = $doc->createElement( "book" );
  
  $author = $doc->createElement( "author" );
  $author->appendChild(
  $doc->createTextNode( $book['author'] )
  );
  $b->appendChild( $author );
  
  $title = $doc->createElement( "title" );
  $title->appendChild(
  $doc->createTextNode( $book['title'] )
  );
  $b->appendChild( $title );
  
  $publisher = $doc->createElement( "publisher" );
  $publisher->appendChild(
  $doc->createTextNode( $book['publisher'] )
  );
  $b->appendChild( $publisher );
  
  $r->appendChild( $b );
  }
  
  echo $doc->saveXML();
  ?>

Link to comment
https://forums.phpfreaks.com/topic/66555-writing-to-xml/#findComment-333406
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.