XML parser will not return a single element from my XML code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • abdoelmasry
    New Member
    • Oct 2006
    • 104

    XML parser will not return a single element from my XML code

    HI men

    im trying To get xml file conetent To insert to database

    xml parser functions couldn't get single element from xml file

    it's return all start elements , end elements and data elements

    i need to create variables have element name and carry element data

    then i can get element data by code :

    [PHP]echo $(element name) ;[/PHP]

    here is my code , it's not working

    i don't know what is wrong on it

    Any body help PLZ !

    [PHP]<?php
    $xml_filename=" style.xml";

    function load_file($file _to_load){
    $openxml=fopen( $file_to_load," r") or die("Cann't Open File $file_to_load") ;
    $file_data=frea d($openxml,file size($file_to_l oad));
    return $file_data;
    }

    function opentag($xml_pa rser,$starttag) {
    global $starttag;
    }

    function closetag($xml_p arser,$endtag){
    global $endtag;
    }

    function xml_data($xml_p arser,$data){
    $$starttag=$dat a;
    }

    $xml_parser=xml _parser_create( );
    xml_set_element _handler($xml_p arser,"opentag" ,"closetag") ;
    xml_set_charact er_data_handler ($xml_parser,"x ml_data");
    xml_parse($xml_ parser,load_fil e($xml_filename ));
    xml_parser_free ($xml_parser);

    //every element name variable carry element data;

    echo $Elementname;
    ?>[/PHP]
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Abdoelmasry.

    Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

    Is there any whitespace before the XML declaration in your XML code?

    Try trim()ing your content.

    Also this function:
    [code=php]
    function load_file($file _to_load){
    $openxml=fopen( $file_to_load," r") or die("Cann't Open File $file_to_load") ;
    $file_data=frea d($openxml,file size($file_to_l oad)) ;
    return $file_data;
    }
    [/code]

    Is nice, but why reinvent the wheel?

    Comment

    • kovik
      Recognized Expert Top Contributor
      • Jun 2007
      • 1044

      #3
      If you have PHP5 (which you should!), I'd suggest that you use the DOM. It's much easier and follows the W3C standards.

      Comment

      • abdoelmasry
        New Member
        • Oct 2006
        • 104

        #4
        Hi pbmods

        im sorry for bad title again

        i want to set good title but im not good in english

        im trying to post good words as i can

        thank you for allowance

        i tried to trim elements name and change case to lower

        but it's not working

        the main problem is :

        the function GLOBAL not working , i cann't make any public variables

        this is not from php.ini , im working with GLOBAL with another script in the same pc , it works good

        but some thing wrong with this script

        Here Is Modified Code:

        [PHP]<?php
        $xml_filename=" style.xml";

        function load_file($file _to_load){
        $openxml=fopen( $file_to_load," r") or die("Cann't Open File $file_to_load") ;
        $file_data=frea d($openxml,file size($file_to_l oad));
        return trim($file_data );
        }

        function opentag($xml_pa rser,$starttag) {
        GLOBAL $starttag;
        $starttag=trim( $starttag);
        $starttag=strto lower($starttag );
        }
        function closetag($xml_p arser,$endtag){
        GLOBAL $endtag;
        $endtag=trim($e ndtag);
        $endtag=strtolo wer($endtag);
        }

        function xml_data($xml_p arser,$data){
        GLOBAL $$starttag;
        $$starttag=$dat a;
        }

        $xml_parser=xml _parser_create( );
        xml_set_element _handler($xml_p arser,"opentag" ,"closetag") ;
        xml_set_charact er_data_handler ($xml_parser,"x ml_data");
        xml_parse($xml_ parser,load_fil e($xml_filename ));
        xml_parser_free ($xml_parser);

        //every element name variable carry element data;
        echo $element_name;
        ?>[/PHP]

        yes volectricity im using php5 , i know DOM Functions
        But i think xml parser is faster and uses small memory range

        i need advice

        thank U

        Comment

        • pbmods
          Recognized Expert Expert
          • Apr 2007
          • 5821

          #5
          Heya, abdoelmasry.

          Nothing personal; don't worry about it. It's a standard response, and I'm certainly making no judgments. I appreciate the effort, and in return, I will do what I can to make sure that as many people can find your post as possible :)

          In this function:
          [code=php]
          function xml_data($xml_p arser,$data){
          GLOBAL $$starttag;
          $$starttag=$dat a;
          }
          [/code]

          Did you mean to do this, instead:
          [code=php]
          function xml_data($xml_p arser,$data){
          global $starttag;
          $starttag=$data ;
          }
          [/code]

          Comment

          • abdoelmasry
            New Member
            • Oct 2006
            • 104

            #6
            thank u pbmods
            for everything

            im realy mean this:

            [PHP]function xml_data($xml_p arser,$data){
            GLOBAL $$starttag;
            $$starttag=$dat a;
            }[/PHP]

            because

            i want to set element name as variable

            ex:

            if the element name is : login_form

            the variable will be:

            [PHP]$login_form= login form html code(element contents)[/PHP]

            then i can print login form by writing :

            [PHP]echo $login_form[/PHP]

            i don't know what is wrong with function GLOBAL

            when i remove GLOBAL The code works good but as you know

            i can't read variables out of the function

            i mean i can't make any variable as global

            Comment

            • pbmods
              Recognized Expert Expert
              • Apr 2007
              • 5821

              #7
              Heya, abdoelmasry.

              Gotcha.

              Using 'global' instead of 'GLOBAL' is a tad bit faster, as then PHP doesn't have to convert it to lowercase.

              The only problem is that you don't define $starttag in your function. In other words, $$starttag === ${null}, which is probably not what you meant.

              Perhaps you meant to do something like this:
              [code=php]
              function register_global ($starttag, $data)
              {
              global $$starttag;
              $$starttag = $data;
              }
              [/code]

              Comment

              • abdoelmasry
                New Member
                • Oct 2006
                • 104

                #8
                Hi pbmods

                i tried but it's not working

                i gonna be mad

                it's not important code in my work but

                i wanna know WHAT IS THE WRONG WITH THIS CODE ???? !i!i!i!i!i!i!i! i!i!i!i!i!i!i!

                [PHP]<?php
                $xml_filename=" style.xml";

                function register_global ($tagvar,$data)
                {
                global $$tagvar;
                $$tagvar=$data;
                }

                function load_file($file _to_load){
                $openxml=fopen( $file_to_load," r") or die("Cann't Open File $file_to_load") ;
                $file_data=frea d($openxml,file size($file_to_l oad)) ;
                return $file_data;
                }

                function opentag($xml_pa rser,$starttag) {
                global $starttag;
                $starttag=trim( $starttag);
                $starttag=strto lower($starttag );
                }

                function closetag($xml_p arser,$endtag){
                }

                function xml_data($xml_p arser,$data){
                global $starttag;
                register_global ($starttag,$dat a);
                }

                $xml_parser=xml _parser_create( );
                xml_set_element _handler($xml_p arser,"opentag" ,"closetag") ;
                xml_set_charact er_data_handler ($xml_parser,"x ml_data");
                xml_parse($xml_ parser,load_fil e($xml_filename ));
                xml_parser_free ($xml_parser);

                //every element name variable carry element data;
                echo $login_form; //To view login form
                ?>[/PHP]

                waiting For Idea ............

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Heya, Abdoelmasry.

                  Go back and re-read my last post.

                  The problem is that $starttag is null in your xml_data() function. I used the name 'register_globa l' because that better describes what the function does, instead of 'xml_data'. But I meant the same function.

                  Comment

                  • abdoelmasry
                    New Member
                    • Oct 2006
                    • 104

                    #10
                    Hi man

                    i got it

                    the code works good

                    but some thing wrong with xml tags

                    in xml file there is two tags for any element : open , close

                    the function reads open tag then set it as variable , it carry the element data but when it read close tag ... this is the problem

                    close tag not carring any data soooo

                    the function reset the variable with null

                    i make another code to read elements it works great

                    i just send tag name and it returns array of all elements , its data

                    here is my new code :

                    [PHP]<?php
                    $xml_style_file ="style.xml" ;

                    ## File Loader ##
                    function load_file($file _to_load){
                    $openxml=fopen( $file_to_load," r") or die("Cann't Open File $file_to_load") ;
                    $file_data=frea d($openxml,file size($file_to_l oad));
                    return $file_data;
                    }

                    ## XML element splitter ##
                    function get_tage_elemen ts($tag_name,$x ml_array){
                    // Get Start , end ofest
                    while(list($key ,$value)=each($ xml_array)){
                    if(($value["tag"]=="$tag_name" ) and ($value["type"]=="open")){
                    $startofest=key ($xml_array);
                    }
                    if(($value["tag"]=="$tag_name" ) and ($value["type"]=="close")){
                    $endofest=key($ xml_array);
                    }
                    }
                    unset($key,$val ue);
                    //get tag slice
                    $slice_size=$en dofest-$startofest;
                    $tagarray=array _slice($xml_arr ay,$startofest, $slice_size);
                    //create elements array
                    while(list($key ,$value)=each($ tagarray)){
                    if($value["type"]=="complete") {
                    $tagcontents[$value["tag"]]["name"]=$value["tag"];
                    $tagcontents[$value["tag"]]["data"]=$value["value"];
                    }}
                    unset($key,$val ue);
                    return $tagcontents;
                    }

                    $xml_parser=xml _parser_create( );
                    xml_parse_into_ struct($xml_par ser,load_file($ xml_style_file) ,$struct);
                    xml_parse($xml_ parser,load_fil e($xml_style_fi le));
                    xml_parser_free ($xml_parser);


                    //Get all elements on tag FORMS

                    $forms=get_tage _elements("FORM S",$struct);

                    ?>[/PHP]


                    thank U Great man

                    Comment

                    • pbmods
                      Recognized Expert Expert
                      • Apr 2007
                      • 5821

                      #11
                      Heya Abdoelmasry.

                      Change these line:
                      [code=php]
                      xml_parse_into_ struct($xml_par ser,load_file($ xml_style_file) ,$struct);
                      xml_parse($xml_ parser,load_fil e($xml_style_fi le));
                      [/code]

                      To this:
                      [code=php]
                      xml_parse_into_ struct( $xml_parser, load_file($xml_ style_file), $values, $index );
                      [/code]

                      To access any element of the XML document, you would use this syntax:
                      [code=php]
                      echo $values[$index['TAGNAME'][0]];
                      [/code]

                      You can use these statements to get a better idea of what xml_parse_into_ struct() does:
                      [code=php]
                      print_r($values );
                      print_r($index) ;
                      [/code]

                      Comment

                      • abdoelmasry
                        New Member
                        • Oct 2006
                        • 104

                        #12
                        Ya man

                        it works Great

                        thank you

                        im sorry for big thread

                        Comment

                        • pbmods
                          Recognized Expert Expert
                          • Apr 2007
                          • 5821

                          #13
                          Heya, Abdoelmasry.

                          That's what we're here for ~_^

                          Comment

                          Working...