943,974 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1381
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 1st, 2008
0

PHP and XML Question

Expand Post »
I have this csv2xml.php file

PHP Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3.  
  4.  
  5. error_reporting(E_ALL ^ E_NOTICE);
  6. ini_set("display_errors", true);
  7.  
  8. ini_set('memory_limit', '24M');
  9.  
  10.  
  11.  
  12. function csv2xml($file, $container = 'data')
  13. {
  14. $r = "<{$container}>\n";
  15. $row = 0;
  16. $cols = 0;
  17. $titles = array();
  18.  
  19. $handle = @fopen($file, 'r');
  20. if (!$handle) return $handle;
  21.  
  22. while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
  23. {
  24.  
  25. if (!$cols) $cols = count($data);
  26. for ($i = 0; $i < $cols; $i++)
  27. {
  28. if ($row == 0)
  29. {
  30. $titles[$i] = $data[$i];
  31. continue;
  32. }
  33.  
  34. $r .= "\t\t<{$titles[$i]}>";
  35. $r .= $data[$i];
  36. $r .= "</{$titles[$i]}>\n";
  37. }
  38.  
  39. $row++;
  40. }
  41. fclose($handle);
  42. $r .= "</{$container}>";
  43.  
  44. return $r;
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54. $xml = '<?xml version="1.0" encoding="ISO-8859-1" ?> ';
  55. $xml .= csv2xml('dtifeed.csv', 'petad');
  56.  
  57.  
  58.  
  59. $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');
  60.  
  61. fwrite($xmlfile, $xml) or die('Could not write string to XML file');
  62.  
  63. fclose($xmlfile);
  64.  
  65. echo "Successfully wrote the XML file";
  66.  
  67. ?>


And I get this results

PHP Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="ISO-8859-1" ?> <petad>
  2. <ID>456493</ID>
  3. <Category>P105</Category>
  4. <></>
  5. <></>
  6. <Description>DOGS FOR SALE - They are the best dogs ever. This is a test ad, I would never sell my dogs. Test ad. Please call 928-555-1212.
  7. </Description>
  8. <ID>456501</ID>
  9. <Category>P105</Category>
  10. <></>
  11. <></>
  12. <Description>FIDO MUST GO - But not because we don't love him. Because this is a test ad. The best test ad you've ever read. $1,000. 928-555-1212
  13. </Description>
  14. <ID>456503</ID>
  15. <Category>P110</Category>
  16. <></>
  17. <></>
  18. <Description>KITTENS FREE TO A GOOD HOME - All must go together! There are 101 of them. 928-555-6868.456504</Description>
  19. <ID>456514</ID>
  20. <Category>P125</Category>
  21. <></>
  22. <></>
  23. <Description>A HORSE IS A HORSE unless it's a test ad. Horses for sale, $8,000. 928-111-2323.456516</Description>
  24. </petad>
  25.  


How can I get rid of the empty tags?

Thank you
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007
Oct 1st, 2008
0

Re: PHP and XML Question

this should do it.
php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3.  
  4. error_reporting(E_ALL ^ E_NOTICE);
  5. ini_set("display_errors", true);
  6.  
  7. ini_set('memory_limit', '24M');
  8.  
  9.  
  10.  
  11. function csv2xml($file, $container = 'data')
  12. {
  13. $r = "<{$container}>\n";
  14. $row = 0;
  15. $cols = 0;
  16. $titles = array();
  17.  
  18. $handle = @fopen($file, 'r');
  19. if (!$handle) return $handle;
  20.  
  21. while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
  22. {
  23.  
  24. if (!$cols) $cols = count($data);
  25. for ($i = 0; $i < $cols; $i++)
  26. {
  27. if ($row == 0)
  28. {
  29. $titles[$i] = $data[$i];
  30. continue;
  31. }
  32.  
  33. $r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";
  34. }
  35.  
  36. $row++;
  37. }
  38. fclose($handle);
  39. $r .= "</{$container}>";
  40.  
  41. return $r;
  42. }
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51. $xml = '<?xml version="1.0" encoding="ISO-8859-1" ?> ';
  52. $xml .= csv2xml('dtifeed.csv', 'petad');
  53.  
  54.  
  55.  
  56. $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');
  57.  
  58. fwrite($xmlfile, $xml) or die('Could not write string to XML file');
  59.  
  60. fclose($xmlfile);
  61.  
  62. echo "Successfully wrote the XML file";
  63.  
  64. ?>
Last edited by R0bb0b; Oct 1st, 2008 at 4:09 pm.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 1st, 2008
0

Re: PHP and XML Question

This worked

I also added some code to create a ne file every day and write the xml content there

thank you
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007
Oct 2nd, 2008
0

Re: PHP and XML Question

Hello, lets say that I write an email address in the description field, is there any way to get that email and place it in the xml as a <email></email>

But if I dont have an email in the description just avoid this and continue?

is this possible ?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007
Oct 2nd, 2008
0

Re: PHP and XML Question

You can explode the description field with explode(" ", $description) and then loop through the array with a regular expression(just like you would with an email text field validation). In the case that you do get one or more that comes up true, add them to an emails array and after you are done with the description you can then loop through the emails array echoing them with <email> and </email> surrounding email email address.

make sense?
Last edited by R0bb0b; Oct 2nd, 2008 at 3:03 pm.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 2nd, 2008
0

Re: PHP and XML Question

Click to Expand / Collapse  Quote originally posted by R0bb0b ...
You can explode the description field with explode(" ", $description) and then loop through the array with a regular expression(just like you would with an email text field validation). In the case that you do get one or more that comes up true, add them to an emails array and after you are done with the description you can then loop through the emails array echoing them with <email> and </email> surrounding email email address.

make sense?
Yes do you have a sample
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007
Oct 2nd, 2008
0

Re: PHP and XML Question

Click to Expand / Collapse  Quote originally posted by jencinas69 ...
Yes do you have a sample
I also made some adjustments to help keep it xml compliant
php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3.  
  4. error_reporting(E_ALL ^ E_NOTICE);
  5. ini_set("display_errors", true);
  6.  
  7. ini_set('memory_limit', '24M');
  8.  
  9.  
  10.  
  11. function csv2xml($file, $container = 'data')
  12. {
  13. $dataarray = array();
  14. $r = "<{$container}>\n";
  15.  
  16. $row = 0;
  17. $cols = 0;
  18. $titles = array();
  19.  
  20. $handle = @fopen($file, 'r');
  21. if (!$handle) return $handle;
  22.  
  23. while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
  24. {
  25.  
  26. if (!$cols) $cols = count($data);
  27. for ($i = 0; $i < $cols; $i++)
  28. {
  29. if ($row == 0)
  30. {
  31. $titles[$i] = $data[$i];
  32. continue;
  33. }
  34.  
  35. $emailsarray = array();
  36. if(strtolower($titles[$i]) == "description")
  37. {
  38. foreach(explode(" ", $data[$i]) as $value)
  39. {
  40. if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value))
  41. {
  42. $emailsarray[] = $value;
  43. }
  44. }
  45. }
  46.  
  47. $dataarray[$titles[$i]][] = $data[$i];
  48.  
  49. foreach($emailsarray as $value)
  50. {
  51. $dataarray["email"][] = $value;
  52. }
  53. }
  54.  
  55. $row++;
  56. }
  57. fclose($handle);
  58.  
  59. $containers = array("email");
  60.  
  61. foreach($dataarray as $key=>$value)
  62. {
  63. if(in_array(strtolower($key), $containers))
  64. {
  65. $r .= "\t\t<" . $key . "-container>\n";
  66. }
  67.  
  68. foreach($value as $value2)
  69. {
  70. $tabchars = "\t\t";
  71. if(in_array(strtolower($key), $containers))
  72. {
  73. $tabchars .= "\t\t";
  74. }
  75. $r .= $tabchars . "<$key>$value2</$key>\n";
  76. }
  77.  
  78. if(in_array(strtolower($key), $containers))
  79. {
  80. $r .= "\t\t</" . $key . "-container>\n";
  81. }
  82. }
  83.  
  84. $r .= "</{$container}>\n";
  85. return $r;
  86. }
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
  96. $xml .= csv2xml('dtifeed.csv', 'petad');
  97. echo "<xmp>" . $xml . "</xmp>";
  98.  
  99.  
  100. $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');
  101.  
  102. fwrite($xmlfile, $xml) or die('Could not write string to XML file');
  103.  
  104. fclose($xmlfile);
  105.  
  106. echo "Successfully wrote the XML file";
  107.  
  108. ?>
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008
Oct 2nd, 2008
0

