Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Parsing XML and attributes

    Hi,

    First off I am a newbie to this and I'm trying my best to learn. Anyhow, I'm trying to parse this XML file. Though I'm running into a problem. This tag <itms:coverArt> appears three times in the same <item> with different attributes. I'm trying to figure out how I can choose the one I want to use. Does anyone know a way to filter out the attributes you don't want to use? Any help would be much appreciated. Here's the code I'm using to display all of them.

    PHP Code:
    $insideitem false;
    $tag "";
    $cover "";


    function 
    startElement($parser$name$attrs) {
        global 
    $insideitem$tag$cover;
        if (
    $insideitem) {
            
    $tag $name;
        } elseif (
    $name == "ITEM") {
            
    $insideitem true;
        }
    }

    function 
    endElement($parser$name) {
        global 
    $insideitem$tag$cover;
        if (
    $name == "ITEM") {
            echo 
    $cover;        
            
    $cover "";        
            
    $insideitem false;
        }
    }


    function 
    characterData($parser$data) {
        global 
    $insideitem$tag$cover;
        if (
    $insideitem) {
        switch (
    $tag) {
            case 
    "ITMS:COVERART":
            
    $cover .= $data;
            break;        
        }
        }
    }

    $xml_parser xml_parser_create();
    xml_set_element_handler($xml_parser"startElement""endElement");
    xml_set_character_data_handler($xml_parser"characterData");
    $fp fopen("http://ax.phobos.apple.com.edgesuite.net/WebObjects/MZStore.woa/wpa/MRSS/topalbums/sf=143441/genre=0000800/limit=10/rss.xml","r")
        or die(
    "Error reading RSS data.");
    while (
    $data fread($fp4096))
        
    xml_parse($xml_parser$datafeof($fp))
            or die(
    sprintf("XML error: %s at line %d"
                
    xml_error_string(xml_get_error_code($xml_parser)), 
                
    xml_get_current_line_number($xml_parser)));
    fclose($fp);
    xml_parser_free($xml_parser); 

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,096
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Hi , in the element parser ..function startElement($parser, $name, $attrs) {

    $attrs is an array of the elements attributes , now I do not know how you want to filter these but e.g you only want the images where height="100"

    PHP Code:
    <?
    } elseif ($name == "ITEM") { 
            if(
    $attrs['height']=='100'){
                
    $insideitem true
            }
        } 
    ?>
    may be along the right lines ?
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #3
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, I tried it with your code but it didn't return anything. I'll keep trying different things I guess. Thanks for the reply though!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •