Solved

open XML and copy content to another xml file

Posted on 2012-03-28
10
324 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
Comment Utility
It is very simple, use this:
$path="./googlexml.xml";
$pathtwo="./finalxml.xml";
copy ( $path , $pathtwo);
0
 
LVL 1

Author Comment

by:jblayney
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:jblayney
Comment Utility
Ray, I have fopen in both my examples..

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

Author Comment

by:jblayney
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
it provided the missing portion of the final code, yet Bartvd answered my initial question perfect and deserves the points
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.

771 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

14 Experts available now in Live!

Get 1:1 Help Now