ktsixit
12-11-2008, 10:01 AM
hi, I'm creating a php script for adding content into an xml file, in a specific position. The xml file ends with a "</data>" tag, so I believe that a good way to add content before "</data>" is to use fseek.
$fname = "demo.xml";
$fhandle = fopen($fname,"r");
fseek($fhandle,-7,SEEK_END);
$content = "<title>".$_POST['title']."</title>";
$content .= "<text>".$_POST['text']."</text>";
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
The problem is that this code deletes all contents of the xml file and adds the current $content. So I suppose I don't use fseek right. What I want to do is add content before "</data>" and keep the rest of the file as it was.
xml file:
<?xml version="1.0" encode="UTF-8"?>
<!DOCTYPE data[
<!ELEMENT title(comments, image)>
<!ATTLIST title name CDATA #REQUIRED>
<!ELEMENT text (#PCDATA)>
]>
<data>
<title name="title 1">
<text>text1</text>
</title>
<title name="title 2">
<text>text2</text>
</title>
<title name="title 3">
<text>text 3</text>
</title>
<--------- add text here
</data>
$fname = "demo.xml";
$fhandle = fopen($fname,"r");
fseek($fhandle,-7,SEEK_END);
$content = "<title>".$_POST['title']."</title>";
$content .= "<text>".$_POST['text']."</text>";
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
The problem is that this code deletes all contents of the xml file and adds the current $content. So I suppose I don't use fseek right. What I want to do is add content before "</data>" and keep the rest of the file as it was.
xml file:
<?xml version="1.0" encode="UTF-8"?>
<!DOCTYPE data[
<!ELEMENT title(comments, image)>
<!ATTLIST title name CDATA #REQUIRED>
<!ELEMENT text (#PCDATA)>
]>
<data>
<title name="title 1">
<text>text1</text>
</title>
<title name="title 2">
<text>text2</text>
</title>
<title name="title 3">
<text>text 3</text>
</title>
<--------- add text here
</data>