Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]php i xml
dave666
post
Post #1





Grupa: Zarejestrowani
Postów: 255
Pomógł: 0
Dołączył: 1.04.2008

Ostrzeżenie: (0%)
-----


Witam mam takie pytanie czy poprzez skrypt php można dodać dane do pliku xml ? tak samo jak php dodaje do pliku testowego jakąś treść

chodzi mi o coś takiego skrypt otwiera plik jak wiadomo sprawdza ostatnią linie gdzie jest i za nią wpisuje kolejny ciąg znaków itd..

Ten post edytował dave666 7.04.2010, 07:36:54
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
batman
post
Post #2





Grupa: Moderatorzy
Postów: 2 921
Pomógł: 269
Dołączył: 11.08.2005
Skąd: 127.0.0.1




Cytat
czy poprzez skrypt php można dodać dane do pliku xml ?
Można.
Przetwarzanie XML


--------------------
I would love to change the world, but they won't give me the source code.
My software never has bugs. It just develops random features.
Go to the top of the page
+Quote Post
dave666
post
Post #3





Grupa: Zarejestrowani
Postów: 255
Pomógł: 0
Dołączył: 1.04.2008

Ostrzeżenie: (0%)
-----


znalazłem taki skrypt ale coś nie działa czy ktoś mógłby mi podpowiedzieć jak mogę go naprawić albo w którym miejscu jest błąd bo nie wyświetla mi skryptu wcale
  1. <?php
  2.  
  3. function tag_start($parser, $attr, $params){ // 1
  4. echo 'Znaleziono nowy tag: '.$attr.' z parametrami ';
  5. print_r($params);
  6. echo '<br>';
  7. }
  8.  
  9. function tag_end($parser, $attr){ // 2
  10. echo 'Koniec tagu '.$attr.'<br>';
  11. }
  12.  
  13. $parser = xml_parser_create(); // 3
  14.  
  15. xml_set_element_handler($parser, 'tag_start', 'tag_end'); // 4
  16.  
  17. if(!($fp = fopen($_GET['file'], "r"))) { // 5
  18. die("Nie można otworzyć podanego pliku XML!!!");
  19. }
  20.  
  21. while($data = fread($fp, 4096)) {
  22. if(!xml_parse($parser, $data, feof($fp))){ // 6
  23. die(sprintf("Błąd XML: %s w linii %d",
  24. xml_error_string(xml_get_error_code($_parser)),
  25. xml_get_current_line_number($parser)));
  26. }
  27. }
  28. xml_parser_free($parser); // 7
  29.  
  30. ?>
  31. <?php
  32.  
  33. function tag_start($parser, $attr, $params){ // 1
  34. echo 'Znaleziono nowy tag: '.$attr.' z parametrami ';
  35. print_r($params);
  36. echo '<br>';
  37. }
  38.  
  39. function tag_end($parser, $attr){ // 2
  40. echo 'Koniec tagu '.$attr.'<br>';
  41. }
  42.  
  43. $parser = xml_parser_create(); // 3
  44.  
  45. xml_set_element_handler($parser, 'tag_start', 'tag_end'); // 4
  46.  
  47. if(!($fp = fopen($_GET['file'], "r"))) { // 5
  48. die("Nie można otworzyć podanego pliku XML!!!");
  49. }
  50.  
  51. while($data = fread($fp, 4096)) {
  52. if(!xml_parse($parser, $data, feof($fp))){ // 6
  53. die(sprintf("Błąd XML: %s w linii %d",
  54. xml_error_string(xml_get_error_code($_parser)),
  55. xml_get_current_line_number($parser)));
  56. }
  57. }
  58. xml_parser_free($parser); // 7
  59. <?php
  60.  
  61. class redaktor{
  62. var $id;
  63. var $nick;
  64. var $imie;
  65. var $nazwisko;
  66. var $mail;
  67. var $www;
  68. var $funkcja;
  69. var $opis;
  70.  
  71. function redaktor($id){
  72. $this -> id = $id;
  73. $this -> www = '';
  74. }
  75.  
  76. function PobierzPersonalia(){
  77. return $this -> imie.' '.$this -> nazwisko;
  78. }
  79. }
  80. $int = 0;
  81. $redakcja = array();
  82. $czy_redaktor = 0;
  83. function tag_start($parser, $attr, $params){
  84. global $act_tag, $int, $redakcja, $czy_redaktor;
  85. if($attr == 'REDAKTOR' && $czy_redaktor == 1){
  86. die('Błąd: Niedozwolone zagnieżdżenie redaktorów! :]<br>');
  87. }elseif($attr == 'REDAKTOR' && $czy_redaktor == 0){
  88. $redakcja[$int] = new redaktor($params['ID']);
  89. $czy_redaktor = 1;
  90. }
  91.  
  92. if($czy_redaktor == 1){
  93. if($attr != 'PERSONALIA'){
  94. $act_tag = $attr;
  95. }else{
  96. $redakcja[$int] -> imie = $params['IMIE'];
  97. $redakcja[$int] -> nazwisko = $params['NAZWISKO'];
  98. }
  99. }
  100. }
  101. function tag_text($parser, $text){
  102. global $act_tag, $int, $redakcja, $czy_redaktor;
  103.  
  104. if($czy_redaktor == 1){
  105. switch($act_tag){
  106. case 'KSYWA': $redakcja[$int] -> nick .= $text; break;
  107. case 'MAIL': $redakcja[$int] -> mail .= $text; break;
  108. case 'WWW': $redakcja[$int] -> www .= $text; break;
  109. case 'PRAWA': $redakcja[$int] -> funkcja .= $text; break;
  110. case 'OPIS': $redakcja[$int] -> opis .= $text; break;
  111. }
  112. }
  113. }
  114. function tag_end($parser, $attr){
  115. global $act_tag, $int, $czy_redaktor;
  116. if($attr == 'REDAKTOR' && $czy_redaktor == 1){
  117. $int++;
  118. $czy_redaktor = 0;
  119. }
  120. }
  121. $parser = xml_parser_create();
  122.  
  123. xml_set_element_handler($parser, 'tag_start', 'tag_end');
  124. xml_set_character_data_handler($parser, 'tag_text');
  125.  
  126. if(!($fp = fopen('redakcja.xml', 'r'))) {
  127. die('Nie można otworzyć podanego pliku XML!!!');
  128. }
  129.  
  130. while($data = fread($fp, 4096)) {
  131. if(!xml_parse($parser, $data, feof($fp))){
  132. die(sprintf("Błąd XML: %s w linii %d",
  133. xml_error_string(xml_get_error_code($_parser)),
  134. xml_get_current_line_number($parser)));
  135. }
  136. }
  137. xml_parser_free($parser);
  138. echo '<center>REDAKCJA</center><br>';
  139. foreach($redakcja as $redaktor){
  140. echo '<hr>
  141. <b>Nick</b>: '.$redaktor -> nick.'<br>
  142. <b>Imię i nazwisko</b>: '.$redaktor -> PobierzPersonalia().'<br>
  143. <b>Mail</b>: <a href="mailto:'.$redaktor -> mail.'">'.$redaktor -> mail.'</a><br>
  144. <b>WWW</b>: '.(strlen($redaktor -> www) != 0 ? ('<a href="'.$redaktor -> www.'">'.$redaktor -> www.'</a>') : 'Brak').'<br>
  145. <b>Funkcja</b>: '.$redaktor -> funkcja.'<br>
  146. <b>Opis</b>: '.$redaktor -> opis.'<br>
  147. ';
  148. }
  149. ?>
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 21:46