Solved

open XML and copy content to another xml file

Posted on 2012-03-28
10
327 Views
Last Modified: 2012-04-03
Hello,

I want to be open one XML file with PHP, copy the content of the file and paste it into another XML file and save that to the server.

My code is...


$path="./googlexml.xml";
$filenum=fopen($path,"c+");

$pathtwo="./finalxml.xml";
$filenumtwo=fopen($pathtwo,"w");  

$file = file_get_contents($pathtwo, true);

fwrite($pathtwo,$file);
fclose($pathtwo);

fclose($path);


the errors i am getting are..

Warning: fwrite() expects parameter 1 to be resource, string given in /write-xml.php on line 17

Warning: fclose() expects parameter 1 to be resource, string given in /write-xml.php on line 18

Warning: fclose() expects parameter 1 to be resource, string given in /write-xml.php on line 20
0
Comment
Question by:jblayney
  • 6
  • 2
  • 2
10 Comments
 
LVL 6

Accepted Solution

by:
bartvd earned 500 total points
ID: 37778551
It is very simple, use this:
$path="./googlexml.xml";
$pathtwo="./finalxml.xml";
copy ( $path , $pathtwo);
0
 
LVL 1

Author Comment

by:jblayney
ID: 37778686
Thank you.. maybe i should have been more clear.. that works great, thank you. But it wont allow me to write more data to the bottom of finalxml.xml ?

for example (sorry you deserve points, can make new thread)

$path="./googlexml.xml";
$pathtwo="./finalxml.xml";
copy ( $path , $pathtwo);


$filenum=fopen($pathtwo,"a");
fwrite($pathtwo,"test");
fclose($pathtwo);  

i get errors

Warning: fwrite() expects parameter 1 to be resource, string given in /write-xml.php on line 10

Warning: fclose() expects parameter 1 to be resource, string given in /write-xml.php on line 11
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37779016
Here is the link to the documentation for fwrite()
http://php.net/manual/en/function.fwrite.php

As you can see the function expects that you have used fopen() to open the file and get a resource for the first parameter passed to fwrite().  Hence the message.
0
 
LVL 6

Expert Comment

by:bartvd
ID: 37779024
Ok, I understand, so you want to add the contents of $path at the end of the existing file $pathtwo?

I think this should work:
$path="./googlexml.xml";

$pathtwo="./finalxml.xml";
$filenumtwo=fopen($pathtwo,"a");  

$file = file_get_contents($path, true);

fwrite($filenumtwo,$file);
fclose($filenumtwo);
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37779049
Suggestion: Instead of posting code that does not work, try posting the inputs and show us an example of the required output -- just show us the data, no code at all.  Then we can show you, with tested and working examples, how the code fits in with the test data.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:jblayney
ID: 37781786
Ray, I have fopen in both my examples..

$filenum=fopen($path,"c+");
0
 
LVL 1

Author Comment

by:jblayney
ID: 37781853
Just to explain what i am doing..

I have a google sitemap which a user is currently managing and they want to continue managing. (googlexml.xml)

The website has many dynamic pages which are constantly changing which need to be added to the sitemap.

So I am building a function where my code will open up the clients sitemap XML file (googlexml.xml), copy all the contents to another sitemap file (finalxml.xml), then I will run a few recordsets to write out all the dynamic pages into the sitemap and save with the new name (finalxml.xml).

I already have lots of scripts for writing out XML sitemaps from a database, the part that is missing and that i cant figure out how to import the content of another XML file.

and yes ray i did visit that website and read it before i posted, i come to experts-exchange only as a last resort because i usually figure out my problems before anyone ever responds
0
 
LVL 1

Author Comment

by:jblayney
ID: 37781923
bartvd, nothing happens....

it makes no sense, my code should work, i double checked.. something small is off...


$path="./googlexml.xml";
$pathtwo="./finalxml.xml";
copy ($path , $pathtwo);

// these first 3 lines copy the content perfectly from one file to the other..


$filenum=fopen($pathtwo,"a");
fwrite($pathtwo,"test");
fclose($pathtwo);  

// this is the code to use to use a file and write to it. fopen with a "a" open a file and write stuff at end
0
 
LVL 1

Assisted Solution

by:jblayney
jblayney earned 0 total points
ID: 37781998
ok, here is final code
i mixed up my variables ( $filenum )

$path="../googlexml.xml";
$pathtwo="../finalxml.xml";
copy ($path , $pathtwo);


$letsopen="../finalxml.xml";
$filenum = fopen($letsopen,"a+");
fwrite($filenum,"<loc>/new-website-line.php</loc>");
fclose($filenum);
0
 
LVL 1

Author Closing Comment

by:jblayney
ID: 37799977
it provided the missing portion of the final code, yet Bartvd answered my initial question perfect and deserves the points
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now