Solved

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

Posted on 2013-06-08
12
408 Views
Last Modified: 2013-06-10
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
Comment
Question by:jello32
  • 6
  • 5
12 Comments
 
LVL 9

Expert Comment

by:TvMpt
ID: 39232815
$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
 

Author Comment

by:jello32
ID: 39232911
No the txt file has urls in the form of
http://www.example.com/item1.svg
http://www.example.com/item2.svg
0
 
LVL 9

Expert Comment

by:TvMpt
ID: 39233003
but where is the aditional info? like lastmod, changefreq and others?
0
 

Author Comment

by:jello32
ID: 39233277
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
 
LVL 9

Expert Comment

by:TvMpt
ID: 39233296
...
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
 

Author Comment

by:jello32
ID: 39233309
how do I post it to a url?
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:jello32
ID: 39233313
also how do i specify the filename i want it saved as?
0
 
LVL 9

Expert Comment

by:TvMpt
ID: 39233546
instead echo use

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $xml->saveXML());
fclose($fh);
0
 

Author Comment

by:jello32
ID: 39233588
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
 
LVL 9

Accepted Solution

by:
TvMpt earned 500 total points
ID: 39234302
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39235034
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
 

Author Closing Comment

by:jello32
ID: 39235259
thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn how to count occurrences of each item in an array.

867 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

16 Experts available now in Live!

Get 1:1 Help Now