Jump to content

I need help using XML with PHP


programmer-in-training

Recommended Posts

Umm... if your XML was something like this

<items>	<item>		<download_mirror1>http://www.wanted_domain.com</download_mirror1>	</item></items>

You could do

$items = new DOMDocument();$items->load("items.xml");$mirror = $items->getElementsByTagName("item")->item(0)->getElementsByTagName("download_mirror1")->item(0)->nodeValue;header("location:$mirror");

I've never used DOMDocument before though so it may not work.

Link to comment
Share on other sites

header("location:$mirror");

Is the $mirror supposed to be inside the quotes? I thought it would be header("Location:" . $mirror)Instead of "getElementsByTagName("item")->item(0)", can I do "getElementsByTagName("item")(0)" like in other languages?(Or should it be (1) instead of (0)?)I seem to be having trouble with this line:
$mirror = $items->getElementsByTagName("item")->item(0)->getElementsByTagName("download_mirror1")->item(0)->nodeValue;

Link to comment
Share on other sites

example file:

<items>	<item>		<download_mirror1>link</download_mirror1>		<download_mirror2>link 2</download_mirror2>	</item>	<item>		<download_mirror1>other link</download_mirror1>		<download_mirror2>other link 2</download_mirror2>	</item></items>

then use in php:

	$dom	= new DOMDocument;	$dom->load('the example file.xml');		$items	= $dom->getElementsByTagName('item');	foreach($items as $item){		$mirror1[]	= $item->getElementsByTagName('download_mirror1')->item(0)->nodeValue;		$mirror2[]	= $item->getElementsByTagName('download_mirror2')->item(0)->nodeValue;	}		print_r($mirror1);	print_r($mirror2);

something like that should work, you can easily remoe the $mirror2 thing if you dont need those

Link to comment
Share on other sites

This is my entire file:

<?php	$dom = new DOMDocument;	$dom->load('http://schoolsareforfish.net/index.xml');		$items = $dom->getElementsByTagName('item');	foreach($items as $item){		$mirror1[] = $item->getElementsByTagName('download_mirror1')->item(0)->nodeValue;	}		print_r($mirror1);?>

Nothing happens. (How can I debug PHP? Either things work or nothing happens at all.)

Link to comment
Share on other sites

strange, if i use that exact code it gives me:

Array(	[0] => http://w1.wikisend.com:8080/WinLogAssist.zip	[1] => http://w1.wikisend.com:8080/YouTubeGet.zip	[2] => http://w1.wikisend.com:8080/LeaderTask.zip	[3] => http://w1.wikisend.com:8080/GeneralKnowledgeBase.zip	[4] => http://w1.wikisend.com:8080/CaptainTrayPro.zip	[5] => http://w1.wikisend.com:8080/SeaVoyage3DScreensaver.zip	[6] => http://w1.wikisend.com:8080/ImageBadgerDeluxe.zip	[7] => http://w1.wikisend.com:8080/MultiSet.zip	[8] => http://w1.wikisend.com:8080/TNTScreenCapture.zip	[9] => http://w1.wikisend.com:8080/RecentX.zip)

can u try putting:ini_set('error_reporting',E_ALL|E_STRICT);ini_set('display_errors',true);at the top of the page? right after the <?php open tag, does it still not give any errors?

Link to comment
Share on other sites

i just noticed in the php site that some methods only work in php5, i recommend u to upgrade to php5, php4 wont get any new bug fixes, and theyre already working on php6, but if you have to keep working with php 4 try thisreplace getElementsByTagName by get_elements_by_tagnameand instead of nodeValue, try these, not sure which one will work:node_valuevaluenodeValue()node_value()value()

Link to comment
Share on other sites

I don't know how to upgrade. I'm getting service from Globat.I wrote it in ASP.NET and it works like a charm, but I can't do ASP with Globat:

<%@ Page Language="VB" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><script runat="server">	Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)		Dim Doc As New System.Xml.XmlDocument		Doc.Load("http://export.giveawayoftheday.com/index.xml")		Response.Redirect(Doc.SelectSingleNode("/items/item/download_mirror1").InnerText, True)	End Sub</script>

Link to comment
Share on other sites

then i dont think ur able to upgrade it urself (unless theres some setting in ure CPanel where u can change used php version), so either ask your host or use the php4 alternative

Link to comment
Share on other sites

XML DOM and SimpleXML don't seem to be supported.I used this:

<?php$parser = xml_parser_create();$getit = 0;function start($parser, $element_name, $element_attrs) {global $getit;if ($element_name == "DOWNLOAD_MIRROR1") {$getit = 1;}} function stop($parser, $element_name) {}function char($parser, $data) {global $getit;if ($getit == 1) {header("location:$data");}}xml_set_element_handler($parser, "start", "stop");xml_set_character_data_handler($parser, "char");$fp = fopen("http://export.giveawayoftheday.com/index.xml","r");while ($data = fread($fp,4096)) {  xml_parse($parser,$data,feof($fp)) or   die (sprintf("XML Error: %s at line %d",   xml_error_string(xml_get_error_code($parser)),  xml_get_current_line_number($parser)));}xml_parser_free($parser);?>

This works, all except for the redirection part. It does nothing. How can i fix it?

Link to comment
Share on other sites

Fixed! I had to use ob_start() and ob_flush():

<?phpob_start();$parser = xml_parser_create();$getit = 0;function start($parser, $element_name, $element_attrs) {global $getit;if ($element_name == "DOWNLOAD_MIRROR1") {$getit = 1;}} function stop($parser, $element_name) {}function char($parser, $data) {global $getit;if ($getit == 1) {header("location:$data"); ob_flush();}}xml_set_element_handler($parser, "start", "stop");xml_set_character_data_handler($parser, "char");$fp = fopen("http://export.giveawayoftheday.com/index.xml", "r");while ($data = fread($fp,4096)) {xml_parse($parser, $data,feof($fp));}xml_parser_free($parser);?>

Thank you for all your help and support!

Link to comment
Share on other sites

Hmm ... the old XML parser. Ask them to install DOMDocument, you'll like it :)

Link to comment
Share on other sites

XML DOM and SimpleXML don't seem to be supported.
"XML DOM" and "DOM" are two different extensions. "XML DOM" is the PHP4 extension for DOM processing and is incompatible with the "DOM" extension. The "DOM" extension, as well as the "SimpleXML" extension are available only for PHP5, and can't run on PHP4.
Ha! I just asked them and they said I need to change the extensions to php5! Now I'll try it.I get this error now:
As they said, change your file extensions to php5, i.e. rename
whateverYourFileNameIs.php

to

whateverYourFileNameIs.php5

Only then can we begin to debug the code. DOMDocument will most likely be available in PHP5. It's bundled in with the Windows version. I don't see why other OSes would remove it. If it's not available, then ask them to install it.

Link to comment
Share on other sites

PHP (and DOMDocument) is platform-independent. Did you ask them whether it was installed?

Link to comment
Share on other sites

shouldnt matter, if you have php5 they should work, the php site sais about DOMDocument: "There is no installation needed to use these functions; they are part of the PHP core."make a file called something.php5 and put in it:<?phpprint_r(get_class_methods('DOMDocument'));?>this should give a list of functions, with for example "load", "getElementsByTagName" and many more, does that work?

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...