Solved

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

Posted on 2013-06-08
12
405 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

744 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

9 Experts available now in Live!

Get 1:1 Help Now