Jump to content

editing xml document through php!


annie76

Recommended Posts

hi there!
im relatively new to php so i apologise in advance if the answer is staring me in the face.
i am trying to make a content mangement system where through the use of forms someone can edit the information in the xml document. below i have included the code for the three php pages i have so far. i was wondering if anyone could help me change the code on the third page so that i can edit the xml document according to what "storytype" is selected in stepone?


i hope someone can help,
thanks in advance!
annie



[b]page1: stepone.php[/b]


  <?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);



?>
</p>
<p>
  <?php



for( $i=0 ; $i < $person_counter ; ++$i )
{

$link = $person_data[$i]["storytype"];

echo "<a href=\"steptwo.php?infoItem=$link\">$link </a><br><p>";

}

?>
 
</p>
</body>
</html>


[b]Page 2 steptwo.php[/b]

<?php echo $infoItem; ?>




<p>&nbsp;</p>
<form METHOD=GET action="stepthreeA.php">
  <p><br>
    one:<br>
   
  <textarea cols="20" rows="3" name="headlineTagContent">Enter headline here</textarea>
  </p>
  <p>two:
    <input type="text" name="abstractContent" value="enter story type" size="20" maxlength="20">
  </p>
  <p>three:<br>
    <textarea cols="20" rows="3" name="bodyTextInput">Enter content for story here</textarea>
   
    <input type="submit" name="submit" value="SUBMIT">
  </p>
</form></p>




[b]page3: stepthreeA.php[/b]


<?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



$lengthOfArray = count($person_data);
//echo $lengthOfArray;


for( $i=0 ; $i < $lengthOfArray ; ++$i ) 
//echo $i;

$thisName = $person_data[$i];
//print_r ($thisName);



$person_data[1]['headline'] = $headlineTagContent;
$person_data[1]['bodytext'] = $bodyTextInput;
$person_data[1]['abstract'] = $abstractContent;




$filename = 'sponsor1.xml';

if (!$fp = fopen($filename, 'w+')) {
print "Cannot open file ($filename)";

exit;
}

//$lengthOfArray = count($person_data);
//echo $lengthOfArray;


$somecontent = "<?xml version=\"1.0\"?>\n";
$somecontent .= "<sponsor_tab>\n";
for( $i=0 ; $i < $person_counter ; ++$i )
{
$somecontent .= "<story>\n";
$thisName = $person_data[$i];
//print_r ($thisName);

foreach($thisName As $myKey => $myContent)
{
$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);
?>
View the XML file you have just written to <a href="sponsor1.xml">sponsor1.xml</a></p>


Link to comment
https://forums.phpfreaks.com/topic/33022-editing-xml-document-through-php/
Share on other sites

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.