Re: PHP and XML Question

Hello I added my code to your code so I can be able to write to the new xml file and also the trim function and is not working it gives me a blank page

PHP Syntax (Toggle Plain Text)
  1.  
  2. <?php
  3.  
  4.  
  5. error_reporting(E_ALL ^ E_NOTICE);
  6. ini_set("display_errors", true);
  7.  
  8. ini_set('memory_limit', '24M');
  9.  
  10. $file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd
  11.  
  12. if (!file_exists($file)) touch($file);
  13.  
  14. $fh = fopen($file, "r");
  15.  
  16. function csv2xml($file, $container = 'data')
  17. {
  18. $dataarray = array();
  19. $r = "<{$container}>\n";
  20.  
  21. $row = 0;
  22. $cols = 0;
  23. $titles = array();
  24.  
  25. $handle = @fopen($file, 'r');
  26. if (!$handle) return $handle;
  27.  
  28. while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
  29. {
  30.  
  31. if (!$cols) $cols = count($data);
  32. for ($i = 0; $i < $cols; $i++)
  33. {
  34. if ($row == 0)
  35. {
  36. $titles[$i] = $data[$i];
  37. continue;
  38. }
  39. $r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";
  40. }
  41.  
  42. $emailsarray = array();
  43. if(strtolower($titles[$i]) == "description")
  44. {
  45. foreach(explode(" ", $data[$i]) as $value)
  46. {
  47. if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value))
  48. {
  49. $emailsarray[] = $value;
  50. }
  51. }
  52. }
  53.  
  54. $dataarray[$titles[$i]][] = $data[$i];
  55.  
  56. foreach($emailsarray as $value)
  57. {
  58. $dataarray["email"][] = $value;
  59. }
  60. }
  61.  
  62. $row++;
  63. }
  64. fclose($handle);
  65.  
  66. $containers = array("email");
  67.  
  68. foreach($dataarray as $key=>$value)
  69. {
  70. if(in_array(strtolower($key), $containers))
  71. {
  72. $r .= "\t\t<" . $key . "-container>\n";
  73. }
  74.  
  75. foreach($value as $value2)
  76. {
  77. $tabchars = "\t\t";
  78. if(in_array(strtolower($key), $containers))
  79. {
  80. $tabchars .= "\t\t";
  81. }
  82. $r .= $tabchars . "<$key>$value2</$key>\n";
  83. }
  84.  
  85. if(in_array(strtolower($key), $containers))
  86. {
  87. $r .= "\t\t</" . $key . "-container>\n";
  88. }
  89. }
  90.  
  91. $r .= "</{$container}>\n";
  92. return $r;
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102. $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
  103. $xml .= csv2xml('petsfeed.csv', 'petad');
  104. echo "<xmp>" . $xml . "</xmp>";
  105.  
  106.  
  107. $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');
  108.  
  109. fwrite($xmlfile, $xml) or die('Could not write string to XML file');
  110.  
  111. fclose($xmlfile);
  112.  
  113. echo "Successfully wrote the XML file";
  114.  
  115. ?>
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007
Oct 2nd, 2008
0

