Solved

open XML and copy content to another xml file

Posted on 2012-03-28
10
337 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 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 110

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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 110

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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

729 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