Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 27th, 2007, 02:18 AM
Authorized User
 
Join Date: Feb 2007
Location: IIT Kanpur, UP, India.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default Read, write, delete and display xml file in php

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>raajkhurana@gmail.com</email>
                <newstitle>u can post news now</newstitle>
                <description>U can post news now..</description>
                </person>

                <person alignment="evil">
                <name>Deep</name>
                <email>deepsingh@iitk.ac.in</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>pankaj_spidy@gmail.com</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: &nbsp;&nbsp;" . $person_data[$i]["email"] . "<br>\n";
                        echo "News Title: &nbsp;&nbsp;" . $person_data[$i]["newstitle"] . "<br>\n";
                        echo "News Desc: &nbsp;&nbsp;" . $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: &nbsp;&nbsp;" . $person_data[$i]["email"] . "<br>\n";
                        echo "News Title: &nbsp;&nbsp;" . $person_data[$i]["newstitle"] . "<br>\n";
                        echo "News Desc: &nbsp;&nbsp;" . $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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Read and write excel file using VB6 sjlsysprg1 VB How-To 14 September 21st, 2011 01:21 PM
write into xml file in php rapraj PHP How-To 0 December 3rd, 2007 06:04 AM
read & write file C# minhpx General .NET 1 February 21st, 2005 04:14 PM
a problem about read and write the xml files watson .NET Web Services 0 September 5th, 2004 09:25 PM
a problem about read and write the xml files watson C# 0 September 4th, 2004 10:33 AM



All times are GMT -4. The time now is 02:20 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.