SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Thread: XML Parsing

  1. #1
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML Parsing

    Hey,

    I was wondering how I would go about parsing the following XML document:
    Code XML:
    <autoupdateservice>
    <update ver="0.1.1">
    <file name="admin.php" hash="25697170d3df46ed6ab9a46cc465319d">
    <file name="index.php" hash="25697170d3df46ed6ab9a46cc465319d">
    </update>
    <update ver="0.1.2">
    <file name="admin.php" hash="25697170d3df46ed6ab9a46cc465319d">
    <file name="index.php" hash="25697170d3df46ed6ab9a46cc465319d">
    </update>
    </autoupdateservice>

    There will be a number of <update> tags and a number of <file> tags within the <update> tags.

    I have had no prior experience to parsing XML documents, but I am very familiar with PHP.

    It would be good to get it compatible with PHP4 but if I can only to get it to work with PHP5 that ill just have to go with that :P

    Thanks,

    Tom

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    search =  '<autoupdateservice>'.
                
    '<update ver="0.1.1">'.
                
    '<file name="admin.php" hash="25697170d3df46ed6ab9a46cc465319d">'.
                
    '<file name="index.php" hash="25697170d3df46ed6ab9a46cc465319d">'.
                
    '</update>'.
                
    '<update ver="0.1.2">'.
                
    '<file name="admin.php" hash="25697170d3df46ed6ab9a46cc465319d">'.
                
    '<file name="index.php" hash="25697170d3df46ed6ab9a46cc465319d">'.
                
    '</update>'.
                
    '</autoupdateservice>';

    function 
    ParseRevisionsString$In_text ) {
        
    $parts explode">" $In_text ); // Explode @ param ends
        
        
    foreach( $parts as $V ) {
            if ( 
    strstr$V"update" ) ) {
                
    // Get Version number
                
    $start strpos$V"ver=" ) + 5;
                
    $end = ( strpos $V'"'$start ) ) - $start;
                
    $Ver substr$V$start$end );
            }
            if ( 
    strstr$V"file" ) ) {
                
    // Get name
                
    $start strpos$V"name=" ) + 6;
                
    $end = ( strpos $V'"'$start ) - $start );
                
    $object[$Ver]["name"][] = substr$V$start$end );
                
    // Get hash
                
    $start strpos$V"hash=" ) + 6;
                
    $end = ( strpos $V'"'$start ) - $start);
                
    $object[$Ver]["hash"][] = substr$V$start$end );
            }
        }
        return 
    $object;
    }


    var_dumpParseRevisionsString$search ) ); 
    Yields :
    array(2) { ["0.1.1"]=> array(2) { ["name"]=> array(2) { [0]=> string(9) "admin.php" [1]=> string(9) "index.php" } ["hash"]=> array(2) { [0]=> string(32) "25697170d3df46ed6ab9a46cc465319d" [1]=> string(32) "25697170d3df46ed6ab9a46cc465319d" } } ["0.1.2"]=> array(2) { ["name"]=> array(2) { [0]=> string(9) "admin.php" [1]=> string(9) "index.php" } ["hash"]=> array(2) { [0]=> string(32) "25697170d3df46ed6ab9a46cc465319d" [1]=> string(32) "25697170d3df46ed6ab9a46cc465319d" } } }
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Zealot
    Join Date
    May 2006
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks ALOT ;D works a treat and I understand how it works.


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
  •