But it doesn't work like the sample code!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DaveLH1
Forum Newbie
Posts: 2
Joined: Tue Oct 24, 2006 4:53 pm

But it doesn't work like the sample code!

Post 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]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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
DaveLH1
Forum Newbie
Posts: 2
Joined: Tue Oct 24, 2006 4:53 pm

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

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

volka wrote:
DaveLH1 wrote:<filenname>IncidentFile</filename>
typo
Dude, seriously good eye!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

scottayy wrote:Dude, seriously good eye!
naa, xml_get_error_code and xml_get_current_line_number told me so ;)
Post Reply