Jump to content

Recommended Posts

Howdy! This code was taken from another site and is open source. I'm trying to use it to parse an xml file with oplm attributes and display it on a web page. On line 29 of the parser.php file there is a parameter for the getContents function that I think needs the url to the ocw.xml file. That file is located in the same directory and I insert the file name here. Then on my index.php page I use include once php method to call the parser.php file. Here is the code to the parser.php file. I may be approaching this in the wrong way and I would appreciate some direction.

  <?php 
class IAM_OPML_Parser 
{ 
     var $parser; 
     var $data; 
     var $index = 0; 

    // Outline items we wish to map and their mapping names:  link_url, link_name, link_target, link_description... 
    var $opml_map_vars = array('URL' => 'link_url', 'HTMLURL' => 'link_url', 'TEXT' => 'link_name', 'TITLE' => 'link_name', 
                                'TARGET' => 'link_target','DESCRIPTION' => 'link_description', 'XMLURL' => 'link_rss', 
                                "CREATED"=>'created', 'TYPE'=>'type'); 

     function OPML_Parser() 
     { 
          $this->parser = null; 
          $this->data = ''; 
     } 

    /** 
     * IAM_OPML_Parser::getContent() 
     * Fetch Contents of Page (from URL). 
     * 
     * @param string $url 
     * @return string contents of the page at $url 
     */ 
    function getContent($url=ocw.xml') 
    { 
        $html = @file_get_contents($url); 
        return $html; 
    } 

     function ParseElementStart($parser, $tagName, $attrs) 
     { 
        $map = $this->opml_map_vars; 
        if ($tagName == 'OUTLINE') 
        { 
              foreach (array_keys($this->opml_map_vars) as $key) 
            { 
                   if (isset($attrs[$key])) 
                { 
                        $$map[$key] = $attrs[$key]; 
                   } 
              } 
              // save the data away. 
              $this->data[$this->index][names] = $link_name; 
              $this->data[$this->index][urls] = $link_url; 
              $this->data[$this->index][targets] = $link_target; 
              $this->data[$this->index][feeds] = $link_rss; 
              $this->data[$this->index][descriptions] = $link_description; 
              $this->data[$this->index][created] = $created; 
              $this->data[$this->index][type] = $type; 
              $this->index++; 
         } // end if outline 
     } 

    function ParseElementEnd($parser, $name) 
     { 
         // nothing to do. 
     } 

    function ParseElementCharData($parser, $name) 
     { 
         // nothing to do. 
     } 

     function Parse($XMLdata) 
     { 
          $this->parser = xml_parser_create(); 
          xml_set_object($this->parser, $this); 

          xml_set_element_handler($this->parser, 
               array(&$this, 'ParseElementStart'), 
               array(&$this, 'ParseElementEnd')); 

        xml_set_character_data_handler($this->parser, 
            array(&$this, 'ParseElementCharData')); 

          xml_parse($this->parser, $XMLdata); 

          xml_parser_free($this->parser); 

     } 


    function list_contents($arrayname, $tab = "        ", $indent = 0) // recursively displays contents of the array and sub-arrays: 
    { 
         // This function (c) Peter Kionga-Kamau (http://www.pmkmedia.com) 
         // Free for unrestricted use, except sale - do not resell. 
         // use: echo LIST_CONTENTS(array $arrayname, string $tab, int $indent); 
         // $tab = string to use as a tab, $indent = number of tabs to indent result 
         while (list($key, $value) = each($arrayname)) 
         { 
              for($i = 0; $i < $indent; $i++) $currenttab .= $tab; 
              if (is_array($value)) 
              { 
                   $retval .= "$currenttab$key : Array: <BR>$currenttab{<BR>"; 
                   $retval .= $this->list_contents($value, $tab, $indent + 1) . "$currenttab}<BR>"; 
              } 
              else 
                $retval .= "$currenttab$key => $value<BR>"; 

              $currenttab = null; 
         } 
         return $retval; 
    } 

     function getFeeds($opml_url) 
     { 
        $this->index = 0; 
        $this->Parse($this->getContent($opml_url)); 
        $this->index = 0; 
        return $this->data; 
    } 

    function displayOPMLContents($opml_url, $tab = "        ") 
    { 
        echo $this->list_contents($this->getFeeds($opml_url), $tab, 0); 
    } 

} 
?> 

I'm testing on a wamp server localy

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/174669-opml-parser/
Share on other sites

Hi,

What's the problem ?

if you stuck with its usage, I would say your index.php should have the following code.

 

include "parser.php";

$parser = new IAM_OPML_Parser();

$parser->displayOPMLContents('ocw.xml');

 

however your need to change line 29 back,

probably to

function getContent($url='') 

Link to comment
https://forums.phpfreaks.com/topic/174669-opml-parser/#findComment-920526
Share on other sites

Thanks MadTechie. I made the changes you suggested and this is what was rendered In FF and IE.

 

Warning: Variable passed to each() is not an array or object in C:\wamp\www\ocw\parser.php on line 91

 

Notice: Undefined variable: retval in C:\wamp\www\ocw\parser.php on line 104.

 

I placed the code you suggested in the body, nested between "<?php ?>" tags.

 

I know very little about this type of php usage. Maybe I'm mucking up the placement.

Thanks again for your help.

 

Link to comment
https://forums.phpfreaks.com/topic/174669-opml-parser/#findComment-920812
Share on other sites

Okay i think i found the problem..

the ocw.txt (opml) file is not valid

 

i have attached a valid one

this is your file

  <?xml version="1.0" encoding="UTF-8" ?> 
- <opml version="1.1">
- <head>
  <title>OpenLearn RSS Feeds</title> 
  <ownerName>The Open University</ownerName> 
  <dateCreated>Thu, 17 Sep 2009 09:51:24 BST</dateCreated> 
  </head>
- <body>
- <outline text="Science and Nature">

 

heres mine

<?xml version="1.0" encoding="UTF-8" ?>
<opml version="1.1">
<head>
<title>OpenLearn RSS Feeds</title>
<ownerName>The Open University</ownerName>
<dateCreated>Thu, 17 Sep 2009 09:51:24 BST</dateCreated>
</head>
<body>
<outline text="Science and Nature">

 

basically i removed the - and spaced at the start of the line,

I have added a extra line of code to clean your files up,

 

function getContent($url='')
{
	$html = @file_get_contents($url);
	$html = preg_replace('/^[\s-]*(.*?)[\s-]*$/sm', '\1', $html); //clean up
	return $html;
}

however i would highly recommend getting a valid file

 

hope that helps

 

[attachment deleted by admin]

Link to comment
https://forums.phpfreaks.com/topic/174669-opml-parser/#findComment-921018
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.