Thread: Handling XML

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0

    Handling XML


    i am tring to parse XML news from google news. I am getting result but i don't want array to show in my results like displayed below. I want to show in paragraph without array() like we can see in google news. can somebody help me in editing this code. Thank you.

    here is the code:

    PHP Code:
    <?php
    $news 
    simplexml_load_file('https://news.google.com/news/feeds?gl=us&pz=1&cf=all&ned=us&hl=en&q=nepal&output=rss');
    $feeds = array();
    $i 0;

    foreach (
    $news->channel->item as $item
    {
        
    preg_match('@src="([^"]+)"@'$item->description$match);
        
    $parts explode('<font size="-1">'$item->description);

        
    $feeds[$i]['title'] = (string) $item->title;
        
    $feeds[$i]['link'] = (string) $item->link;
        
    $feeds[$i]['image'] = $match[1];
        
    $feeds[$i]['site_title'] = strip_tags($parts[1]);
        
    $feeds[$i]['story'] = strip_tags($parts[2]);

        
    $i++;
    }
    echo 
    '<pre>';
    print_r($feeds);
    echo 
    '</pre>';
    ?>
    This will display result in this form:

    Code:
    Array
    (
        [0] => Array
            (
                [title] => Consensus government the only option in Nepal, says India - The Hindu
                [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNE63DIoE8GUmBts6KmBwei07ORcQQ&url=http://www.thehindu.com/news/national/consensus-government-the-only-option-in-nepal-says-india/article4241229.ece
                [image] => //nt1.ggpht.com/news/tbn/eU4EtWR1knGLfM/6.jpg
                [site_title] => The Hindu
                [story] => India said it has put its faith in Nepal President Ram Baran Yadav's approach of urging all political parties represented in the Constituent Assembly to form a consensus government. Although repeated deadlines have been missed, New Delhi feels this is ...
            )
    
        [1] => Array
            (
                [title] => Nepal to ease visa regime for Chinese nationals - Xinhua
                [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNFLwWZBJ2n55MqM8w3di4w6VQpVrQ&url=http://news.xinhuanet.com/english/travel/2012-12/27/c_132066808.htm
                [image] => 
                [site_title] => Xinhua
                [story] => KATHMANDU, Dec. 27 (Xinhua)-- The government of Nepal is planning to ease visa regime for Nepal-bound Chinese tourists, officials said Thursday. The Ministry of Tourism has submitted a report to this effect to various ministries for approval, officials ...and more 
            )
    
        [2] => Array
            (
                [title] => India will continue to do its best to assist Nepal: President Pranab Mukherjee - Times of India
                [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNE8awPnQbMO47XEWdGLhbaJzDkv_A&url=http://timesofindia.indiatimes.com/india/India-will-continue-to-do-its-best-to-assist-Nepal-President-Pranab-Mukherjee/articleshow/17768188.cms
                [image] => //nt0.ggpht.com/news/tbn/dMxS18sWneJwbM/6.jpg
                [site_title] => Times of India
                [story] => It is in India's interest that Nepal progresses as a stable, secular, democratic republic, the President said welcoming his Nepalese counterpart Ram Baran Yadav at Rashtrapati Bhavan here on Wednesday. Mukherjee said that people of India consider him a ...
            )
    
        [3] => Array
            (
                [title] => Nepal: Mao's philosophy resulted in success of People's war, claims Dahal - Telegraphnepal.com
                [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNGGYjPsu7UEC_fj2hoi3AwMksIn6A&url=http://www.telegraphnepal.com/headline/2012-12-27/nepal:-maos-philosophy-resulted-in-success-of-peoples-war-claims-dahal
                [image] => //nt0.ggpht.com/news/tbn/jOxM2eVyJr8DeM/6.jpg
                [site_title] => Telegraphnepal.com
                [story] => Unity between Nepal's political parties and people could only help resolve troubles confronting the nation, so claims Chairman Pushpa Kamal of Unified Maoists Party. But what about Indian Foreign Minister Salman Khurshid's fresh expression wherein he ...and more 
            )
    
        [4] => Array
            (
                [title] => 'Yak insurance' plan saving Nepal's snow leopard - Phys.Org
                [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNGY1rg0BfENyDNo_vZddq_OVR1RSg&url=http://phys.org/news/2012-12-yak-nepal-leopard.html
                [image] => //nt0.ggpht.com/news/tbn/2FruHZIH_HxYvM/6.jpg
                [site_title] => Phys.Org
                [story] => This handout photograph taken by a remote camera trap and released by the World Wide Fund for Nature (WWF) Nepal shows a rare snow leopard in the Kangchenjunga Conservation Area, 260 kilometres (160 miles) east of the Nepalese capital Kathmandu ...
            )
    
        [5] => Array
            (
                [title] => Nepal: A country of hope - Himalayan Times
                [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNFMoTWRSJstk40rPF9ZIyKYo9ONug&url=http://www.thehimalayantimes.com/fullNews.php?headline%3DNepal%253A%2BA%2Bcountry%2Bof%2Bhope%26NewsID%3D359258
                [image] => 
                [site_title] => Himalayan Times
                [story] => Few months ago while wandering in my hometown Kirtipur, I saw some tourists. I felt they had forgotten their route. So, I approached them to ask if they needed any help. Then, I guided them to different places around Kirtipur. I also asked them how ...
            )
    
        [6] => Array
            (
                [title] => India Promises Support for Nepal Socio-Economic Development - Prensa Latina
                [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNHsHOvY04GE2w6EJV7FBfv5RgSSaA&url=http://www.plenglish.com/index.php?option%3Dcom_content%26task%3Dview%26id%3D841431%26Itemid%3D1
                [image] => 
                [site_title] => Prensa Latina
                [story] => Imagen activa 26 de diciembre de 2012, 10:53New Delhi, Dec 26 (Prensa Latina) India will make every effort to continue to support the socio-economic development of Nepal, President Pranab Mukherjee today told his counterpart in the neighboring nation, ...
            )
    
        [7] => Array
            (
                [title] => Cold snap in Nepal kills 17 in 10 days - Global Times
                [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNHlGjLknFuYJchen_2srC_DOztrFg&url=http://www.globaltimes.cn/content/752403.shtml
                [image] => 
                [site_title] => Global Times
                [story] => Cold wave has killed at least 17 people across the Terai plains of Nepal in the past 10 days, reports from across the country said Wednesday. As of today, seven individuals have died in Mahottari, five in Siraha, four in Rautahat and one in Bara ...and more 
            )
    
        [8] => Array
            (
                [title] => Six persons arrested with explosives in Nepal - Zee News
                [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNFEBRl5OFEmxs4G894L7k12Hflq5Q&url=http://zeenews.india.com/news/south-asia/six-persons-arrested-with-explosives-in-nepal_819103.html
                [image] => 
                [site_title] => Zee News
                [story] => Six persons arrested with explosives in Nepal Kathmandu: Nepal Police has arrested six people, including the woman coordinator of a relatively unknown armed ethnic group, who were allegedly planning to carry out explosions in the capital city. Six ...and more 
            )
    
        [9] => Array
            (
                [title] => The Challenge for Nepal's Migrant Workers - the Diplomat
                [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNElWx0TVAI11SbadT_H5zy_8S7FQg&url=http://thediplomat.com/2012/12/26/the-exploitation-of-nepali-migrant-workers/
                [image] => //nt0.ggpht.com/news/tbn/CLC80vv3WU8WWM/6.jpg
                [site_title] => the Diplomat
                [story] => Last week countries around the world marked International Migrants Day in recognition of the 214 million international migrants on the move across the globe in search of better economic opportunity. Nowhere is this recognition more important than in ...
            )
    
    )
    [[ please wrap long bits of output, like the Array stuff above, in &amp;#91;code][/code] tags --requinix ]]
    Last edited by requinix; December 27th, 2012 at 04:52 AM.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    Print_r is really more of a debugging tool not an output tool. To do what you want you need to format the output yourself. For example:
    PHP Code:
    for ($i=0$i<count($feeds); $i++) {
       echo 
    "Title: <a href=\"".$feeds[$i]['link']."\">".$feeds[$i]['title']."</a><br />";
       .
       .
       .

    Of course you need to format it the way you want but I think that gives you the idea.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    13
    Rep Power
    0
    I like the way your result are but i want to show the result this way.
    PHP Code:
    for ($i=0$i<count($feeds); $i++) { 
       
    $description $news->channel->item->description;
       echo 
    $description;

    But, it is just displaying only one news repeatedly.
    Results are all repeating. There is something missing in my code.
    Below is the result:

    Aaj Ki Khabar

    Nepal's exports to China surged four-fold in current fiscal
    Daily News & Analysis
    Nepal's exports to China have surged four-fold over the first four months of the current fiscal year, Xinhua reported Thursday. However, the surge in exports remains insignificant compared to the country's imports from the northern neighbour resulting ...
    Nepal to ease visa regime for Chinese nationalsZee News

    all 17 news articles

    Aaj Ki Khabar

    Nepal's exports to China surged four-fold in current fiscal
    Daily News & Analysis
    Nepal's exports to China have surged four-fold over the first four months of the current fiscal year, Xinhua reported Thursday. However, the surge in exports remains insignificant compared to the country's imports from the northern neighbour resulting ...
    Nepal to ease visa regime for Chinese nationalsZee News

    all 17 news articles
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    Are you asking an HTML formatting question or a PHP question? If the latter, post a sample of HTML you want. If the former then you need to be more explicit as to what you want the HTML to look like and probably post it on the HTML forum.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo