Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 13
i under stand the basics of using xml and php. here are some functions im using. !! I didnt right them !! function startElement($parser,$name,$attribs); function endElement($parser,$name,$attribs); function characterData($parser,$data) I know ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    Apr 2003
    Location
    TEXAS
    Posts
    314

    xml and php


    i under stand the basics of using xml and php.

    here are some functions im using. !! I didnt right them !!

    function startElement($parser,$name,$attribs);
    function endElement($parser,$name,$attribs);
    function characterData($parser,$data)

    I know that when a starting tag is reached startElement is called the characterData is called whin the data is reached then when end element is reached endElement is called what im wondering is how can i write these functions so that lets this is you xml document

    <?xml version=1.0?>
    <books>
    <title>
    A title
    <description>
    a very good book
    </description>
    </title>
    <title>
    Another title
    <description>
    A very long
    description on multiple
    lines
    </description>
    </title>
    </books>

    I want to store this in a reference array where the title points to the descriptions.
    The computer made me do it!! Slackware and SUSE too Gig\'em WHOOOOP!!
    \"God put me on this earth to accomplish a certain amount of tasks, At the rate I\'m going I will never die.\" (I don\'t know)

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I don't really understand how you want to store it. Can you elaborate on that?

  3. #3
    Linux User
    Join Date
    Apr 2003
    Location
    TEXAS
    Posts
    314
    something like this

    as an array
    Title => "the description"

    or as a class like this might be easier

    class book {
    var $Title;
    var $Description;
    #maybe some other variable that would be useful#
    }

    my problem is when you get to data that is multiple lines long it doesnt stor right.
    The computer made me do it!! Slackware and SUSE too Gig\'em WHOOOOP!!
    \"God put me on this earth to accomplish a certain amount of tasks, At the rate I\'m going I will never die.\" (I don\'t know)

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Correct me if I'm wrong, but all you should have to do here is a state machine, right? What is your problem with multiple lines?

  6. #5
    Linux Engineer
    Join Date
    Apr 2003
    Location
    Sweden
    Posts
    796
    Doesnt you have to use and XMLparser for PHP when to read,import,manuipulate XML??

    Regards
    Regards

    Andutt

  7. #6
    Linux User
    Join Date
    Apr 2003
    Location
    TEXAS
    Posts
    314
    yes thats what im haveing problems with. I dont really know how to use it
    The computer made me do it!! Slackware and SUSE too Gig\'em WHOOOOP!!
    \"God put me on this earth to accomplish a certain amount of tasks, At the rate I\'m going I will never die.\" (I don\'t know)

  8. #7
    Linux Engineer
    Join Date
    Apr 2003
    Location
    Sweden
    Posts
    796
    Im sorry to say that i havent used the XML-stuff yet, ihave started to look at it on Perl but not on PHP....so if you learn something new, let me know.

    Regards
    Regards

    Andutt

  9. #8
    Linux User
    Join Date
    Apr 2003
    Location
    TEXAS
    Posts
    314

    This will be quite long

    This will be quite long but it is what I've learned about XML and php so far

    The example code is taken from "Programming PHP" authored by Lerdorf & Tatroe published by O'Reilly.

    parser that PHP uses is an expat like parser written in C.

    you have Element Handlers

    xml_set_element_handler(parser, start_element, end_element);

    in you code it will look like this
    xml_set_element_handler($parser, 'start_element', 'end_element');
    really though start_element and end_element are functions that you design to deal with a starting tag and a ending tag.

    next is you have to design your start_element and end_element functions

    ex: my_start_element_handler($parser, $element, $attributes);
    my_end_element_handler($parser, $element);
    so if you use these function calls then you would call the xml_set_element.....
    like this
    xml_set_element_handler($parser,'my_start_element_ handler','my_end_element_handler');

    useing the xml document i described earlier ill describe the start_element function and end_element function

    the three parameters passed into the start element function are for example:
    you hit the <book length=100> tag
    $element == book;
    $attributes == length => 100 //i think i havent looked into attributes yet
    $parser is the parser that you instantiate

    you define what you want done with the different elements and attribute your self.

    next is the end_element parameters which would be for example.
    </book>
    $parser same as above
    $element == '/book'

    like before you define what you want done

    ok nex tis the data handler which you set like before but you use this set function
    xml_set_character_data_handler($parser, 'handler');

    you define the data handler like so
    my_character_data_handler($parser, $data);
    which like before you define the set function like this
    xml_set_character_data_handler($parser,'xml_set_ch aracter_data_handler');

    ok this is the basic layout for defining the functions there are other things but for the simple document like described before i dont think ill need them next is defineing the parser itself and starting the parseing.

    here is a function that i think describes that process quite well

    function create_parser ($filename) {
    $fp = fopen("$filename",'r');
    $parser = xml_parser_create();

    xml_set_element_handler($parser, 'start_element','end_element');
    xml_set_character_data_handler($parser, 'charcter_data');


    return array($parser, $fp);
    }

    and here is how you parse the document

    function parse($parser, $fp) {
    $blocksize = 4 * 1024; //read in 4 kilobyte chunks

    while($data = fread($fp, $blocksize)) {
    if(!xml_parse($parser, $data, feof($fp))) {
    echo "error";
    return false;
    }
    }
    return true;
    }
    //how to start parseing useing the described functions
    if(list($parser, $fp) = create_parser('test.xml')) {
    parse($parser, $fp)
    fclose($fp);
    xml_parser_free($parser);
    }

    my problem is when the parser runs for example with this example xml

    <book>
    obrien
    </book>

    it will hit book and call the start_element then it will hit obiren and call the character_data
    handler then it will hit /book and call the end_element.

    my problem with this is book is not carried over to character_data so the program doesnt know that obrien is inside the book element. Ive been reading around and found useing stacks is one want to remedy this but if you character data is multiple lines long it becomed more confusing.

    If any of the above made any sense to anyone and maybe after reading it you can help me with my problem that would be great.

    If you read this far thanx for reading!!!!!
    The computer made me do it!! Slackware and SUSE too Gig\'em WHOOOOP!!
    \"God put me on this earth to accomplish a certain amount of tasks, At the rate I\'m going I will never die.\" (I don\'t know)

  10. #9
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Well, like I said, write a simple state machine, somewhat like this (modify to suit you):
    Code:
    $state = 0;
    $title = "";
    $desc = "";
    $books = array&#40;&#41;;
    
    function startElement&#40;$parser, $name, $attribs&#41;
    &#123;
        global $state;
    
        switch&#40;$state&#41;
        &#123;
        case 0&#58;
            if&#40;$name == "book"&#41;
                $state = 1;
            break;
        case 1&#58;
            if&#40;$name == "title"&#41;
            &#123;
                $state = 2;
                $title = "";
            &#125;
            if&#40;$name == "description"&#41;
            &#123;
                $state = 3;
                $desc = "";
            &#125;
            break;
        &#125;
    &#125;
    
    function endElement&#40;$parser, $name, $attribs&#41;
    &#123;
        global $state, $title, $desc, $books;
    
        switch&#40;$state&#41;
        &#123;
        case 1&#58;
            if&#40;$name == "book"&#41;
            &#123;
                $books&#91;$title&#93; = $desc;
                $state = 0;
            &#125;
            break;
        &#125;
    &#125;
    
    function characterData&#40;$parser, $data&#41;
    &#123;
        global $state, $title, $desc;
    
        switch&#40;$state&#41;
        &#123;
        case 2&#58;
            $title += $data;
            break;
        case 3&#58;
            $desc += $data;
            break;
        &#125;
    &#125;
    I haven't been writing PHP in quite a while, so I don't know how correct this is. Just take it as pseudocode.

  11. #10
    Linux User
    Join Date
    Apr 2003
    Location
    TEXAS
    Posts
    314
    Well that was a simple answer. I guess i was trying to do things too complicated with stacks and so on that I just didnt see what was in front of my face. Thanx for info.
    The computer made me do it!! Slackware and SUSE too Gig\'em WHOOOOP!!
    \"God put me on this earth to accomplish a certain amount of tasks, At the rate I\'m going I will never die.\" (I don\'t know)

Page 1 of 2 1 2 LastLast

Posting Permissions

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