Page 1 of 1

But it doesn't work like the sample code!

Posted: Tue Oct 24, 2006 6:40 pm
by DaveLH1
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I am trying to use XML to store data then read it in a different page and parse it using the xml_parse_into_struct. The examples use both file_get_contents and fopen/fread. I get different results retrieving the XML.

file_get_contents gives a list of the data but no tags
fopen opens the file but fread returns FALSE but errno is not set

any help would be greatly appreciated
Dave

Here is my code. This is code just to test the function before I clean it up and put it into the app so you may think it is sloppy.

Code: Select all

<?PHP

	$FTxt = "testcall.xml";

	echo "Filesize = ".filesize($FTxt)."<br>";

//	if (!$Fl = fopen($FTxt, 'r')) {
//		echo "<Font color=\"red\" size=\"+1\">";
//		echo "Error: Cannot open file ($FTxt) for reading.<br>";
//		echo "Error: Error is ".$errmsg."(".$errno.").</Font><br>";
//		$err = 1;
//	}
//	else {
		$XmlData = file_get_contents($FTxt);
//		$XmlData = fread($F1, filesize($FTxt));
//		if ($XmlData === FALSE) {
//			echo "<Font color=\"red\" size=\"+1\">";
//			echo "Error: Cannot read to file ($FTxt) - Original eMail<br>";
//			echo "Error: Error is ".$errmsg."(".$errno.").</Font><br>";
//		}
//		fclose($Fl);
//	}

	echo "<Pre>".$XmlData."</Pre><br>";

// Code from php.net
//	$simple = "<para><note>simple note</note></para>";
	$p = xml_parser_create();
	xml_parse_into_struct($p, $XmlData, $vals, $index);
	xml_parser_free($p);
	echo "Index array<Pre>";
	print_r($index);
	echo "\nVals array\n";
	print_r($vals);
	echo "</Pre>";

?>
and the XML file

Code: Select all

<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
 <dispatcher>
  <type>incident</type>
  <nature>IncidentType</nature>
  <agency>IncidentAgency</agency>
  <municipality>IncidentMunicipality</municipality>
  <houseno>IncidentHouseNo</houseno>
  <street>IncidentStreet</street>
  <cross1>IncidentCrossSt1</cross1>
  <cross2>IncidentCrossSt2</cross2>
  <crosssts>IncidentCrossSts</crosssts>
  <units>IncidentUnit</units>
  <timeout>IncidentTimeOut</timeout>
  <datetime>IncidentDatePrt</datetime>
  <filenname>IncidentFile</filename>
 </dispatcher>
It is probably something stupid I'm doing.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Oct 24, 2006 6:57 pm
by volka
You have no error handling in your code
DaveLH1 wrote:xml_parse_into_struct($p, $XmlData, $vals, $index);

Code: Select all

if (!xml_parse_into_struct($p, $XmlData, $vals, $index)) {
	echo 'error: ', xml_error_string(xml_get_error_code($p)), ' @ ', xml_get_current_line_number($p);
}
DaveLH1 wrote:<?xml version="1.0" encoding="ISO-8859-1"?>
Why are there \ before the quotes?
DaveLH1 wrote:<filenname>IncidentFile</filename>
typo

Did you ever look at code for hours?????????

Posted: Tue Oct 24, 2006 7:34 pm
by DaveLH1
Thanks,

The \ were left over because I took the statement out of a program and I escaped the ".
Anyways, when I fixed that the code worked.

Dave

Posted: Tue Oct 24, 2006 8:03 pm
by s.dot
volka wrote:
DaveLH1 wrote:<filenname>IncidentFile</filename>
typo
Dude, seriously good eye!

Posted: Tue Oct 24, 2006 8:25 pm
by volka
scottayy wrote:Dude, seriously good eye!
naa, xml_get_error_code and xml_get_current_line_number told me so ;)