SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP4 xml questions

    doing some parsing and not understanding why it behaves as it does. i have this function:
    PHP Code:
    function tag_contents($parser,$data){
        global 
    $usercount;
        global 
    $userdata;
        global 
    $current;
        
        if(!
    $current){return;}
        if(
    $current=="FIRST"){
            echo 
    "<br>filling first: " $data "<br>";
            
    $userdata[$usercount]["first"]=$data;
        }
        if(
    $current=="LAST"){
            
    $userdata[$usercount]["last"]=$data;
            echo 
    "filling last: " $data "<br>";
        }

    and this xml:
    Code:
    <?xml version="1.0"?>
    <Employees>
    <Employee id="12615" title="Architect">
    <First>Nancy</First>
    <Middle>J.</Middle>
    <Last>Davolio</Last>
    <Street>507 - 20th Ave. E. Apt. 2A</Street>
    <City>Seattle</City>
    <Zip>98122</Zip>
    <Country>U.S.A.</Country>
    <Office>5/7682</Office>
    <Phone>(206) 555-9857</Phone>
    <Photo>Photo.jpg</Photo>
    </Employee>
    <Employee id="12616" title="Developer">
    <First>Jim</First>
    <Middle>K.</Middle>
    <Last>Smith</Last>
    <Street>1106 East West Street</Street>
    <City>Seattle</City>
    <Zip>98123</Zip>
    <Country>U.S.A.</Country>
    <Office>5/7683</Office>
    <Phone>(206) 555-9856</Phone>
    <Photo>Photo.jpg</Photo>
    </Employee>
    </Employees>
    which outputs:
    Code:
    filling first: Nancy
    
    filling first:
    filling last: Davolio
    filling last:
    filling last:
    
    filling first: Jim
    
    filling first:
    filling last: Smith
    filling last:
    filling last: 
    why am i getting all the blanks (in bold)?
    Last edited by bbolte; Nov 22, 2006 at 15:27. Reason: modified the xml

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1 It depends on more than this code. How is this function invoked?
    2 You should go for php5, which has excellent xml-support. Php 4 is dead.

  3. #3
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i would love to do 5, can't. i only have 4 available on this server, can't be upgraded until the control panel is, yada, yada, yada....

    seems to be the line breaks, if i remove all the line breaks, the issue goes away. not sure how to check for line breaks. i'm doing a reg ex thing for spaces, but not sure of syntax to remove line breaks. here's the whole code:
    PHP Code:
    if(!($fp=fopen("assets/employeee.xml","rt"))){
        die(
    '<p>Couldn\'t open XML.</p>');
    }

    $usercount=0;
    $userdata=array();
    $current='';

    if(!(
    $xml_parser=xml_parser_create())){
        die(
    '<p>Couldn\'t create parser.</p>');
    }

    xml_set_element_handler($xml_parser,"start_tag","end_tag");
    xml_set_character_data_handler($xml_parser,"tag_contents");

    while(
    $data=fread($fp,4096)){
        
    $data=eregi_replace(">"."[[:space:]]+"."< ",">< ",$data);
        
    //echo '<h3>' . $data . '</h3>';
        
    if(!xml_parse($xml_parser,$data,feof($fp))){
            
    $reason xml_error_string(xml_get_error_code($xml_parser));
            
    $reason .= xml_get_current_line_number($xml_parser);
            die(
    $reason);
        }
    }

    xml_parser_free($xml_parser);

    echo 
    '<h2>Starting name output: ' $usercount ' total</h2>';
    for(
    $i=0;$i<$usercount;$i++){
        echo 
    "<p><b>Name:</b> " $userdata[$i]["title"] . " " $userdata[$i]["first"] . " " $userdata[$i]["last"] . '</p>';
    }

    // functions
    function start_tag($parser,$name,$attrib){
        
        
    //echo '<p>Start tag: ' . $name . '</p>';
        
        
    global $usercount;
        global 
    $userdata;
        global 
    $current;
        
        
    $current $name;
        
        switch(
    $name){
            case 
    $name=="EMPLOYEE":{
                
    $userdata[$usercount]["title"] = $attrib["TITLE"];
                
    //echo '<p>Name: ' . $attrib["TITLE"];
            
    }
        }
    }

    function 
    end_tag($parser,$name){
        global 
    $usercount;
        global 
    $userdata;

        if(
    $name=="EMPLOYEE"){$usercount++;}
    }

    function 
    tag_contents($parser,$data){
        global 
    $usercount;
        global 
    $userdata;
        global 
    $current;
        
        if(!
    $current){return;}
        if(
    $current=="FIRST"){
            echo 
    "<br>filling first: " $data "<br>";
            
    $userdata[$usercount]["first"]=$data;
        }
        if(
    $current=="LAST"){
            
    $userdata[$usercount]["last"]=$data;
            echo 
    "filling last: " $data "<br>";
        }
            
    // if i trim out the data, works fine
        /*if($current=="FIRST" && trim($data)!=""){
            //echo "<br>filling first: " . $data . "<br>";
            $userdata[$usercount]["first"]=$data;
        }
        if($current=="LAST" && trim($data)!=""){
            $userdata[$usercount]["last"]=$data;
            //echo "filling last: " . $data . "<br>";
        }*/


  4. #4
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    should note that i've tried different modes on this line: if(!($fp=fopen("assets/employeee.xml","rt"))){ . i've tried just "r", "rt", "rb" all with the same result.

  5. #5
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so i added: $data=str_replace("\n","",$data); after my regex line and that seems to resolve it

  6. #6
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and for good measure, added these as well:
    $data=str_replace("\r","",$data);
    $data=str_replace("\t","",$data);

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    51
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you solved te problem allready?

  8. #8
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The man says str_replace can take an array as an argument.

    $c= array("\n","\r","\t");

    str_replace($c,"",$data);

  9. #9
    SitePoint Wizard bbolte's Avatar
    Join Date
    Nov 2001
    Location
    The Central Plains
    Posts
    3,304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by plaatspunt
    So you solved te problem allready?
    yep
    Quote Originally Posted by Cups
    The man says str_replace can take an array as an argument.
    i'm such a noob! thanks cups, that's way better...


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
  •