[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2013-06-08
12
Medium Priority
?
428 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
[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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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
 

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 2000 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 111

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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

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…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

656 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