Hi
Verybody...
I have to read and display contents of a xml file named as pendingnews.xml, given below
//************* pendingnews.xml *****************
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!DOCTYPE lotr_characters [
<!ELEMENT person (name , email , newstitle , description)>
<!ATTLIST person alignment (good | evil) #REQUIRED>
<!ELEMENT name (#PCDATA)>
<!ELEMENT email (#PCDATA)>
<!ELEMENT newstitle (#PCDATA)>
<!ELEMENT description (#PCDATA)>
]>
<ieee>
<tgroup>
<person alignment="good">
<name>Raaj</name>
<email>
[email protected]</email>
<newstitle>u can post news now</newstitle>
<description>U can post news now..</description>
</person>
<person alignment="evil">
<name>Deep</name>
<email>
[email protected]</email>
<newstitle>Easy tool for u</newstitle>
<description>There r ur desired tools available on net.</description>
</person>
<person alignment="good">
<name>Pankaj: The Bhangra Guru</name>
<email>
[email protected]</email>
<newstitle>Bhangra in IITK</newstitle>
<description>There will bhagra dance in iitk on occasion of Antaragani</description>
</person>
</tgroup>
</ieee>
//************************************************** ***********
I write this file by using php code shown below
//*************** lnewspost.php **********************
<?
$post=$_POST['post'];
$news_msg='';
$xml='./doc/xml/pendingnews.xml';
if($post)
{
if (version_compare(PHP_VERSION,'5','>='))
require_once('domxml-php4-to-php5.php');
//the folowing cotents are posted by GUI having field names
//name,email..etc
$name =$_POST['name'];
$email =$_POST['email'];
$newstitle =$_POST['newstitle'];
$description =$_POST['description'];
if(($email=='')|| ($newstitle=='')|| ($describe==''))
{
$tag=array(name, email, newstitle, description);
$tagval=array($name, $email, $newstitle, $description);
if(file_exists($xml))
{
if ($dom=domxml_open_file("$xml"))
{
$root=$dom->document_element();
$bs=$root->get_elements_by_tagname('tgroup');
if (count($bs)==1)
{
$totarr=count($tag);
$body=$bs[0];
for($r=0; $r<$totarr; $r++)
{
$body=$bs[0];
$k=$dom->create_element($tag[$r]);
$k->append_child($dom->create_text_node($tagval[$r]));
$body->append_child($k);
$body->append_child($dom->create_text_node("\n"));
} //end of for $body->append_child($dom->create_text_node("\n"));
} //end of 5th inner most "if"
$dom->dump_file("$xml",false,false);
$news_msg="Congratulations..! News has posted successfully";
header("location: newspost.php?newsmsg=$news_msg");
exit;
} //end of 4th inner "if"
else
{
$news_msg="Sorry..! News file could not opend";
header("location: newspost.php?newsmsg=$news_msg");
}
} //end of 3rd inner "if"
else
{
$news_msg="File does not exists or write permission denied";
header("location: newspost.php?newsmsg=$news_msg");
}
} //end of 2nd inner "if"
}//end of 1st inner "if"
?>
//************************************************** ************
I had display the xml file (pendingnews.xml) by following code
//*********************** readnews.php *****************
<?
if( ! ($fp = fopen( "./doc/xml/pendingnews.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 == "PERSON" )
{
$person_data[$person_counter]["alignment"] = $element_attribs["ALIGNMENT"];
}
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 == "PERSON" )
{
$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 == "NAME" )
{
$person_data[$person_counter]["name"] = $data;
}
if( $xml_current_tag_state == "EMAIL" )
{
$person_data[$person_counter]["email"] = $data;
}
if( $xml_current_tag_state == "NEWSTITLE" )
{
$person_data[$person_counter]["newstitle"] = $data;
}
if( $xml_current_tag_state == "DESCRIPTION" )
{
$person_data[$person_counter]["description"] = $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);
?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Parsing the Sample XML File</title>
</head>
<body bgcolor="#ffffff">
<?
//echo$person_counter;
for( $i=0 ; $i < $person_counter ; ++$i )
//for( $i=0 ; $i < 5; ++$i )
{
$font_color = $person_data[$i]["alignment"] == "good" ? "#0000ff" : "#ff0000";
echo "" .
$person_data[$i]["name"] . "<br>\n";
echo "Email: " . $person_data[$i]["email"] . "<br>\n";
echo "News Title: " . $person_data[$i]["newstitle"] . "<br>\n";
echo "News Desc: " . $person_data[$i]["description"] . "<br>\n";
echo "<br>\n";
}
?>
</body>
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);
?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Parsing the Sample XML File</title>
</head>
<body bgcolor="#ffffff">
<?
//echo$person_counter;
for( $i=0 ; $i < $person_counter ; ++$i )
//for( $i=0 ; $i < 5; ++$i )
{
$font_color = $person_data[$i]["alignment"] == "good" ? "#0000ff" : "#ff0000";
echo "" .
$person_data[$i]["name"] . "<br>\n";
echo "Email: " . $person_data[$i]["email"] . "<br>\n";
echo "News Title: " . $person_data[$i]["newstitle"] . "<br>\n";
echo "News Desc: " . $person_data[$i]["description"] . "<br>\n";
echo "<br>\n";
}
?>
</body>
</html>
//************************************************** *******
My code in lnewspost.php is not writing the following 2 lines in xml file pendingnews.xml
<person alignment="good"> //in the begning of block
</person> //in the end of the block
My First question is how to write these line at right location ?(through php code in lnewspost.php) note that I wrote it manually
My second question is, How I can delete any perticular block / blocks (starting with <person alignment="good/or evil"> and ending with </person>) and write it into another same xml file, named as approvenews.xml by editing or without editing the cotents( means editing newstitle, newsdescription ..etc)?
//************************************************** ************
Please help for this code ...
Thanx and Regards
rapraj