Re: PHP and XML Question

I discover the problem this is

PHP Syntax (Toggle Plain Text)
  1. $r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";

DO I need to name r different???
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
jencinas69 is offline Offline
68 posts
since Nov 2007
Oct 2nd, 2008
0

Re: PHP and XML Question

Click to Expand / Collapse  Quote originally posted by jencinas69 ...
I discover the problem this is

PHP Syntax (Toggle Plain Text)
  1. $r .= trim($data[$i]) != ""?"\t\t<{$titles[$i]}>$data[$i]</{$titles[$i]}>\n":"";

DO I need to name r different???
Oops, sorry, forgot about that line, this should do it.
php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3.  
  4. error_reporting(E_ALL ^ E_NOTICE);
  5. ini_set("display_errors", true);
  6.  
  7. ini_set('memory_limit', '24M');
  8.  
  9. $file = "pets_feed_" . date("Ymd") . ".xml";// - for yyyymmdd
  10.  
  11. if (!file_exists($file)) touch($file);
  12.  
  13. $fh = fopen($file, "r");
  14.  
  15. function csv2xml($file, $container = 'data')
  16. {
  17. $dataarray = array();
  18. $r = "<{$container}>\n";
  19.  
  20. $row = 0;
  21. $cols = 0;
  22. $titles = array();
  23.  
  24. $handle = @fopen($file, 'r');
  25. if (!$handle) return $handle;
  26.  
  27. while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
  28. {
  29.  
  30. if (!$cols) $cols = count($data);
  31. for ($i = 0; $i < $cols; $i++)
  32. {
  33. if ($row == 0)
  34. {
  35. $titles[$i] = $data[$i];
  36. continue;
  37. }
  38.  
  39. $emailsarray = array();
  40. if(strtolower($titles[$i]) == "description")
  41. {
  42. foreach(explode(" ", $data[$i]) as $value)
  43. {
  44. if(eregi("^[a-zA-Z0-9/\._\-]+@[a-zA-Z0-9_\-]+\.[a-zA-Z0-9\._\-]+$", $value))
  45. {
  46. $emailsarray[] = $value;
  47. }
  48. }
  49. }
  50.  
  51. $dataarray[$titles[$i]][] = trim($data[$i]) != ""?$data[$i]:"";
  52.  
  53. foreach($emailsarray as $value)
  54. {
  55. $dataarray["email"][] = $value;
  56. }
  57. }
  58.  
  59. $row++;
  60. }
  61. fclose($handle);
  62.  
  63. $containers = array("email");
  64.  
  65. foreach($dataarray as $key=>$value)
  66. {
  67. if(in_array(strtolower($key), $containers))
  68. {
  69. $r .= "\t\t<" . $key . "-container>\n";
  70. }
  71.  
  72. foreach($value as $value2)
  73. {
  74. $tabchars = "\t\t";
  75. if(in_array(strtolower($key), $containers))
  76. {
  77. $tabchars .= "\t\t";
  78. }
  79. $r .= $tabchars . "<$key>$value2</$key>\n";
  80. }
  81.  
  82. if(in_array(strtolower($key), $containers))
  83. {
  84. $r .= "\t\t</" . $key . "-container>\n";
  85. }
  86. }
  87.  
  88. $r .= "</{$container}>\n";
  89. return $r;
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. $xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
  100. $xml .= csv2xml('petsfeed.csv', 'petad');
  101.  
  102.  
  103. $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');
  104.  
  105. fwrite($xmlfile, $xml) or die('Could not write string to XML file');
  106.  
  107. fclose($xmlfile);
  108.  
  109. echo "Successfully wrote the XML file";
  110.  
  111. ?>

it has been changed to this line
$dataarray[$titles[$i]][] = trim($data[$i]) != ""?$data[$i]:"";

The reason I added the data to $dataarray is because it is possible now to have multiple email addresses and it is (I believe) xml compliant that when you have multiple of anything that they should go into a container. Or maybe it is just good practice.

This line here defines your containers:
$containers = array("email");

Another thing that this does is if you have an email field and you are also pulling email addresses out of the description, you want them to be grouped together, the $dataarray allows you to do that by adding all of the email address to $dataarray['email'] key and then looping through after all the data is looped through.
Last edited by R0bb0b; Oct 2nd, 2008 at 6:09 pm.
Reputation Points: 358
Solved Threads: 89
Posting Shark
R0bb0b is offline Offline
986 posts
since Jun 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Symfony MOdule redirection
Next Thread in PHP Forum Timeline: Multiple SQL Queries in PHP page





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC