Jump to content

I need some help with two things.


TJMAudio

Recommended Posts

Alright, I am decently skilled with PHP, however I am trying to learn some of the more advanced programming methods.  What I am trying to learn now is XML parsing and OOP. 

Question 1:

What is the point of setting vars in a class beforehand?  Do they set up the $this->var for the functions?  I cannot find a very good tutorial for classes, they are all either too simple, or too advanced.

Question 2:
Is there a simple way to parse an xml file?  I keep finding really, really in-depth huge classes that are multi-function used to process XML files, such as news feeds, etc.  Are these huge classes required?  Again, I can't find a good tutorial for these.

Thanks guys.
Link to comment
https://forums.phpfreaks.com/topic/31560-i-need-some-help-with-two-things/
Share on other sites

[quote author=thorpe link=topic=119599.msg490014#msg490014 date=1166762415]
1/ Sometimes it comes in handy to instantiate an object with some default values.

2/ Look at the example sin the manual entry for the [url=http://php.net/simplexml]simplexml[/url] extension. They don't get much simpler.
[/quote]
I don't understand them even more now..  People's tutorials I look at have them set like this:
[code]
var $name;
var $time;
var $date;
[/code]
They don't have any values set..
Here is my XML class..

[code]
<?php
/*
* Created on Sep 6, 2006 by keeb
*
* Description:
*
*/

class xml {

private $url;
public $data;
private $vals;
private $debug = false;

public function __construct($url) {
$this->url  = $url;
$xml = file_get_contents($url);
$data = $this->xml2array($xml);
$this->data = $data;
if ($this->debug) {
print "<pre>";
print_r($data);
}
}

private function &last(&$array){
if (!count($array))
return null;
end($array);
return $array[key($array)];
}

private function parsexml(&$vals, &$dom, &$lev){
do {
$curr = current($vals);
$lev = $curr['level'];
$tag = strtolower($curr['tag']);
switch ($curr['type'])
{
case 'open':
if (!isset($dom[$tag]))
$dom[$tag] = array();
array_push($dom[$tag], array());
$new =& $this->last($dom[$tag]);
next($vals);
$this->parsexml(&$vals, $new, $lev);
break;

case 'cdata':
break;

case 'complete':
if (!isset($dom[$tag]))
$dom[$tag] = $curr['value'];
else
array_push($dom[$tag] , $curr['value']);
break;

case 'close':
return;
}
}
while (next($vals)!==FALSE);
}

private function xml2array($xml){
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $xml, $vals);
xml_parser_free($xml_parser);
reset($vals);
$dom = array();
$lev = 0;
$this->parsexml($vals, $dom, $lev);
return $dom;
}

}

?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.