SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2000
    Location
    Palo Alto, CA
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    reading XML attributes with PHP. (<tag attr='val' />)

    Hi, i have an e XML file like this:
    <head>
    <tag attribute="value" attribute2="value" />
    </head>
    I need to read attribute and attribute2 values.
    Is it passible in PHP? Please help me,
    Thanks.

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2000
    Location
    Palo Alto, CA
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    n/m, I've got it working....

  3. #3
    SitePoint Zealot cokeman's Avatar
    Join Date
    Dec 2000
    Location
    So. California
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Out of curiosity, what did you do to get it working?

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2000
    Location
    Palo Alto, CA
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Better what havent I done to get it working...
    This is quick peace of code i did last night at 3am.... so, you know.
    PHP Code:
    <?php
    require("global.php"); 

    $response '';

    function 
    setvals($response) {
        echo 
    "Result: $response[0]<br>";
        echo 
    "Name: $response[1]<br>";
        echo 
    "$response[2]<br>";
        echo 
    "$response[3]<br>";
    }
    function 
    start_element($parser$element_name$element_attrs) {
        
    $debug 1;
        switch(
    $element_name) {
            case 
    "RESULT":
                
    $_resultcode = ("$element_name{$element_attrs['RESULTCODE']}");
                
    $_username = ("$element_name{$element_attrs['USERNAME']}");
                
    $_name =  ("$element_name{$element_attrs['NAME']}");
                
    $_sid =  ("$element_name{$element_attrs['SID']}");
                 
    /* 6 is a number of letters in RESULT. we need to count letters better. coz it may change.
                  */
                
    $resultcode substr($_resultcode6);
                
    $username substr($_username6);
                
    $name substr($_name6);
                
    $sid substr($_sid6);
                if(
    $debug) {
                    print 
    "ResultCode: $resultcode<br>";
                    print 
    "Name: $name<br>";
                    print 
    "Username: $username<br>";
                    print 
    "SID: $sid<br>";
                }
                
    $response = array($resultcode$username$name$sid);
                
    setvals($response);
                break;
        }
    }

    function 
    end_element($parser$element_name) {
        return;
    }
    function 
    character_data($parser$data) {
        echo 
    $data;
    }
    function 
    getData() {
         
    $file "test.xml";

        
    $parser xml_parser_create();
        
    xml_set_element_handler($parser'start_element''end_element');
        
    xml_set_character_data_handler($parser'character_data');
        
    $fp fopen($file,"r") or die("cant open file: $file");

        while(
    $data fread($fp4096)) {
            
    xml_parse($parser$datafeof($fp))
                or die(
    sprintf("XML Error: %s at  line %d",
                    
    xml_error_string(xml_get_error_code($parser)),
                    
    xml_get_current_line_number($parser)));
        }
        
    xml_parser_free($parser);
    }
    getData();
    ?>
    And XML file looks like this:
    PHP Code:
    <RESPONSE>
              <
    RESULT ResultCode="130" UserName="john123" Name="John Johnson"  SID="a1230cvxdf02?l==" />
    </
    RESPONSE
    Last edited by java; Jun 12, 2001 at 11:44.


Bookmarks

Posting Permissions

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