315 posts
  • Bought between 100 and 499 items
  • Has been a member for 7-8 years
  • United States
timmylogue says

Hello everyone! Let see who the smartest person is! :)

I need help adding a node to an existing .xml file without deleting any other node in that .xml.

Pretty much what I’m trying to do is have a html field text box. Put in the YouTube ID and hit submit.

Then poof!!! it gets added into the already existing .xml file.

Here is the XML File format

<item type="youtube" src="ID_HERE" autoplay="true" controls="false" />

I already have an .xml file created. It reads – images, swf, and more. I would need theses YouTube ID submissions to be randomly put in the .xml file.

For example if YouTube ID submited:

<item type="image" src="1.jpg" autoplay="true" controls="false" />
<item type="image" src="2.jpg" autoplay="true" controls="false" />

<item type="youtube" src="ID_HERE" autoplay="true" controls="false" />

<item type="image" src="3.jpg" autoplay="true" controls="false" />
<item type="image" src="4.jpg" autoplay="true" controls="false" />
<item type="image" src="5.jpg" autoplay="true" controls="false" />

How hard is this? :/

I always look around online before I post, it just seems like a headache. I’m willing to learn!

Thanks to anyone who can help.

Tim

260 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
  • United Kingdom
ravenwill says

Hi Tim,

What language are you currently using?

This is fairly straight forward with php using fopen/read/write etc but largely depends on the complexity of the structure of your xml

You could also parse the xml in javascript, but ultimately to create a file from the data you will need to use a server side language.

315 posts
  • Bought between 100 and 499 items
  • Has been a member for 7-8 years
  • United States
timmylogue says

Hi Tim,

What language are you currently using?

This is fairly straight forward with php using fopen/read/write etc but largely depends on the complexity of the structure of your xml

You could also parse the xml in javascript, but ultimately to create a file from the data you will need to use a server side language.

Hey! Thanks for the reply. I’m using php. Do you have any examples? :D

Tim

260 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
  • United Kingdom
ravenwill says

Hi,

Very basic example, you can do alot more than this.

$file = fopen('myXML.xml', 'a') // Open the file with write permissions, put pointer at the end of the file, if file doesn't exist this creates it

$newXML = '<item type="image" src="1.jpg" autoplay="true" controls="false" \/>\n';

fwrite($file, $newXML); // Writes new string to file
fclose($file); // Closes file

Again this is very basic you would want to do checks and validation and also maybe change the pointer position

315 posts
  • Bought between 100 and 499 items
  • Has been a member for 7-8 years
  • United States
timmylogue says

Thanks!!! I see how this will work now.

315 posts
  • Bought between 100 and 499 items
  • Has been a member for 7-8 years
  • United States
timmylogue says

Hey! I got pretty far with this thanks to you.

HTML :

<form method="post" action="add.php">
<textarea id="area" size="40" cols=22 rows=12 name="area"></textarea>
<input style="margin-top:10px" type="submit" name="submit" value="submit">
</form>

PHP :

<?php

$youTube_ID    =    $_POST['area']; // This is the data from your field
$youTube_ID    =    strip_tags( $youTube_ID ); // This strips any html from the input.

$xml    =     <<<XML

<item type="youtube" src="{$youTube_ID}" autoplay="true" controls="false" \/>

XML;

if ( ! $handle = fopen( 'file.xml', 'a' ) )
{
    die("Unable to open file");
}

if( ! fwrite( $handle, $xml ) )
{
    die("Unable to write to file.");
}

echo "Successfully wrote to xml.";

?>

Here is my XML file sample: http://pastie.org/3279073

My question is how do I get it to appear inside the <items> </items> and not outside of them?

.... and another thing is it possible to place it in the xml random. Not show up on the top after I submit it?

Again thanks for much. I really appreciate it!

Tim

260 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
  • United Kingdom
ravenwill says

Hi Tim,

Glad I could help. Good to see your making progress!

At the moment you have the “a” flag set on your fwrite command, this puts the pointer at the end of the file, so therefore in your case after the closing </items> tag.

You will need to move the pointer using fseek, but first you will need to change the “a” to “w” as the “a” flag doesn’t support fseek.

“w” moves the pointer to the start of the file but then allows you to use fseek to move it around.

Here is the documentation for fseek.

http://www.php.net/manual/en/function.fseek.php

I’d imagine in your case you would want to use SEEK _END for the whence parameter and then move backwards to the first newline (PHP_EOL) which in your file would be the line immediately before </items>

315 posts
  • Bought between 100 and 499 items
  • Has been a member for 7-8 years
  • United States
timmylogue says

Getting a headache haha

losing it

Its not working. I did the steps to what you said. Also changing “a” to “w” just removes the whole current. xml.

I don’t want to delete any data just add.

I need more HELP ! haha

260 posts
  • Author had a Free File of the Month
  • Bought between 10 and 49 items
  • Exclusive Author
  • Grew a moustache for the Envato Movember competition
  • Has been a member for 5-6 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
  • United Kingdom
ravenwill says

Sorry yes my mistake “w” truncates the file, you need to use “c” which doesn’t truncate and puts pointer at the start of file

409 posts
  • Exclusive Author
  • Has been a member for 3-4 years
  • India
Dhruv says

It’s working perfectly (atlest for me)

$line_to_add = "\n".'<item type="youtube" src="NEWLINE" autoplay="true" controls="false" />';

$file = fopen('xmlfile.xml', 'a') or die("can't open file");

fwrite($file, $line_to_add);

fclose($file);
by
by
by
by
by
by