Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 19

Thread: PHP Parsing XML

  1. #1
    Member webmastercorey's Avatar
    Join Date
    Nov 2005
    Location
    Kansas
    Posts
    49
    Member #
    11909
    I'm looking how to make this script only use what would be found with in a <VIDEO> tag.

    PHP Code:
    <?
    $xml_file 
    "videos.xml";

    // This code will work on PHP4.


    echo "<table border=0 cellpadding=5>";

    echo 
    "<tr><th colspan=2>Video Collection</th></tr>";
    function 
    startElement($parser_instance$element_name$attrs) {
        switch(
    $element_name) {
            case 
    "TITLE"     :    echo "<tr><td>";
                                break;
            case 
    "AUTHOR" :    echo "<td>";
                                break;
        }

     }
    function 
    characterData($parser_instance$xml_data) {
        echo 
    $xml_data;

     }
     
    function 
    endElement($parser_instance$element_name) {
        switch(
    $element_name) {
            case 
    "TITLE"   :    echo "</td>";
                                break;
            case 
    "AUTHOR" :    echo "</td></tr>";
                                break;
        }

     }
     
    $parser xml_parser_create();

    xml_set_element_handler($parser"startElement""endElement");

    xml_set_character_data_handler($parser"characterData");
    if (!(
    $filehandler fopen($xml_file"r"))) {
        die(
    "could not open XML input");

     }
    while (
    $data fread($filehandler4096)) {
        if (!
    xml_parse($parser$datafeof($filehandler))) {
            die(
    sprintf("XML error: %s at line %d",
                        
    xml_error_string(xml_get_error_code($parser)),
                        
    xml_get_current_line_number($parser)));
        }

     }
    fclose($filehandler);

     
    xml_parser_free($parser);
    echo 
    "</table>";
    ?>
    For example my XML playlist might have:

    <video file="video.mov">
    <title>Movie Title</title>
    <author>Author Name</author>
    </video>
    <commercials>
    <commercial id="default">default.mov</commercial>
    </commercials>


    And I don't want anything within the commercials section to be parsed.

    Thanks,
    Corey
    "What good will it be for a man if he gains the whole world, yet forfeits his soul?"
    -Matthew 16:26

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,733
    Member #
    5580
    Liked
    718 times
    Give me a link to an actual XML page that I can use for testing.
    I happen to have a simple PHP script that might work.


  4. #3
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    I would probably use the PHP DOM XML functions if I were you. Then you can just do a get_elements_by_tagname with no troubles at all.

  5. #4
    WDF Staff smoseley's Avatar
    Join Date
    Mar 2003
    Location
    Boston, MA
    Posts
    9,729
    Member #
    819
    Liked
    205 times
    Unfortunately, XML DOM isn't available in a base PHP install.

    Try this:
    PHP Code:
    $in_video false;

    function 
    startElement($parser_instance$element_name$attrs) {
        switch(
    $element_name) {
            case 
    "VIDEO":
                
    $in_video true;
                break;
            case 
    "TITLE":
                if (
    $in_video) echo "<tr><td>";
                break;
            case 
    "AUTHOR":
                if (
    $in_video) echo "<td>";
                break;
        }

     }
    function 
    characterData($parser_instance$xml_data) {
        if (
    $in_video) echo $xml_data;
     }
     
    function 
    endElement($parser_instance$element_name) {
        switch(
    $element_name) {
            case 
    "VIDEO":
                
    $in_video false;
                break;
            case 
    "TITLE":
                if (
    $in_video) echo "</td>";
                break;
            case 
    "AUTHOR":
                if (
    $in_video) echo "</td></tr>";
                break;
        }
     } 

  6. #5
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    If you're using PHP 5, http://www.php.net/dom
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  7. #6
    Member webmastercorey's Avatar
    Join Date
    Nov 2005
    Location
    Kansas
    Posts
    49
    Member #
    11909
    I do not have php 5 guys. I'll try the code above. Is there a way to have an attribute of video display as well? like <video file="file.flv">?

    Here is my code for both the xml and php sheet as is on my site:

    XML:
    Code:
    <ratings>
      <video file="enter-the-matrix.flv">
        <filename>enter-the-matrix.flv</filename>
    	  <title>Enter the Matrix</title>
    	  <runtime>auto</runtime>
    	  <author>MKCproductions</author>
    	  <rating>0</rating>
    	  <views>0</views>
    	  <commercialid></commercialid>
      </video>
      <video file="kfc-awards-2004.flv">
        <filename>kfc-awards-2004.flv</filename>
        <title>KFC Awards 2004</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="tomber.flv">
        <filename>tomber.flv</filename>
        <title>Tomber</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="a-day-in-the-life-of-ryan-debok.flv">
        <filename>a-day-in-the-life-of-ryan-debok.flv</filename>
        <title>A Day in the Life of Ryan Debok</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="coke-test.flv">
        <filename>coke-test.flv</filename>
        <title>Coke Test</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="kfc-awards-2005.flv">
        <filename>kfc-awards-2005.flv</filename>
        <title>KFC Awards 2005</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="kleenex.flv">
        <filename>kleenex.flv</filename>
        <title>Kleenex</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="know-your-rights.flv">
        <filename>know-your-rights.flv</filename>
        <title>Know Your Rights</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="a-merry-western.flv">
        <filename>a-merry-western.flv</filename>
        <title>A Merry Western</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="peripheral-destination.flv">
        <filename>peripheral-destination.flv</filename>
        <title>Peripheral Destination</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="take-me-out.flv">
        <filename>take-me-out.flv</filename>
        <title>Take Me Out</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="989-dance.flv">
        <filename>989-dance.flv</filename>
        <title>98.9 Dance</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="989-ninjas.flv">
        <filename>989-ninjas.flv</filename>
        <title>98.9 Ninjas</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="aliens.flv">
        <filename>aliens.flv</filename>
        <title>Aliens</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="baptism-video.flv">
        <filename>baptism-video.flv</filename>
        <title>Baptism Video</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="catsup.flv">
        <filename>catsup.flv</filename>
        <title>Catsup</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="christian-defense.flv">
        <filename>christian-defense.flv</filename>
        <title>Christian Defense</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="christian-retreat.flv">
        <filename>christian-retreat.flv</filename>
        <title>Christian Retreat</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="help-the-beatles.flv">
        <filename>help-the-beatles.flv</filename>
        <title>Help the Beatles</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="kfc-awards-2006.flv">
        <filename>kfc-awards-2006.flv</filename>
        <title>KFC Awards 2006</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="latin-pop.flv">
        <filename>latin-pop.flv</filename>
        <title>Latin Pop</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="a-merry-western-II.flv">
        <filename>a-merry-western-II.flv</filename>
        <title>A Merry Western II: Electric Boogaloo</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="mkc-logo.flv">
        <filename>mkc-logo.flv</filename>
        <title>MKC Logo</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="murder-for-love.flv">
        <filename>murder-for-love.flv</filename>
        <title>Murder for Love</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="mustard.flv">
        <filename>mustard.flv</filename>
        <title>Mustard</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="ninja-extended.flv">
        <filename>ninja-extended.flv</filename>
        <title>Ninja Extended</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="relish.flv">
        <filename>relish.flv</filename>
        <title>Relish</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="spanish-video.flv">
        <filename>spanish-video.flv</filename>
        <title>Spanish Video</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="where-r-u-going.flv">
        <filename>where-r-u-going.flv</filename>
        <title>Where R U Going?</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="yankee-doodle.flv">
        <filename>yankee-doodle.flv</filename>
        <title>Yankee Doodle</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="zombies-attacked.flv">
        <filename>zombies-attacked.flv</filename>
        <title>Zombies Attacked</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="cajun-man.flv">
        <filename>cajun-man.flv</filename>
        <title>Cajun Man</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <video file="a-merry-western-III.flv">
        <filename>a-merry-western-III.flv</filename>
        <title>A Merry Western III</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
        <video file="peripheral-destination-II.flv">
        <filename>peripheral-destination-II.flv</filename>
        <title>Peripheral Destination II: Yearning for Life, Longing for Death</title>
        <runtime>auto</runtime>
        <author>MKCproductions</author>
        <rating>0</rating>
        <views>0</views>
        <commercialid></commercialid>
      </video>
      <commercials>
    	  <commercial id="default">mkc-logo.flv</commercial>
    	  <commercial id="1">v</commercial>
    	  <commercial id="2"></commercial>
    	  <commercial id="3"></commercial>
      </commercials>
    </ratings>
    And the PHP file as of right now:

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>MKCproductions | Videos</title>
        <link rel="stylesheet" type="text/css" href="http://www.mkcproductions.com/styles/reflection.css" media="screen" />
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="http://www.mkcproductions.com/scripts/dropmenu.js"></script>
        <script type="text/javascript" src="http://www.mkcproductions.com/scripts/swfobject.js"></script>
        <script type="text/javascript" src="http://www.mkcproductions.com/scripts/global.js"></script>
    </head>
    <body>
        <div id="master-container">
        <div id="header">
                <!-- MKC Bar 1.5  by Corey Campbell -->
              <?php include('http://www.mkcproductions.com/includes/mkc-bar-usernone.htm'); ?>
                <!-- // MKC Bar 1.5  by Corey Campbell // -->        
            </div>
            <div id="body">
            <?
    $xml_file 
    "videos.xml";
    echo 
    "<table border=0 cellpadding=5 width=100%>";

    function 
    startElement($parser_instance$element_name$attrs) {
        switch(
    $element_name) {
            case 
    "FILENAME"      :    echo "<tr><td><a href=\"http://video.mkcproductions.com/video-player.php?video=";
                                break;
            case 
    "TITLE"         :    echo "";
                                break;
            case 
    "AUTHOR"        :    echo "<span style=\"display: none;\">";
                                break; 
            case 
    "RUNTIME"       :    echo "<span style=\"display: none;\">";
                                break; 
            case 
    "RATING"        :    echo "<td><strong>Rating: </strong>";
                                break; 
            case 
    "VIEWS"         :    echo "<td><strong>Views: </strong>";
                                break; 
            case 
    "COMMERCIALID"  :    echo "<span style=\"display: none;\">";
                                break; 
            case 
    "COMMERCIALS"  :    echo "<span style=\"display: none;\">";
                                break;             
        }

     }
     
    function 
    characterData($parser_instance$xml_data) {
        echo 
    $xml_data;

     }
     
    function 
    endElement($parser_instance$element_name) {
        switch(
    $element_name) {
            case 
    "FILENAME"      :    echo "\">";
                                break;
            case 
    "TITLE"         :    echo "</a></td>";
                                break;
            case 
    "AUTHOR"        :    echo "</span>";
                                break; 
            case 
    "RUNTIME"       :    echo "</span>";
                               break;
            case 
    "RATING"        :    echo "</td>";
                                break; 
            case 
    "VIEWS"         :    echo "</td></tr>";
                                break;  
            case 
    "COMMERCIALID"  :    echo "</span>";
                                break; 
            case 
    "COMMERCIALS"  :    echo "</span>";
                                break; 
        }

     }
     
    $parser xml_parser_create();
    xml_set_element_handler($parser"startElement""endElement");

     
    xml_set_character_data_handler($parser"characterData");
    if (!(
    $filehandler fopen($xml_file"r"))) {
        die(
    "could not open XML input");

     }
    while (
    $data fread($filehandler4096)) {
        if (!
    xml_parse($parser$datafeof($filehandler))) {
            die(
    sprintf("XML error: %s at line %d",
                        
    xml_error_string(xml_get_error_code($parser)),
                        
    xml_get_current_line_number($parser)));
        }

     }
    fclose($filehandler);

     
    xml_parser_free($parser);
    echo 
    "</table>";
    ?>
              </div>
            <div id="footer">
                    <?php include('http://www.mkcproductions.com/includes/footer.htm'); ?>
            </div>
        </div>
    </body>
    </html>
    "What good will it be for a man if he gains the whole world, yet forfeits his soul?"
    -Matthew 16:26

  8. #7
    Member webmastercorey's Avatar
    Join Date
    Nov 2005
    Location
    Kansas
    Posts
    49
    Member #
    11909
    I couldn't get that to work transio. I've used a crappy display: none; right now... lol it kinda works but definitely not clean. I do need it to display attributes of a <video file="file.flv">
    "What good will it be for a man if he gains the whole world, yet forfeits his soul?"
    -Matthew 16:26

  9. #8
    Member webmastercorey's Avatar
    Join Date
    Nov 2005
    Location
    Kansas
    Posts
    49
    Member #
    11909
    I just added and it fixed my problem.

    PHP Code:
            case "VIDEO"         :    echo "<tr><td><a href=\"http://video.mkcproductions.com/video-player.php?video=" $attrs[FILE] . "\">";
                                break; 
    "What good will it be for a man if he gains the whole world, yet forfeits his soul?"
    -Matthew 16:26

  10. #9
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Oy... Endless switch statement... There's gotta be a better way O_O. Sadly, I don't have time to sit down and think of it

  11. #10
    Member webmastercorey's Avatar
    Join Date
    Nov 2005
    Location
    Kansas
    Posts
    49
    Member #
    11909
    Yeah my code isn't very clean. The main reason being that I'm completely new to this xml parser idea.
    "What good will it be for a man if he gains the whole world, yet forfeits his soul?"
    -Matthew 16:26


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:25 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com