www.webdeveloper.com
Results 1 to 11 of 11

Thread: XML parser

  1. #1
    Join Date
    May 2012
    Posts
    11

    XML parser

    hi, i have the following xml :

    Code:
    <?xml version="1.0" encoding="UTF-8" ?>
    <rss version="2.0" >
    <channel>
    <item>
    <title>title 1</title>
    <link>http://www.somelink.com/clients/image1.JPG</link>
    <source></source>
    <pubDate>08-05-2012</pubDate>
    <category>cars</category>
    <author>08-05-2012</author>
    </item>
    <item>
    <title>title 2 </title>
    <link>http://www.somelink.com/clients/image1.JPG</link>
    <source></source>
    <pubDate>08-05-2012</pubDate>
    <category>bikes</category>
    <author>08-05-2012</author>
    </item>
    </channel>
    </rss>
    that i parse it with this code at php, successfully
    Code:
    <?php
    error_reporting(0);
    $html = "";
    $url= "http://www.mysite.com/clients/mydata.xml";
    $xml = simplexml_load_file($url);
    
    
    
    
    if(phpversion() >= '5.3.0'){
        $bar_count = $xml->channel->item->count();
        }
    else    {
        $doc = new DOMDocument();
        $str = $xml->asXML();
        $doc->loadXML($str);
        $bar_count = $doc->getElementsByTagName("item")->length;
        }
    
    for($i =0; $i < $bar_count; $i++) { 
                $title = $xml -> channel -> item [$i] -> title;
                $date = $xml -> channel -> item [$i] -> pubDate;          
                $category = $xml -> channel -> item [$i] -> category;
                $author = $xml -> channel -> item [$i] -> author;  
                $link = $xml -> channel -> item [$i] -> link;
    			
    			
    echo "<table border='1'>";
               
    		   
    		   $html  .= "<center><tr><h3>$title : $date</h3>  <a href=$link > <img src=$link height='367' width='300' align='middle' border='0' /></a> </tr></center>";
    
    }
    
    echo "</table>";
    
    
    echo $html;
    
     
    
    ?>

    how can i do the same with the following xml file :


    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <tickdata>
      <t>Ώρα: 17:12</t>
      <m s="ΓΔ" l="620,54" c="-23,33" p="-3,62" v="-1"/>
      <m s="FTSE" l="231,83" c="-10,77" p="-4,44" v="-1"/>
      <m s="FTSEA" l="534,97" c="-24,67" p="-4,41" v="-1"/>
      <m s="FTSEM" l="596,28" c="-25,72" p="-4,14" v="-1"/>
      <m s="FTSES" l="161,35" c="-6,51" p="-3,88" v="-1"/>
      <m s="FTSEB" l="163,59" c="-17,36" p="-9,59" v="37500"/>
      <m s="FTSEL" l="533,10" c="-23,88" p="-4,29" v="1000"/>
      <m s="FTSENTR" l="899,33" c="-41,80" p="-4,44" v="40500"/>
      <m s="ΑΒΑΞ" l="0,69" c="-0,06" p="-7,77" v="200"/>
      <m s="ΑΒΕ" l="0,08" c="0,02" p="28,57" v="5"/>
      <m s="ΑΒΕΝΙΡ" l="0,06" c="0,00" p="1,85" v="510"/>
      <m s="ΑΕΓΕΚ" l="0,09" c="0,01" p="7,32" v="2006"/>
      <m s="ΑΘΗΝΑ" l="0,10" c="0,00" p="-1,96" v="1000"/>
      <m s="ΑΚΡΙΤ" l="0,24" c="0,00" p="0,00" v="1500"/>
      <m s="ΑΛΚΟ" l="0,30" c="-0,06" p="-16,67" v="10"/>
      <m s="ΑΛΤΕΚ" l="0,03" c="-0,01" p="-16,67" v="1"/>
      <m s="ΑΛΦΑ" l="0,72" c="-0,12" p="-14,29" v="400"/>
      <m s="ΑΝΔΡΟ" l="0,16" c="-0,01" p="-5,33" v="1000"/>
      <m s="ΑΝΕΚ" l="0,09" c="-0,01" p="-9,47" v="1"/>
      <m s="ΑΡΑΙΓ" l="1,23" c="-0,02" p="-1,60" v="15"/>
      <m s="ΑΡΒΑ" l="5,00" c="0,01" p="0,20" v="20"/>
      <m s="ΑΣΚΟ" l="0,23" c="-0,02" p="-6,94" v="856"/>
      <m s="ΑΣΤΑΚ" l="7,60" c="0,00" p="0,00" v="15"/>
      <m s="ΑΣΤΗΡ" l="2,46" c="0,04" p="1,65" v="300"/>
      <m s="ΑΤΕ" l="0,17" c="0,00" p="1,85" v="100"/>
      <m s="ΑΤΤ" l="0,22" c="0,00" p="-0,45" v="1000"/>
      <m s="ΑΤΤΙΚ" l="0,04" c="0,01" p="18,75" v="500"/>
      <m s="ΑΤΤΙΚΑ" l="0,22" c="0,00" p="0,00" v="999"/>
      <m s="ΑΧΟΝ" l="0,11" c="0,02" p="17,78" v="200"/>
      <m s="ΒΑΛΚ" l="0,24" c="-0,01" p="-4,00" v="500"/>
      <m s="ΒΙΟΣΚ" l="0,03" c="0,01" p="19,23" v="981"/>
      <m s="ΒΙΟΧΚ" l="2,60" c="-0,20" p="-7,14" v="19"/>
      <m s="ΒΙΣ" l="1,00" c="0,00" p="0,00" v="350"/>
      <m s="ΒΥΤΕ" l="0,33" c="-0,01" p="-3,85" v="75"/>
      <m s="ΓΑΛΑΞ" l="0,51" c="-0,03" p="-5,38" v="15"/>
      <m s="ΓΕΒΚΑ" l="0,35" c="0,00" p="-0,86" v="20"/>
      <m s="ΓΕΚΤΕΡΝΑ" l="0,56" c="-0,09" p="-13,85" v="1000"/>
      <m s="ΓΤΕ" l="0,20" c="-0,01" p="-5,77" v="2000"/>
      <m s="ΔΑΚΕΤ30" l="6,15" c="-0,03" p="-0,49" v="2"/>
      <m s="ΔΑΠ" l="1.136,13" c="-63,49" p="-5,29" v="165"/>
      <m s="ΔΒΠ" l="2.297,20" c="-100,25" p="-4,18" v="700"/>
      <m s="ΔΕΗ" l="1,97" c="-0,17" p="-7,94" v="500"/>
      <m s="ΔΕΜ" l="785,61" c="-29,77" p="-3,65" v="3399"/>
      <m s="ΔΙΟΝ" l="0,10" c="0,00" p="0,99" v="100"/>
      <m s="ΔΙΧΘ" l="1,05" c="-0,01" p="-0,94" v="983"/>
      <m s="ΔΚΥ" l="1.554,83" c="-26,57" p="-1,68" v="1000"/>
      <m s="ΔΚΩ" l="795,18" c="-64,77" p="-7,53" v="5000"/>
      <m s="ΔΜΚ" l="1.694,50" c="16,12" p="0,96" v="540"/>
      <m s="ΔΟΛ" l="0,16" c="-0,01" p="-5,85" v="4000"/>
      <m s="ΔΟΜΙΚ" l="0,48" c="-0,04" p="-6,80" v="50"/>
      <m s="ΔΠΑ" l="1.940,26" c="-9,90" p="-0,51" v="367"/>
      <m s="ΔΠΟ" l="1.756,28" c="-14,25" p="-0,80" v="4000"/>
      <m s="ΔΠΥ" l="952,47" c="-64,95" p="-6,38" v="2589"/>
      <m s="ΔΡΟΜΕ" l="0,16" c="-0,04" p="-18,78" v="100"/>
      <m s="ΔΤΑ" l="1.040,17" c="-4,17" p="-0,40" v="3000"/>
      <m s="ΔΤΛ" l="572,53" c="-11,01" p="-1,89" v="3000"/>
      <m s="ΔΤΠ" l="5.156,35" c="-148,90" p="-2,81" v="800"/>
      <m s="ΔΤΡ" l="217,87" c="-23,45" p="-9,72" v="37500"/>
      <m s="ΔΤΧ" l="267,34" c="-12,31" p="-4,40" v="15396"/>
      <m s="ΔΥΓ" l="93,31" c="-7,22" p="-7,18" v="300"/>
      <m s="ΔΧΜ" l="5.093,77" c="-5,04" p="-0,10" v="3"/>
      <m s="ΔΧΥ" l="701,35" c="-43,13" p="-5,79" v="10647"/>
      <m s="ΕΒΖ" l="0,61" c="-0,06" p="-8,96" v="800"/>
      <m s="ΕΒΡΟΦ" l="0,28" c="0,00" p="0,72" v="2"/>
      <m s="ΕΔΡΑ" l="0,06" c="0,00" p="0,00" v="1"/>
      <m s="ΕΔΡΙΠ" l="0,65" c="0,00" p="-0,15" v="3"/>
      <m s="ΕΕΕΚ" l="14,10" c="-0,40" p="-2,76" v="10"/>
      <m s="ΕΚΤΕΡ" l="0,34" c="0,02" p="4,62" v="25"/>
      <m s="ΕΛΒΑ" l="1,01" c="-0,06" p="-5,61" v="2500"/>
      <m s="ΕΛΒΕ" l="0,60" c="0,02" p="3,10" v="800"/>
      <m s="ΕΛΓΕΚ" l="0,22" c="0,01" p="5,24" v="50"/>
      <m s="ΕΛΙΝ" l="0,67" c="0,04" p="6,53" v="1"/>
      <m s="ΕΛΙΧΘ" l="0,06" c="-0,01" p="-15,38" v="5400"/>
      <m s="ΕΛΚΑ" l="1,00" c="-0,09" p="-8,26" v="400"/>
      <m s="ΕΛΛ" l="11,31" c="-0,45" p="-3,83" v="20"/>
      <m s="ΕΛΛΑΚΤΩΡ" l="0,97" c="-0,08" p="-7,24" v="1000"/>
      <m s="ΕΛΠΕ" l="5,17" c="-0,05" p="-0,96" v="260"/>
      <m s="ΕΛΣΤΡ" l="0,45" c="-0,02" p="-4,47" v="800"/>
      <m s="ΕΛΤΚ" l="1,79" c="0,00" p="0,00" v="120"/>
      <m s="ΕΠΙΛΚ" l="0,53" c="0,00" p="-0,75" v="10"/>
      <m s="ΕΤΕ" l="1,42" c="-0,13" p="-8,39" v="3000"/>
      <m s="ΕΤΕΜ" l="0,18" c="-0,02" p="-9,95" v="10"/>
      <m s="ΕΥΑΠΣ" l="3,28" c="-0,22" p="-6,29" v="40"/>
      <m s="ΕΥΔΑΠ" l="3,11" c="-0,22" p="-6,61" v="190"/>
      <m s="ΕΥΠΙΚ" l="0,49" c="0,00" p="0,00" v="50"/>
    
    </tickdata>

    any help appreciate

    thanks

  2. #2
    Join Date
    Oct 2007
    Posts
    371
    What do you want do do with the data from the second xml?

  3. #3
    Join Date
    May 2012
    Posts
    11
    Quote Originally Posted by TecBrat View Post
    What do you want do do with the data from the second xml?
    its is prices of stocks (Athens stock exchange) lot's of things, but basically to make a banner, scroller to provide them from my site with some and keep stat's for some of them..

  4. #4
    Join Date
    May 2012
    Posts
    11
    bump...

  5. #5
    Join Date
    Oct 2007
    Posts
    371
    You can probably do this with DOM like you started, but I've not used it before.

    assuming
    PHP Code:
    $xml simplexml_load_file($url); 
    then see what happens when you try this:
    PHP Code:
    foreach($xml->tickdata->as $stock)
      {
        echo 
    $stock-> l
        echo 
    $stock-> c;
        echo 
    $stock-> p;
        echo 
    $stock-> v;
        echo 
    "<br>\n";
      } 

  6. #6
    Join Date
    May 2012
    Posts
    11
    ty for reply.

    PHP Code:
    <?php

    //error_reporting(0);

    $url"http://www.mysite.com/clients/mydata.xml";
    $xml simplexml_load_file($url);


    foreach(
    $xml->tickdata->as $stock)
      {
        echo 
    $stock-> l;
        echo 
    $stock-> c;
        echo 
    $stock-> p;
        echo 
    $stock-> v;
        echo 
    "<br>\n";
      } 
               
               


    ?>

    Invalid argument supplied for foreach()

  7. #7
    Join Date
    Oct 2007
    Posts
    371
    tickdata was root and I was missing 'attributes()->'
    PHP Code:
    <?php
    $string
    ='<?xml version="1.0" encoding="UTF-8"?>
    <tickdata>
      <t>??a: 17:12</t>
      <m s="G?" l="620,54" c="-23,33" p="-3,62" v="-1"/>
      <m s="FTSE" l="231,83" c="-10,77" p="-4,44" v="-1"/>
      <m s="FTSEA" l="534,97" c="-24,67" p="-4,41" v="-1"/>
      <m s="FTSEM" l="596,28" c="-25,72" p="-4,14" v="-1"/>
      <m s="FTSES" l="161,35" c="-6,51" p="-3,88" v="-1"/>
      <m s="FTSEB" l="163,59" c="-17,36" p="-9,59" v="37500"/>
      <m s="FTSEL" l="533,10" c="-23,88" p="-4,29" v="1000"/>
      <m s="FTSENTR" l="899,33" c="-41,80" p="-4,44" v="40500"/>
      <m s="????" l="0,69" c="-0,06" p="-7,77" v="200"/>
    </tickdata>'
    ;

    $xml simplexml_load_string($string);
    foreach(
    $xml->as $stock)
      {
        echo 
    $stock->attributes()->l.' | ';
        echo 
    $stock->attributes()->c.' | ';
        echo 
    $stock->attributes()->p.' | ';
        echo 
    $stock->attributes()->v.' | ';
        echo 
    "<br>\n";
      }
     
    ?>
    HTML Code:
    620,54 | -23,33 | -3,62 | -1 | <br>
    231,83 | -10,77 | -4,44 | -1 | <br>
    534,97 | -24,67 | -4,41 | -1 | <br>
    596,28 | -25,72 | -4,14 | -1 | <br>
    161,35 | -6,51 | -3,88 | -1 | <br>
    163,59 | -17,36 | -9,59 | 37500 | <br>
    533,10 | -23,88 | -4,29 | 1000 | <br>
    899,33 | -41,80 | -4,44 | 40500 | <br>
    0,69 | -0,06 | -7,77 | 200 | <br>

  8. #8
    Join Date
    May 2012
    Posts
    8
    maybe (just maybe) this can help: http://www.kirupa.com/web/xml_php_parse_beginner.htm

  9. #9
    Join Date
    May 2012
    Posts
    11
    @ TecBrat works fine, thanks

  10. #10
    Join Date
    May 2012
    Posts
    11
    i create a ticker with this data in php, works fine but i am looking a way to update data without refreshing page..... any suggestion? thanks

  11. #11
    Join Date
    Oct 2007
    Posts
    371
    Quote Originally Posted by Chrisad View Post
    i create a ticker with this data in php, works fine but i am looking a way to update data without refreshing page..... any suggestion? thanks
    You probably need a new thread, but the quick answer is AJAX. Google it and you'll be in a whole new world of interactive web development.

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