PHP and XML Newbie

Associate
Joined
21 Sep 2007
Posts
453
I am looking to learn some basic PHP and XML functions, for making a control panel for a Flash banner.

Currently I am using SQL to store some variables, but I wish to change this to XML.

I have started looking at the tutorials on Kirupa.com but i can only Just get my head around the basic XML tutorial.

I can make a page that will display the XML data, but the next task is to figure out how to inject the data (a couple of numbers and maybe some text) into my previous PHP code e.g. <? echo $fade; ?> (fade being an integer)

My XML file would be:

Code:
<?xml version="1.0"?> 
    <variables>
        <fade>1</fade> 
        <holdLength>5</holdLength> 
</variables>

Beyond displaying them, I am lost. For what i need, they do not need displaying, but simply passed into the PHP code shown.

Links to good, easy to understand XML tutorials are very welcome!
 
me again lol..

mySQL to XML using PHP:
http://www.kirupa.com/web/mysql_xml_php.htm

XML in Flash:
http://www.kirupa.com/web/xml/XMLwithFlash3.htm

When you get the .php to output the data in the format you posted earlier.
Code:
<?xml version="1.0"?> 
    <variables>
        <fade>1</fade> 
        <holdLength>5</holdLength> 
</variables>

Example AS:
Code:
var nfadeXml:Number;
var nHoldLengthXml:Number;

var dbXML:XML = new XML();
dbXML.ignoreWhite = true;
dbXML.onLoad = function(success:Boolean) {
	if (success == true) {
		var aRootXML:Array = this.firstChild.childNodes;
		nfadeXml = aRootXML[0].childNodes[0].nodeValue;
		nHoldLengthXml= aRootXML[1].childNodes[0].nodeValue;
	} else {
		//xml not found, set defaults 
		nfadeXml = 2;
		nHoldLengthXml= 4;
	}
	trace ("fade:"+ nfadeXml +" holdLength:"+ nHoldLengthXml);
};
dbXML.load("[B]test.php[/B]");

PS once you get to grips with this, you'll never use flashvars again..
 
Last edited:
http://uk3.php.net/dom

to create the small bit of code you're after:

Code:
$dom = new DOMDocument('1.0','UTF-8');
$variables = $dom->createElement('variables');
$fade = $dom->createElement('fade',1);
$holdLength = $dom->createElement('holdLength',5);
$variables->appendChild($fade);
$variables->appendChild($holdLength);
$dom->appendChild($variables,true);
echo $dom->saveXML();

that'll do the code bit you posted. the php docs are pretty damn good for dom stuff. take a look at xpath, too because that's nearly invaluable for things like this
 
Thanks for the replies folks, but I managed to figure it out after carefully going through one of the Kirupa tutorials.

And the solution was much cleaner then what you have posted :confused:

Hector - I have no problems with FlashVars, why do you hate them so? Also, you keep insisting they don't work in IE6, when they clearly do :confused:

Here is the PHP code i used in the end:

Code:
$file = "flash.xml";

$xml_fade_key = "*VARIABLES*FADE";
$xml_holdLength_key = "*VARIABLES*HOLDLENGTH";

function startTag($parser, $data){

	global $current_tag;
	$current_tag .= "*$data";
}

function endTag($parser, $data){

	global $current_tag;

	$tag_key = strrpos($current_tag, '*');

	$current_tag = substr($current_tag, 0, $tag_key);

}

function contents($parser, $data){

	global $current_tag, $xml_fade_key, $xml_holdLength_key, $fade, $holdLength;
	switch($current_tag){
		case $xml_fade_key:
			$fade = $data;
			break;
		case $xml_holdLength_key:
			$holdLength = $data;
			break;

	}

}

$xml_parser = xml_parser_create();

xml_set_element_handler($xml_parser, "startTag", "endTag");

xml_set_character_data_handler($xml_parser, "contents");

$fp = fopen($file, "r");

$data = fread($fp, 80000);

if(!(xml_parse($xml_parser, $data, feof($fp)))){
    die("Error on line " . xml_get_current_line_number($xml_parser));
}

xml_parser_free($xml_parser);

fclose($fp);

Simple huh?
 
I don't see that's as simple as what I posted by a long shot, but whatever works for you. I also favour the OO approach with DOM and XPath to the procedural xml parser stuff.
 
Sic - I can look through the code i posted, and see how it works, looking at what you posted, I have no clue where it should so, or how it works. Hence I view my code as simpler. Remember I said I am totally new to this.

Thankyou for your input eitherway
 
i don't hate flashVars, they may be okay for small jobs but i just don't use them for a number of reasons. main one being that i mostly make local applications, .exe's.

but another example would be that you can create an .xml simulating the expected output from sql, then you can trace the data with-in flash, making location of the vars that much easier during development. when done, just change the filename from blah.xml to the .php that does the deed with the database.. the AS remains the same.

without adding the param string, my tests failed in IE on both my pc and my brother's. infact the param alone worked without FlashVars on our IE's..
 
has anyone got an links for help on parsing XML with PHP

i've been looking into parsing my last.fm xml pages into some websites but don't really know where to start with XML

thanks
 
Back
Top Bottom