Jump to content

Appending Dynamic Data to existing XML file in PHP


prash91
Go to solution Solved by mac_gyver,

Recommended Posts

I have two php file

createxmlfile.php and display.php

I am trying to append the text which i insert through display.php file and create a new xmlfile call phpxml1.php

everything works perfectly, except it also add duplicate date, I need a hint, how can i avoid inserting duplicate data  

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Create Xml File In PHP</title>
</head>
<body>
<p>Please fill following Data</p>
<Form action="display.php" method="post">
ID :<input type="text" id="id" name="id" />
Name : <input type="text" id="name" name="name" />

<input type="submit" id="submit" value="Submit">
</Form>
</body>
</html>

 

 

display.php

<?PHP
function c_element($e_name,$parent){
 global $xml;
 $node=$xml->createElement($e_name);
 $parent->appendChild($node);
 return $node;
 }
function c_value($value,$parent){
 global $xml;
 $value = $xml->createTextNode($value);
 $parent->appendChild($value);
 return $value;
 
 }

?>

 

<?PHP
$s_id = $_POST['id'];
$s_name = $_POST['name'];

echo $s_id. '<br>' .$s_name;
$xml =new DOMDocument("1.0");
$xml->load("xmlphp1.xml");

$root=$xml->getElementsByTagName("students")->item(0);
$student=c_element("student",$root);

$id=c_element("id", $student);
c_value("$s_id",$id);

$name = c_element("name", $student);
c_value("$s_name",$name);

//$xml= new DOMDocument("1.0","utf-8");
//$employee = $xml->createElement("employee");
//$employee = $xml->appendChild($employee);

//$empname = $xml->createElement("empname",$name);
//$empname = $employee->appendChild($empname);

//$empemail = $xml->createElement("empemail",$email);
//$price= $employee->appendChild($empemail);

$xml->FormatOutput=true;
//$string_value=$xml->saveXML();
$xml->save("xmlphp1.xml");

?>

Link to comment
Share on other sites

Well, the data what i insert here from form which, is createxmlfile.php and display.php 

makes new xml file called xmlphp1.xml

it gives me the results

for example

<?xml version="1.0" encoding="utf-8"?>
<students>
 <student>
  <id>123</id>
  <name>Paul</name>
 </student>
 <student>
  <id>124</id>
  <name>Sandy</name>
 </student>
</students>

so I have these data in my xml file if I try inserting same data let say Paul or 123 it should show me the message that the name is already exists.

Link to comment
Share on other sites

  • Solution

xml is designed/intended to be used as a data exchange format between different systems. it is not designed to be a database. all the extra code and processing needed to treat is as a database, and in your case to prevent adding duplicate data, is not efficient.

 

in order to prevent duplicated data, you would need to search/parse through the "xmlphp1.xml" values and test to make sure that the newly submitted data isn't already present.

 

unless your assignment is to devise how to find duplicate data using xml, an alternate approach would be to store the actual data in a database or at a minimum a serialized/json array stored in a file, so that php can easily read it into a native php array and process it. testing/preventing duplicates would occur either in a database query or using php's array functions. you would then simply iterate over the actual data and output it as xml only when needed.

Edited by mac_gyver
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.