<?
/*
    This work is in the public domain:
    SEE: http://creativecommons.org/licenses/publicdomain/
*/
    
function XMLParser($data_source$data_source_type='raw'$collapse_dups=0$index_numeric=0) {
        
$data '';
        if (
$data_source_type == 'raw')
            
$data $data_source;

        elseif (
$data_source_type == 'stream') {
            while (!
feof($data_source))
                
$data .= fread($data_source1000);

        
// try filename, then if that fails...
        
} elseif (file_exists($data_source))
            
$data implode(''file($data_source));

        
// try url
        
else {
            
$fp fopen($data_source,'r');
            while (!
feof($fp))
                
$data .= fread($fp1000);
            
fclose($fp);
        }
    return 
$data;
    }

    
// Parse the XML file into a verbose, flat array struct.
    // Then, coerce that into a simple nested array.
    
function getTree($data) {
        
$parser xml_parser_create('ISO-8859-1');
        
xml_parser_set_option($parserXML_OPTION_SKIP_WHITE1);
        
xml_parse_into_struct($parser$data$vals$index);
        
xml_parser_free($parser);

        
$i = -1;
        return 
getchildren($vals$i);
    }

    
// internal function: build a node of the tree
    
function buildtag($thisvals$vals, &$i$type) {

        if (isset(
$thisvals['attributes']))
            
$tag['ATTRIBUTES'] = $thisvals['attributes'];

        
// complete tag, just return it for storage in array
        
if ($type === 'complete')
            
$tag['VALUE'] = $thisvals['value'];

        
// open tag, recurse
        
else
            
$tag array_merge($taggetchildren($vals$i));

        return 
$tag;
    }

    
// internal function: build an nested array representing children
    
function getchildren($vals, &$i) {
        
$children = array();     // Contains node data

        // Node has CDATA before it's children
                
if ($i > -&& isset($vals[$i]['value']))
            
$children['VALUE'] = $vals[$i]['value'];

        
// Loop through children, until hit close tag or run out of tags
        
while (++$i count($vals)) {

            
$type $vals[$i]['type'];

            
// 'cdata':    Node has CDATA after one of it's children
            //         (Add to cdata found before in this case)
            
if ($type === 'cdata')
                
$children['VALUE'] .= $vals[$i]['value'];

            
// 'complete':    At end of current branch
            // 'open':    Node has children, recurse
            
elseif ($type === 'complete' || $type === 'open') {
                
$tag buildtag($vals[$i], $vals$i$type);
                if (
$index_numeric) {
                    
$tag['TAG'] = $vals[$i]['tag'];
                    
$children[] = $tag;
                } else
                    
$children[$vals[$i]['tag']][] = $tag;
            }

            
// 'close:    End of node, return collected data
            //        Do not increment $i or nodes disappear!
            
elseif ($type === 'close')
                break;
        }
        if (
$collapse_dups)
            foreach(
$children as $key => $value)
                if (
is_array($value) && (count($value) == 1))
                    
$children[$key] = $value[0];
        return 
$children;
    }

#http://dannyayers.com/misc/foaf/rel3.rdf
#http://www.brain-stream.com/xml/foaf.rdf
# http://rdfweb.org/people/danbri/rdfweb/danbri-foaf.rdf

  
$parser XMLParser('http://www.wasab.dk/morten/blog/archives/author/mortenf/foaf.rdf''url'0);#schema/rdf-schema
 
  
$tree getTree($parser);

  
$personName $tree['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:NAME'][0]['VALUE'];
  
$personNick $tree['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:NICK'][0]['VALUE']; 
  
$personSha1 $tree['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:MBOX_SHA1SUM'][0]['VALUE'];
  
$personHomePage $tree['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:HOMEPAGE'][0]['ATTRIBUTES']['RDF:RESOURCE'];
  
$personImage $tree['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:DEPICTION'][0]['ATTRIBUTES']['RDF:RESOURCE'];

  while(list(
$key,$val)=each($tree['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:KNOWS'])){
    
$knows[$key]['personName'] = $val['FOAF:PERSON'][0]['FOAF:NAME'][0]['VALUE'];
    
$knows[$key]['personSha1'] = $val['FOAF:PERSON'][0]['FOAF:MBOX_SHA1SUM'][0]['VALUE'];
  }

  
#print_r($tree['RDF:RDF'][0]['FOAF:PERSON'][0]['FOAF:KNOWS']);
  
echo "<a href='parse.phps'>source</a><br>";
  echo 
"<pre>";
  echo 
"personName = $personName\n";
  echo 
"personNick = $personNick\n";
  echo 
"personSha1 = $personSha1\n";
  echo 
"personHomePage = $personHomePage\n";
  echo 
"personImage = $personImage\n";
  echo 
"<br><b>KNOWS ARRAY</b>\n";
  
print_r($knows);
  echo 
"<br><b>based on</b>";
  
print_r($tree);
  
#$test = implode(":", $tree);
  #print_r($test);
/*
  function combiner($arr){
    while(list($key,$val)=each($arr)){
        if(is_array($val)){
            $ret .= combiner($val);
        }else{
            $ret .= $key."^".$val." ";
        }
    }
    return $ret;
  }
  reset($tree); 
  while(list($key,$val)=each($tree['RDF:RDF'][0])){
      echo combiner($val)."<br>\n";
  }
*/
?>