Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

xml & php

Posted on 2005-05-04
3
Medium Priority
?
1,143 Views
Last Modified: 2013-11-19
Can someone explain how I can get the title and urls from this
http://www.gigablast.com/search?n=20&s=0&plus=sweet%E2%88%92=forum&sc=0&dr=0&raw=8

useing this script

<?php
###################################################################################
#
# XML Library, by Keith Devens, version 1.2b
# http://keithdevens.com/software/phpxml
#
# This code is Open Source, released under terms similar to the Artistic License.
# Read the license at http://keithdevens.com/software/license
#
###################################################################################

###################################################################################
# XML_unserialize: takes raw XML as a parameter (a string)
# and returns an equivalent PHP data structure
###################################################################################
function & XML_unserialize(&$xml){
      $xml_parser = &new XML();
      $data = &$xml_parser->parse($xml);
      $xml_parser->destruct();
      return $data;
}
###################################################################################
# XML_serialize: serializes any PHP data structure into XML
# Takes one parameter: the data to serialize. Must be an array.
###################################################################################
function & XML_serialize(&$data, $level = 0, $prior_key = NULL){
      if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
      while(list($key, $value) = each($data))
            if(!strpos($key, ' attr')) #if it's not an attribute
                  #we don't treat attributes by themselves, so for an empty element
                  # that has attributes you still need to set the element to NULL

                  if(is_array($value) and array_key_exists(0, $value)){
                        XML_serialize($value, $level, $key);
                  }else{
                        $tag = $prior_key ? $prior_key : $key;
                        echo str_repeat("\t", $level),'<',$tag;
                        if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
                              while(list($attr_name, $attr_value) = each($data["$key attr"]))
                                    echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
                              reset($data["$key attr"]);
                        }

                        if(is_null($value)) echo " />\n";
                        elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
                        else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
                  }
      reset($data);
      if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
}
###################################################################################
# XML class: utility class to be used with PHP's XML handling functions
###################################################################################
class XML{
      var $parser;   #a reference to the XML parser
      var $document; #the entire XML structure built up so far
      var $parent;   #a pointer to the current parent - the parent will be an array
      var $stack;    #a stack of the most recent parent at each nesting level
      var $last_opened_tag; #keeps track of the last tag opened.

      function XML(){
             $this->parser = &xml_parser_create();
            xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);
            xml_set_object(&$this->parser, &$this);
            xml_set_element_handler(&$this->parser, 'open','close');
            xml_set_character_data_handler(&$this->parser, 'data');
      }
      function destruct(){ xml_parser_free(&$this->parser); }
      function & parse(&$data){
            $this->document = array();
            $this->stack    = array();
            $this->parent   = &$this->document;
            return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;
      }
      function open(&$parser, $tag, $attributes){
            $this->data = ''; #stores temporary cdata
            $this->last_opened_tag = $tag;
            if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
                  if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
                        #this is the third or later instance of $tag we've come across
                        $key = count_numeric_items($this->parent[$tag]);
                  }else{
                        #this is the second instance of $tag that we've seen. shift around
                        if(array_key_exists("$tag attr",$this->parent)){
                              $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
                              unset($this->parent["$tag attr"]);
                        }else{
                              $arr = array(&$this->parent[$tag]);
                        }
                        $this->parent[$tag] = &$arr;
                        $key = 1;
                  }
                  $this->parent = &$this->parent[$tag];
            }else{
                  $key = $tag;
            }
            if($attributes) $this->parent["$key attr"] = $attributes;
            $this->parent  = &$this->parent[$key];
            $this->stack[] = &$this->parent;
      }
      function data(&$parser, $data){
            if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
                  $this->data .= $data;
      }
      function close(&$parser, $tag){
            if($this->last_opened_tag == $tag){
                  $this->parent = $this->data;
                  $this->last_opened_tag = NULL;
            }
            array_pop($this->stack);
            if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
      }
}
function count_numeric_items(&$array){
      return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
}
?>
0
Comment
Question by:davidspan
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Promethyl
ID: 13932881
Something like:

$xm = new XML;

echo $xml->XML_unserialize(&readfile("http://www.gigablast.com/search?n=20&s=0&plus=sweet%E2%88%92=forum&sc=0&dr=0&raw=8"));

Something like that... Not sure if that's right.

0
 
LVL 3

Expert Comment

by:gerodim
ID: 13948751
It gave me an error when I checked it out... However i recommend using Php5 and the simplexml funnction
0
 
LVL 7

Accepted Solution

by:
Promethyl earned 2000 total points
ID: 13949211
echo XML_unserialize(&readfile("http://www.gigablast.com/search?n=20&s=0&plus=sweet%E2%88%92=forum&sc=0&dr=0&raw=8"));

But this didn't work.

I think it's the nested elements that get you. Most of the RSS/XML implementations assume an element depth of 1, wherein your XML you want is 2.

This worked for me:

<?php
if ($showsource) show_source('xml.php');
global $xmloutpt, $xmlsilent; // , $intXMLCount
$xmlnow = 1;
$xmloutpt = '';
$xmlsilent = 0;

$XML_Sites = Array (
Array (0,0), // Blank first item in case we only want to import one.
Array('WTFE','http://www.gigablast.com/search?n=20&s=0&plus=sweet%E2%88%92=forum&sc=0&dr=0&raw=8')
);

$insideitem = false;
$tag = "";
$title = "";
$description = "";
$url = "";
$intXMLCount=0;

function startElement($parser, $name, $attrs) {
    global $insideitem, $tag, $title, $description, $url;
    if ($insideitem) {
        $tag = $name;
        echo "INSIDE ITEM TAG: $tag. \n";
    } elseif ($name == "ITEM" or $name=='RESULT') {
        $insideitem = true;
    } elseif ($name == "URL" or $name=='TITLE') {
        $insideitem = true;

    } else {
        echo $name . "\n";
    }
}

function endElement($parser, $name) {
    global $insideitem, $tag, $title, $description, $url, $xmloutpt, $xmlsilent, $intXMLCount;
    if ($name == "ITEM" or $name=='RESULT') {
        $intXMLCount++;
        If ($intXMLCount < 6) {
            $xmloutpt .= "-<a target=\"_blank\" href=\"" . trim($url) . "\">".
                htmlspecialchars(trim($title))."</a><br/>\n";
        }
        if (!$xmlsilent) { echo  $xmloutput; }
        $title = "";
        $description = "";
        $url = "";
        $insideitem = false;
    }
}

function characterData($parser, $data) {
    global $insideitem, $tag, $title, $description, $url;
    if ($insideitem) {
    echo $tag;
    switch ($tag) {
        case "TITLE":
        $title .= $data;
        break;
        case "DESCRIPTION":
        $description .= $data;
        break;
        case "URL":
        $url .= $data;
        break;
    }
    }
}

$difference = date('U') - date('U',filectime('/home/proorg/www/php/inc/blocks/remoteposts.htm'));
$difference = ($difference / 60 / 60);

if ($difference > .98 or $xmlnow) {

Echo "<!-- XML Parse; I want to generate files. -->\n"; $wanttogenerateXML = 1;

$fp_xml =  fopen('remoteposts.htm', 'w+');

foreach ($XML_Sites as $XML_Site) {
    if (!$XML_Site[0]) { Continue; }
    $intXMLCount=0;
   
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");

    $fp = fopen("$XML_Site[1]","r");
    if (!$fp) { Continue; }
    while ($data = fread($fp, 4096))
        xml_parse($xml_parser, $data, feof($fp))
                or $xml_err.=(sprintf("XML error: %s at line %d",
                xml_error_string(xml_get_error_code($xml_parser)),
            xml_get_current_line_number($xml_parser)));
    fclose($fp);
    xml_parser_free($xml_parser);

        if ($fp_xml) {
        $xmloutpt = strip_tags(html_entity_decode($xmloutpt),"<a><br><br/>");
            fwrite($fp_xml, $str_xml_brk. '<b>'. $XML_Site[0].'</b><br/>'. $xmloutpt . '<!--'.$xml_err.'-->'."\n");
        $xmloutpt = ''; $intxmlnumprocessed++; $str_xml_brk = '<br/>';
            if (!$xmlsilent) { echo "<!-- Wrote $XML_Site[0] on ".Date('r').". Cached info $difference hours aged. -->\n"; }

        } else {
            if (!$xmlsilent) { echo "<!-- WARNING: Unable to write $XML_Site[0] on ".Date('r')." -->\n"; }
        }

}

fclose($fp_xml);

} else {  if (!$xmlsilent) { echo "\n<!-- I do not want to parse XML. Cached info $difference hours aged. -->\n"; } }  
?>
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question