Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

open XML and copy content to another xml file

Posted on 2012-03-28
10
Medium Priority
?
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
10 Comments
 
LVL 6

Accepted Solution

by:
bartvd earned 2000 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 111

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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 111

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
 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

618 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