www.webdeveloper.com
Results 1 to 4 of 4

Thread: How To Parse XML without XSLT_CREATE

  1. #1
    Join Date
    Feb 2004
    Location
    Middlesbrough Country: England
    Posts
    150

    How To Parse XML without XSLT_CREATE

    PHP Code:
        <?php
        
        $file 
    "guestbook.xml";

        
    # function to handle the beginning tags
        
    function tagBegin ($parser$element$attributes) {
            
    $element strtolower($element);
            global 
    $elem;
            switch (
    $element) {
                case 
    'entry':
                    
    $elem "entry";
                    echo 
    "\n    <div class=\"entry\">";
                    break;
                case 
    'title':
                    
    $elem "title";
                    echo 
    "<p class=\"title\">";
                    break;
                case 
    'message':
                    
    $elem "message";
                    echo 
    "<p>";
                    break;
                case 
    'name':
                    
    $elem "name";
                    echo 
    "<p class=\"footer\">Posted by ";
                    break;
                case 
    'time':
                    
    $elem "time";
                    echo 
    " on ";
                    break;
                default:
                    
    $elem "default";
                break;
            }
        }
        
        
    # function to handle the data of each element
        
    function cdataEater ($parser$cdata) {
            global 
    $elem;
            switch (
    $elem) {
                case 
    'time':
                    if (
    is_numeric($cdata)) {
                        echo 
    date("F d, Y h:i a"$cdata);
                    }
                    break;
                default:
                    
    #split words that are more than 50 characters long
                    
    $message explode(" "$cdata);
                    
    $splitmessage "";
                    foreach (
    $message as $word) {
                        
    $splitmessage .= wordwrap($word40" "1)." ";
                    }
                    
    $message $splitmessage;
                    echo 
    $message;
                    break;
            }        
        }
        
        
    # function to handle the ending tags
        
    function tagEnd ($parser$element) {
            
    $element strtolower($element);
            switch(
    $element) {
                case 
    'entry':
                    echo 
    "    </div>\n";
                    break;    
                case 
    'title':
                    echo 
    "</p>";
                    break;
                case 
    'message':
                    echo 
    "</p>";
                    break;
                case 
    'name':
                    break;
                case 
    'time':
                    echo 
    "</p>\n";
                    break;
            }
        }
        
        
    # create the parser and set the handling functions.
        
    $parser xml_parser_create();
        
    xml_set_element_handler ($parser'tagBegin''tagEnd');
        
    xml_set_character_data_handler ($parser'cdataEater');
        
        
    # read the file.
        
    $fp = @fopen ($file'r') or die ("Could not open guestbook file \"$file\" for reading");
        while (
    $data fread ($fpfilesize($file))) {
            if (!
    xml_parse ($parser$datafeof($fp))) {
                echo 
    'XML parsing error: '
                
    .@xml_error_string(@xml_get_error_code($xml_parser))
                .
    ' at line '.@xml_get_current_line_number($xml_parser).'.';
            }
        }
        
        
    # free up the parser.
        
    xml_parser_free($parser);
        
    ?>
    And the guestbook.xml file would look like this:
    Code:
    <guestbook>
    -
    	<entry>
    <title>Confusion?</title>
    -
    	<message>
    Would it be classed as confusing readers if they were to read the XML raw?
    
    Example just below...
    
    You have <message></message></message> in the XML.
    </message>
    <name>Bon Bon</name>
    <time>1076637076</time>
    </entry>
    -
    	<entry>
    <title>Test</title>
    <message></message></message>
    <name>Bon Bon</name>
    <time>1076636927</time>
    </entry>
    -
    	<entry>
    <title>Suggestions</title>
    -
    	<message>
    Also what would be good is if you recorded peoples IP addresses so that you know that wannabies like loys, whoever that is, are not me.
    
    Another suggestion, try including options to allow people to format the post with bold and etc but make sure that only attributes that make sense in the XML are added.
    </message>
    <name>Bon Bon</name>
    <time>1076620445</time>
    </entry>
    -
    	<entry>
    <title>test</title>
    <message>test</message>
    <name>test</name>
    <time>1076535381</time>
    </entry>
    -
    	<entry>
    <title>hello</title>
    <message>Why? not post in here.</message>
    <name>Merve</name>
    <time>1076443466</time>
    </entry>
    -
    	<entry>
    <title>test</title>
    -
    	<message>
    こんにちはー test
    </message>
    <name>loys</name>
    <time>1076403870</time>
    </entry>
    -
    	<entry>
    <title>hello</title>
    <message>hey dude</message>
    <name>me</name>
    <time>1076366752</time>
    </entry>
    -
    	<entry>
    <title>Me Again</title>
    -
    	<message>
    Any chance you can get in touch with me...
    
     bon_bon [at] ntlworld [dot] com,
    
     about the guestbook and ascii like "&".
    </message>
    <name>Bon Bon</name>
    <time>1076222997</time>
    </entry>
    -
    	<entry>
    <title>Congrats</title>
    -
    	<message>
    Sorry but I am just testing 1 more thing.
    
    Line breaks...
    
    
    
    ...
    Test completed.
    </message>
    <name>Bon Bon</name>
    <time>1076222908</time>
    </entry>
    -
    	<entry>
    <title>Congrats</title>
    -
    	<message>
    Sorry but I am just testing 1 more thing.
    
    Line breaks...
    
    
    
    ...
    Test completed.
    </message>
    <name>Bon Bon</name>
    <time>1076222834</time>
    </entry>
    -
    	<entry>
    <title>Congrats</title>
    -
    	<message>
    Nice! The flood restriction is a bit annoying though, maybe 15 seconds would be better?
    </message>
    <name>Bon Bon</name>
    <time>1076222674</time>
    </entry>
    -
    	<entry>
    <title>Test</title>
    <message>s\ript o\nload <\a href\=\"#">#</a></message>
    <name>Bon Bon</name>
    <time>1076222618</time>
    </entry>
    -
    	<entry>
    <title>got to my site</title>
    <message>www.joffe.tk</message>
    <name>Ben</name>
    <time>1075108166</time>
    </entry>
    -
    	<entry>
    <title>How is this better than the other guestbook?</title>
    <message>I wonder...</message>
    <name>JC</name>
    <time>1074858937</time>
    </entry>
    -
    	<entry>
    <title>Appreciation</title>
    -
    	<message>
    It is much appreciated when guys like you will help us trying to get our feet wet.  Thank you.
    
    Mr Limo
    </message>
    <name>Mr Limo</name>
    <time>1074509330</time>
    </entry>
    -
    	<entry>
    -
    	<message>
    I truely like what you have done and can do.  I've just started using flash and I look forward to seeing what it can do.
    </message>
    <name>Jake McAllister</name>
    <time>1074495022</time>
    </entry>
    -
    	<entry>
    <title>z</title>
    <message>z</message>
    <name>z</name>
    <time>1074474970</time>
    </entry>
    -
    	<entry>
    <title>hey</title>
    <message>hey</message>
    <name>hey</name>
    <time>1074042990</time>
    </entry>
    </guestbook>

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    I am afraid(it could only be me),I could not make much sense out of your question.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Feb 2004
    Location
    Middlesbrough Country: England
    Posts
    150
    It may just be you since there was no question .

    I was simply showing people how to parse XML tages with PHP without using XSLT_CREATE; since my servers would not add XSLT_CREATE support as they said it caused problems for them.

    This would also be useful for parsing RSS and other types of feeds from other sites.

    This is a sort of way of replacing the need for XSL templates since it does the same job but does not require the browser to support anything other that what it outputs (ie HTML).

    Sorry if I was not clear I was in a rush. This is also Pyro's work too which I should have also mentioned.

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,119
    lol..no worries..:-)
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles