SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Parsing XML with PHP

    I want to extract the <text></text> parts of the xml output of
    an api into an array then get the actual text out, I wrote something
    like this :

    Code:
    $apiurl=urlencode("http://api.preachingcentral.com/bible.php?passage=$passage&version=$version");
    $data = file_get_contents('$apiurl');
    $bible = new SimpleXMLElement($data,0,true);
    $dtext=array();
    foreach ($bible as $item) 
    { 
      $dtext[]=$item->text;
     }
    It didn't work, no message whatsoever displayed.

    Can someone please help to rectify the code.

    The api url is as follows, you can test with $passage=Genesis1:1-2&version=kjv

    http://api.preachingcentral.com/bible.php?passage=$passage&version=$version

    Thanks.

  2. #2
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    json_encode/decode are great functions to transform XML into associative arrays.

    PHP Code:
    <?php
    $passage
    ='Genesis1:1-2';
    $version='kjv';
    $url "http://api.preachingcentral.com/bible.php?passage=$passage&version=$version";
    $xml = new SimpleXMLElement(file_get_contents($url));
    $data json_decode(json_encode($xml), true);

    // Print the text portions you want to extract
    foreach ($data['range']['item'] as $verse)
    {
        echo 
    "<div>Book: {$verse['bookname']}, Chapter: {$verse['chapter']}, Verse: {$verse['verse']}<p>{$verse['text']}</p></div>\n";
    }

    // Print the structure of the entire array .... for your reference
    echo "<pre>";
    print_r($data);
    echo 
    "</pre>";
    ?>

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    163
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    json_encode/decode are great functions to transform XML into associative arrays.

    PHP Code:
    <?php
    $passage
    ='Genesis1:1-2';
    $version='kjv';
    $url "http://api.preachingcentral.com/bible.php?passage=$passage&version=$version";
    $xml = new SimpleXMLElement(file_get_contents($url));
    $data json_decode(json_encode($xml), true);

    // Print the text portions you want to extract
    foreach ($data['range']['item'] as $verse)
    {
        echo 
    "<div>Book: {$verse['bookname']}, Chapter: {$verse['chapter']}, Verse: {$verse['verse']}<p>{$verse['text']}</p></div>\n";
    }

    // Print the structure of the entire array .... for your reference
    echo "<pre>";
    print_r($data);
    echo 
    "</pre>";
    ?>

    Thanks bro, it works fine, looks like you are an expert on json.

  4. #4
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    JSON is rapidly becoming a standard way to transmit key/value data over the web, so support for it has been growing in PHP and other languages.

    Up until PHP had the json functions, I always made my own custom XML parser to transform XML to arrays ... my life was made infinitely easier when I discovered those two functions. Not only is it an easy way to transform XML, but it will do the same for objects and possibly other formats.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    json_encode/decode are great functions to transform XML into associative arrays.
    Don't do this, it a waste of time and effort. You were very close originally, see the comments within your code below.

    PHP Code:
    // 1. No need to urlencode() the whole URL, in fact this breaks everything.
    $apiurl urlencode("http://api.preachingcentral.com/bible.php?passage=$passage&version=$version");
    // 2. No need to file_get_contents() as SimpleXML can read URLs.
    //    In fact the 'true' says you are passing in a URL.
    $data file_get_contents('$apiurl');
    $bible = new SimpleXMLElement($data,0,true);
    $dtext=array();
    // 3. Here $bible is the <bible> XML element.
    //    Looping over $bible will loop over only its immediate children.
    //    These are the <title>, <range>, <cache> and <time> elements.
    //    You want to loop over the <item> elements which are held
    //    within the <range> element, within the <bible> element.
    foreach ($bible as $item

      
    $dtext[]=$item->text;
     } 
    Here is a similar script doing what you want, that works; and without the crazy JSON encode/decode.

    PHP Code:
    // Properly build the URL, only encoding the query string.
    $parameters = array("passage" => $passage"version" => $version);
    $apiurl     "http://api.preachingcentral.com/bible.php?" http_build_query($parameters);

    // Fetch the XML from $apiurl.
    $bible = new SimpleXMLElement($apiurlnullTRUE);

    $dtext = array();
    foreach (
    $bible->range->item as $item) {
        
    $dtext[] = (string) $item->text;

    See this example running online.
    Salathe
    Software Developer and PHP Manual Author.


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
  •