XML once again

  • classified
  • Expert
  • Expert
  • User avatar
  • Posts: 540
  • Loc: Bahrain

Post 3+ Months Ago

hi all , its been a while , im having troubles with XML once again , one of you could probably solve it for me .

im coding a function that involves XML within , am basically trying to develop an XML based music album for a friend of mine but i have never experienced such XML structure before .

the elements would be Album title , songs under the albums & the lyrics associated with every song , so it would basically be like this :

Code: [ Select ]
<?xml version="1.0"?>
<xml>
    <album title="album1">

        <song><![CDATA[song 1]]></song>
        <lyrics><![CDATA[song 1 lyrics]]></lyrics>

        <song><![CDATA[song 2]]></song>
        <lyrics><![CDATA[song 2 Lyrics]]></lyrics>
    </album>

    
    
    <album title="album2">

        <song><![CDATA[song 1]]></song>
        <lyrics><![CDATA[song 1 lyrics]]></lyrics>

        <song><![CDATA[song 2]]></song>
        <lyrics><![CDATA[song 2 lyrics]]></lyrics>
    </album>
    
</xml>
  1. <?xml version="1.0"?>
  2. <xml>
  3.     <album title="album1">
  4.         <song><![CDATA[song 1]]></song>
  5.         <lyrics><![CDATA[song 1 lyrics]]></lyrics>
  6.         <song><![CDATA[song 2]]></song>
  7.         <lyrics><![CDATA[song 2 Lyrics]]></lyrics>
  8.     </album>
  9.     
  10.     
  11.     <album title="album2">
  12.         <song><![CDATA[song 1]]></song>
  13.         <lyrics><![CDATA[song 1 lyrics]]></lyrics>
  14.         <song><![CDATA[song 2]]></song>
  15.         <lyrics><![CDATA[song 2 lyrics]]></lyrics>
  16.     </album>
  17.     
  18. </xml>


and so on , now how would i be able to grab the album title , songs under it and the lyrics associated with it ? i would appreciate a code snippet or a lead to it anything that helps peeps .
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

// Edit -- I'm sorry, I just realized this is the Flash forum and not the programming forum. :oops:
Hopefully you understand PHP enough to translate this logic into AS ?

I'd push for a less error-prone structure if I were you, but if that's set in stone this should get you going.

Code: [ Select ]
<?php
 
$xml = simplexml_load_file('test.xml');
 
foreach($xml->children() as $album)
{
    echo "<h1>{$album['title']}</h1>";
    $expecting_lyrics = false;
    foreach($album->children() as $songnode)
    {
        switch($songnode->getName())
        {
            case 'song':
                if($expecting_lyrics)
                {
                    echo '<p>No Lyrics</p>';
                }
                echo sprintf('<h2>%s</h2>', $songnode);
            break;
            case 'lyrics':
                if(!$expecting_lyrics)
                {
                    echo '<h2>Unknown Song</h2>';
                }
                echo sprintf('<p>%s</p>', $songnode);
            break;
        }
        $expecting_lyrics = !$expecting_lyrics;
    }
}
 
?>
  1. <?php
  2.  
  3. $xml = simplexml_load_file('test.xml');
  4.  
  5. foreach($xml->children() as $album)
  6. {
  7.     echo "<h1>{$album['title']}</h1>";
  8.     $expecting_lyrics = false;
  9.     foreach($album->children() as $songnode)
  10.     {
  11.         switch($songnode->getName())
  12.         {
  13.             case 'song':
  14.                 if($expecting_lyrics)
  15.                 {
  16.                     echo '<p>No Lyrics</p>';
  17.                 }
  18.                 echo sprintf('<h2>%s</h2>', $songnode);
  19.             break;
  20.             case 'lyrics':
  21.                 if(!$expecting_lyrics)
  22.                 {
  23.                     echo '<h2>Unknown Song</h2>';
  24.                 }
  25.                 echo sprintf('<p>%s</p>', $songnode);
  26.             break;
  27.         }
  28.         $expecting_lyrics = !$expecting_lyrics;
  29.     }
  30. }
  31.  
  32. ?>
  • classified
  • Expert
  • Expert
  • User avatar
  • Posts: 540
  • Loc: Bahrain

Post 3+ Months Ago

Many thanks Joe , im unfortunately running out of time so i separated each album with its own XML for now ( deadlines )
but i really appreciate your help , i will look up the logic behind the php code once im free .

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 13 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.