Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Parsowanie pliku xml
Forum PHP.pl > Forum > PHP > Object-oriented programming
dzobert
Mam taki plik
  1. <?php
  2.  
  3. class xItem {
  4. var $xTitle;
  5. var $xLink;
  6. var $xUpdated;
  7. var $xDescription;
  8. var $xAuthor;
  9. }
  10.  
  11. $sTitle = "";
  12. $sLink = ""; 
  13. $sDescription = ""; 
  14. $sLanguage = "";
  15. $sWebMaster = "";
  16. $arItems = array(); 
  17. $itemCount = 0;
  18.  
  19. function startElement($parser, $name, $attrs) {
  20. global $curTag;
  21. $curTag .= "^$name";
  22. }
  23.  
  24. function endElement($parser, $name) {
  25. global $curTag;
  26. $caret_pos = strrpos($curTag,'^');
  27. $curTag = substr($curTag,0,$caret_pos);
  28. }
  29.  
  30. function characterData($parser, $data) { 
  31. global $curTag; 
  32. // get the Channel information first
  33.  
  34. global $sTitle, $sLink, $sDescription, $sLanguage, $sWebMaster;
  35. $titleKey = "^RSS^CHANNEL^TITLE";
  36. $linkKey = "^RSS^CHANNEL^LINK";
  37. $descKey = "^RSS^CHANNEL^DESCRIPTION";
  38. $langKey = "^RSS^CHANNEL^LANGUAGE";
  39. $webmKey = "^RSS^CHANNEL^WEBMASTER";
  40. if ($curTag == $titleKey) {
  41. $sTitle = $data;
  42. }
  43. elseif ($curTag == $linkKey) {
  44. $sLink = $data;
  45. }
  46. elseif ($curTag == $descKey) {
  47. $sDescription = $data;
  48. }
  49. elseif ($curTag == $langKey) {
  50. $sLanguage = $data;
  51. }
  52. elseif ($curTag == $webmKey) {
  53. $sWebMaster = $data;
  54. }
  55.  
  56.  // now get the items 
  57.  global $arItems, $itemCount;
  58.  $itemTitleKey = "^RSS^CHANNEL^ITEM^TITLE";
  59.  $itemLinkKey = "^RSS^CHANNEL^ITEM^LINK";
  60.  $itemUpdKey = "^RSS^CHANNEL^ITEM^UPDATED";
  61.  $itemDescKey = "^RSS^CHANNEL^ITEM^DESCRIPTION";
  62.  $itemAuthKey = "^RSS^CHANNEL^ITEM^AUTHOR";
  63.  if ($curTag == $itemTitleKey) {
  64.  // make new xItem
  65.  $arItems[$itemCount] = new xItem();  
  66.  // set new item object's properties
  67.  $arItems[$itemCount]->xTitle = $data;
  68.  }
  69.  elseif ($curTag == $itemLinkKey) {
  70.  $arItems[$itemCount]->xLink = $data;
  71.  }
  72.  elseif ($curTag == $itemUpdKey) {
  73.  $arItems[$itemCount]->xUpdated = $data;
  74.  } 
  75.  elseif ($curTag == $itemDescKey) {
  76.  $arItems[$itemCount]->xDescription = $data;
  77.  }
  78.  elseif ($curTag == $itemAuthKey) {
  79.  $arItems[$itemCount]->xAuthor = $data;
  80.  // increment item counter
  81.  $itemCount++;
  82. }
  83.  
  84. // main loop
  85. $xml_parser = xml_parser_create();
  86. xml_set_element_handler($xml_parser, "startElement", "endElement");
  87. xml_set_character_data_handler($xml_parser, "characterData");
  88. if (!($fp = fopen("rss.xml","r"))) {
  89. die ("could not open RSS for input");
  90. }
  91. while ($data = fread($fp, 4096)) {
  92. if (!xml_parse($xml_parser, $data, feof($fp))) {
  93. die(sprintf("XML error: %s at line %d", 
  94. xml_error_string(xml_get_error_code($xml_parser)), 
  95. xml_get_current_line_number($xml_parser)));
  96. }
  97. }
  98. xml_parser_free($xml_parser);
  99.  
  100. ?>



  1. <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  2. <link rel="alternate" type="application/rss+xml" title="4asy" href="rss.xml" />
  3. <title>Czteryasy</title>
  4. </head>

  1. <?php
  2. for($x=0;$x<count($arItems);$x++){
  3. echo $arItems[$x]->xTitle;
  4. echo "<br/>";
  5. echo $arItems[$x]->xUpdated;
  6. echo "<br/>";
  7. echo $arItems[$x]->xDescription;
  8. echo "<br/>";
  9. echo "<a href=" . $arItems[$x]->xLink . ">Zobacz całość</a>";
  10. echo "<br/>";
  11. echo $arItems[$x]->xAuthor;
  12. }
  13. ?>

  1. </body>
  2. </html>


i taki generowany plik rss

  1. <rss version="2.0">
  2. <channel>
  3.  <title>tytuł</title>
  4.  <link>link</link>
  5.  <description>opi</description>
  6.  <language>pl</language>
  7.  <webMaster>email</webMaster>
  8.  <image>
  9.   <title>tytuł</title>
  10.   <link>link</link>
  11.   <url>url</url>
  12.   <height>height</height>
  13.   <width>width</width>
  14.  </image>
  15.  <item>
  16.   <title>tytuł</title>
  17.   <link>link</link>
  18.   <updated>data</updated>
  19.   <description>opis</description>
  20.   <author>email</author>
  21.  </item>
  22. </channel>
  23. </rss>


po uruchomieniu skryptu php wyświetla mi się
Parse error: syntax error, unexpected $end on line 123. POMOCY
Sh4dow
proste nie zamknoles tego
Kod
30  function characterData($parser, $data) {

i brakuje gdzies znaczka
Kod
}
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.