Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Mini parser XML, Klasa, PHP 4 i 5
kicaj
post 26.06.2005, 21:31:48
Post #1





Grupa: Zarejestrowani
Postów: 1 640
Pomógł: 28
Dołączył: 13.02.2003
Skąd: Międzyrzecz/Poznań

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


  1. <?php
  2.  
  3. class XML
  4. {
  5. // Dla PHP5
  6. function __construct( $strFileName )
  7. {
  8. if( file_exists( $strFileName ) )
  9. {
  10. $this -> resFileContent = file_get_contents( $strFileName );
  11. }
  12. else
  13. {
  14. // W razie niepowodzenia, zaladowanie pliku z opisem problemu
  15. $this -> resFileContent = file_get_contents( 'resource/error.xml' );
  16. }
  17. }
  18.  
  19. // Dla PHP4
  20. function XML( $strFileName )
  21. {
  22. $this -> __construct( $strFileName );
  23. }
  24.  
  25. function getContent( $strTagName )
  26. {
  27. preg_match_all( '|<'. $strTagName .'(.*)>(.*)</'. $strTagName .'>|isU', $this -> resFileContent, $arrContentTags );
  28.  
  29. for( $intIterTags = 0; $intIterTags < count( $arrContentTags[0] ); $intIterTags++ )
  30. {
  31. return $arrContentTags[2][$intIterTags];
  32. }
  33. }
  34.  
  35. function getAttributes( $strTagName, $strAttributeName )
  36. {
  37. preg_match_all( '|<'. $strTagName .'(.*)>(.*)</'. $strTagName .'>|isU', $this -> resFileContent, $arrContentTags );
  38.  
  39. for( $intIterTags = 0; $intIterTags < count( $arrContentTags[1] ); $intIterTags++ )
  40. {
  41. if( !empty( $arrContentTags[1][$intIterTags] ) )
  42. {
  43. $arrContentAttributes = explode( ' ', $arrContentTags[1][$intIterTags] );
  44.  
  45. array_shift( $arrContentAttributes );
  46.  
  47. for( $intIterAttributes = 0; $intIterAttributes < count( $arrContentAttributes ); $intIterAttributes++ )
  48. {
  49. $arrContentAttributesElements = explode( '=', $arrContentAttributes[$intIterAttributes] );
  50.  
  51. if( $arrContentAttributesElements[0] == $strAttributeName )
  52. {
  53. // Zabezpiecznie przed roznymi sposobami okreslenia wartosci atrybutow
  54. $arrDeleteQuotes[] = ''';
  55. $arrDeleteQuotes[] = '&#092;"';
  56.  
  57. return str_replace( $arrDeleteQuotes, '', $arrContentAttributesElements[1] );
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. ?>

XML:
  1. <? xml version="1.1" encoding="ISO-8859-2" ?>
  2.  
  3. <index>
  4.    <page>Strona główna</page>
  5.    <author>kicaj_</author>
  6.    <mail antispam="yes">kicaj@m-cz.net</mail>
  7.    <content htmlcode="yes">
  8.        Witamy w Lorem ipsum dolor sit amet,  consectetuer adipiscing elit. Cras ligula. Cras eu sapien. Maecenas
  9.        dignissim elit non dui interdum cursus. Nam sit amet velit vel dolor adipiscing tristique. Cras
  10.        elementum ultricies felis. <img src="img.jpg" align="right" style="float: right; margin-top: 5px;
  11.        margin-left: 3px;">Donec at risus quis nisl egestas nonummy. In augue. Maecenas tempor,
  12.        leo ut bibendum convallis, odio mauris rhoncus leo, a viverra libero odio vitae lorem. In id lacus.
  13.        Ut non ante ac nisl bibendum blandit. Maecenas varius dolor in ligula. Vivamus dapibus suscipit mi.
  14.        Proin fermentum augue non sem. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per
  15.        inceptos hymenaeos. Proin quis urna. Nunc in magna. Fusce eget lectus.<br />
  16.        <br />
  17.        Nam quam. Proin fermentum convallis nunc. Praesent dapibus ligula eget turpis. Phasellus a libero
  18.        non sem tincidunt posuere. Etiam congue massa. Mauris odio. Pellentesque lorem. Vestibulum sem ipsum,
  19.        tempor sit amet, sagittis at, viverra quis, pede. Sed quis sem. Curabitur vitae mi id tellus vulputate
  20.        luctus. Quisque non risus eu dolor aliquet blandit. Phasellus ipsum velit, commodo nec, tristique et,
  21.        consectetuer sit amet, diam.<br />
  22.        <br />
  23.    </content>
  24. </index>

Zastosowanie:
  1. <?php
  2. $startXML = new XML( 'resource.xml' );
  3.  
  4. echo $startXML -> getContent( 'page' ) .'<br />';
  5.  
  6. if( $startXML -> getAttributes( 'mail', 'antispam' ) == 'yes' )
  7. {
  8. echo str_replace( '@', '[at]', $startXML -> getContent( 'mail' ) );
  9. }
  10. else
  11. {
  12. echo $startXML -> getContent( 'mail' );
  13. }
  14. ?>

Wlasnie taki parser, byl mi potrzebny do jednej strony, bardzo prosty przykład, prosta klasa, jesli bedzie rozbudowywana to takze opublikuje ja.

Ten post edytował kicaj_ 26.06.2005, 22:40:08


--------------------
PHP Developer

"Nadmiar wiedzy jest równie szkodliwy jak jej brak" Émile Zola
Go to the top of the page
+Quote Post

Posty w temacie


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 Wersja Lo-Fi Aktualny czas: 31.07.2025 - 06:19