• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 431
  • Last Modified:

write contents of output to xml file and post to a website

I have the following code:
<?
  echo '<?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
  $filenamein = "urls4.txt";
  $urlfile =  @fopen($filenamein, "r") or die("Couldn't open url.txt");
  while (!feof($urlfile)) {
   $line = fgets($urlfile);
   echo '   <url> 
        <loc>' . $line ; 
		echo '</loc>
        <lastmod>2013-06-06</lastmod>
        <changefreq>daily</changefreq>
        <priority>0.8</priority>
    </url>';
	
  }
	?>

Open in new window


I want to save the output to a an xml file and then post it to a website.  How do I do this?
0
jello32
Asked:
jello32
  • 6
  • 5
1 Solution
 
TvMptCommented:
$fp = fopen('urls4.txt', 'r');

$xml = new SimpleXMLElement('<allurls></allurls>');

while ($line = fgetcsv($fp)) {
   $node = $xml->addChild('url');
   $node->addChild('loc', $line[0]);
   $node->addChild('lastmod', $line[1]);
   $node->addChild('changefreq', $line[2]);
   $node->addChild('priority', $line[3]);
}

echo $xml->saveXML();

Open in new window


I assume that txt file have values like:
loc1,2013-12-12,daily,0.8
loc2,2012-12-11,daily,1
0
 
jello32Author Commented:
No the txt file has urls in the form of
http://www.example.com/item1.svg
http://www.example.com/item2.svg
0
 
TvMptCommented:
but where is the aditional info? like lastmod, changefreq and others?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jello32Author Commented:
it gets created in my original code. see above.  it's all the same for each url.  last mod date is 6/6/2013, change frequency is daily.
0
 
TvMptCommented:
...
while ($line = fgetcsv($fp)) {
   $node = $xml->addChild('url');
   $node->addChild('loc', $line[0]);
   $node->addChild('lastmod', 6/6/2013);
   $node->addChild('changefreq', 'daily');
   $node->addChild('priority', '0.8');
}

...

Open in new window

0
 
jello32Author Commented:
how do I post it to a url?
0
 
jello32Author Commented:
also how do i specify the filename i want it saved as?
0
 
TvMptCommented:
instead echo use

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $xml->saveXML());
fclose($fh);
0
 
jello32Author Commented:
I tried what you suggested but it puts 0.000496770988574 as the date for lastmod.  Also how would I put line breaks in as in my original code?  And how to I post it to a website?
0
 
TvMptCommented:
I missed the quotes in
$node->addChild('lastmod', '6/6/2013');

for new line you can add the '<br> or '\n' at end of strings

like
$node->addChild('lastmod', '6/6/2013<br>');
0
 
Ray PaseurCommented:
You might want to consider posting a few different questions here at EE.  It looks like you have several moving parts that need consideration.
puts 0.000496770988574 as the date for lastmod
When you're using DATETIME values, the ISO-8601 standard should be respected for internal representations of dates and also during data interchange.  This article explains how it works.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

New line characters in XML documents can be produced by the PHP_EOL constant.  It is predefined and context-aware, so it will work correctly no matter what OS you're using.  You can use string concatenation to add it to the end of each line of XML.  And, of course, XML does not care about whitespace between tags, so you could just as well omit it.

"How do I post to a website?" is a bit larger question.  In client/server (web) applications, "post" is a term of art, and it seems out of place in your question here.  I don't know that it is out of place, it just seems that way.  You might want to add a new question to EE giving us an example of what you want to do with the XML file.

If you're new to PHP, this book is an excellent learning resource.
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0321833899

Best regards, ~Ray
0
 
jello32Author Commented:
thanks!
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.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now