SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple XML parser - I refuse to believe that there isn't one out there...

    ... that just allows you to put in a simple xml file like this:

    <moldb>
    <molecule>
    <name>Alanine</name>
    <symbol>ala</symbol>
    <code>A</code>
    <type>hydrophobic</type>
    </molecule>
    </moldb>

    and then gives you an output of $name, $symbol, $code and $ type for you to echo or manipulate or whatever.

    I've been searching and there must be something out there. But where?

  2. #2
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP 5 has this built in
    http://ca3.php.net/simplexml/

  3. #3
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have access to php5 unfortunately, only 4.

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Location
    Wolsztyn, Poland
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  5. #5
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's still incredibly complex to get 4 values. It's beyond me.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you tried xml_parser_create
    and xml_parse_into_struct?

    Hope it helps.

    Cheers

  7. #7
    SitePoint Evangelist -Oz-'s Avatar
    Join Date
    Nov 2001
    Location
    Phoenix, AZ, USA
    Posts
    406
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm on php4 and don't even both doing it on my own. I use magpie rss and their class makes it super easy to include xml feeds:
    http://magpierss.sourceforge.net/

    Edit: read more carefully, not sure this will do what you want, but worth a shot.
    Oz
    GamersMark - On Target Gaming
    OzTheory - Programming and Web Solutions
    AmIBlocked - Check if you've been blocked on IM

  8. #8
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I've found a very simple script which gets the data but it cannot get it to diplay it the way I'd like. Could someone tell me what I am doing wrong?

    The xml file:
    <ISBNdb server_time="2006-01-29T11:08:22">
    <BookList total_results="1" page_size="10" page_number="1" shown_results="1">
    <BookData book_id="pharmacology_a62" isbn="0443050473">
    <Title>Pharmacology</Title>
    <TitleLong/>
    <AuthorsText>
    H.P. Rang MB BS MA DPhil FRS, M.M. Dale MB BCh PhD, J.M. Ritter BM BCh MA DPhil FRCP,
    </AuthorsText>
    <PublisherText publisher_id="elsevier">Elsevier</PublisherText>
    </BookData>
    </BookList>
    </ISBNdb>


    The php script:
    PHP Code:
    <?php
    $xml_file 
    "abook.xml";
    $parser xml_parser_create();
    xml_set_element_handler($parser"startElement""endElement");
    xml_set_character_data_handler($parser"characterData");
    $filehandler fopen($xml_file"r");
    while (
    $data fread($filehandler4096)) {
        
    xml_parse($parser$datafeof($filehandler));
    }
    fclose($filehandler);
    xml_parser_free($parser);
    function 
    startElement($parser_instance$element_name$attrs) {
        switch(
    $element_name) {
            case 
    "BookData" : echo "<table>";
            break;
            case 
    "Title" : echo "<tr><td>";
            break;
            case 
    "AuthorsText" : echo "<tr><td>";
            break;
        }
    }
    function 
    characterData($parser_instance$xml_data) {
    echo 
    $xml_data;
    }
    function 
    endElement($parser_instance$element_name) {
        switch(
    $element_name) {
            case 
    "BookData" : echo "</table>";
            break;
            case 
    "Title" : echo "</td></tr>";
            break;
            case 
    "AuthorsText" : echo "</td></tr>";
            break;
        }

    ?>
    The output:
    Pharmacology H.P. Rang MB BS MA DPhil FRS, M.M. Dale MB BCh PhD, J.M. Ritter BM BCh MA DPhil FRCP, Elsevier

    As far as I can see it's the line "echo $xml_data;" that is giving the output. The script is modified from one that is supposed to take data from an xml file wiht url, summary and title as the element names. I've swapped in other names relavant to the xml file I'm dealing with but I clearly have no clue as to how this is supposed to work.

    Is there no way for me to simple extract the bits of $xmldata and put them into separate variables or even an array?


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
  •