hey,
got this code writing to an xml file but it wont apend it just keeps goin over the same one, basically at the end of it i want a list of all the different enteries so i can take out information afterwards. need to do this with PHP and XML!
here's the code!
#!/usr/dist/bin/php
<?php
if( ! ($fp = fopen( "sponsor1.xml" , "r" )) )
die("Couldn't open xml file!");
$person_counter = 0;
$person_data = array();
$xml_current_tag_state = '';
function startElementHandler( $parser, $element_name, $element_attribs )
{
global $person_counter;
global $person_data;
global $xml_current_tag_state;
if( $element_name == "STORY" )
{
// $person_data[$person_counter]["author"] = $element_attribs["AUTHOR"];
}
else
{
$xml_current_tag_state = $element_name;
}
}
function endElementHandler( $parser, $element_name )
{
global $person_counter;
global $person_data;
global $xml_current_tag_state;
$xml_current_tag_state = '';
if( $element_name == "STORY" )
{
$person_counter++;
}
}
function characterDataHandler( $parser , $data )
{
global $person_counter;
global $person_data;
global $xml_current_tag_state;
if( $xml_current_tag_state == '' )
return;
if( $xml_current_tag_state == "STORYTYPE" ) {
$person_data[$person_counter]["storytype"] = $data;
}
if( $xml_current_tag_state == "HEADLINE" ) {
$person_data[$person_counter]["headline"] = $data;
}
if( $xml_current_tag_state == "ABSTRACT" ) {
$person_data[$person_counter]["abstract"] = $data;
}
if( $xml_current_tag_state == "BODYTEXT" ) {
$person_data[$person_counter]["bodytext"] = $data;
}
}
if( !($xml_parser = xml_parser_create()) )
die("Couldn't create XML parser!");
xml_set_element_handler($xml_parser, "startElementHandler", "endElementHandler");
xml_set_character_data_handler($xml_parser, "characterDataHandler");
while( $data = fread($fp, 4096) )
{
if( !xml_parse($xml_parser, $data, feof($fp)) )
{
break; // get out of while loop if we're done with the file
}
}
xml_parser_free($xml_parser);
?>
<?php
//$person_data is the array that holds all the information from the xml file
//print_r spoits its out in a readable version
print_r ($person_data);
//$article = "Music";
$thisPos = -1;
$lengthOfArray = count($person_data);
//this (below) is a loop function to get it to print out 'storytype' to see if it is working
for($i=0 ; $i < $lengthOfArray ; ++$i )
{
$thisName = $person_data [$i] ["storytype"];
echo $thisName . "<br>";
if ($thisName == $article) {
echo "<b> match at position number: ". $i . "</b><br>";
$thisPos = $i;
}
}
echo "our match happened at ".$thisPos;
beneath is a conditional to be able to decide do we append or do we overwrite to item
$elementPos = 0;
if ($thisPos==-1)
{
echo " $i ;br>its not on the ARRAY thisPos value is $thisPos we need to append";
$elementPos = $lengthOfArray;
}
else
{
echo "<br> $i;it IS on the array at position $thisPos we ned to over write";
$elementPos = $thisPos;
}
echo "<br> $i; ----this is elementpos value $elementPos ";
/////////////////////////////////////////////////
///////////////adding data sent over/////////////
/////////////////////////////////////////////////
//$storytypeContent = "oh dear god please work";
//$headlineTagContent = "please!!! my new headline does it work";
//$bodyTextInput = "go on!!!mybody test";
//$abstractContent = "pretty please?? myabstract test";
//updating the array with the new contant
//simply overwrite the older content
//at position $elementPos
//note again person_data is the array that holds the XML info
$person_data[$elementPos]['storytype'] = $storytypeContent;
$person_data[$elementPos]['headline'] = $headlineTagContent;
$person_data[$elementPos]['bodytext'] = $bodyTextInput;
$person_data[$elementPos]['abstract'] = $abstractContent;
$somecontent ="<?xml version =\"1.0\"?>\n";
$somecontent .= "<sponsor_tab>\n";
//////////////////stuff from write to a file//////////////////
$filename = "sponsor1.xml";
if (!$fp = fopen($filename, "w+")) {
print "cannot open file ($filename)";
exit;
}
$lengthOfArray = count($person_data);
echo $lengthOfArray;
#echo "number of items in array
#";
for( $i=0; $i < $person_counter; ++$i)
{
$somecontent.="<story>\n";
//echo "<hr><br>";
//echo $i;
//echo " loop number currently in - value of i in loop
//";
$thisName = $person_data[$i];
#print_r ($thisName);
//echo "<hr>";
foreach($thisName As $myKey => $myContent)
//{echo "<BR><b>".$myKey."</b>-".$myContent."-<b>".$mykey."</b><br>"."\n";
{
$somecontent .="<".$myKey.">".$myContent."</".$myKey.">"."\n";
}
$somecontent.="</story>\n\n";
}
$somecontent .= "</sponsor_tab>\n";
echo $somecontent;
if(!fwrite($fp,$somecontent)){
print "cannot write to file ($filename)";
exit;
}
//remembering to close the file
fclose($fp);
echo "<hr>";
print_r ($person_data);
?>
<p><hr></p>
<p>View the XML FILE <a href="sponsor1.xml">sponsor1.xml</a></p>
<p>My_edit_xml file<a href="my_edit_xml_file.xml">my_edit_xml</a></p>