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
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..
Link to comment
Share on other sites

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]
Link to comment
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.