Solved

open XML and copy content to another xml file

Posted on 2012-03-28
10
329 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 109

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 109

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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

803 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