Posted By

hasantayyar on 07/10/09


Tagged

php xml


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

hasantayyar


XMLResourceBundle.class.php - XML Resource Bundle


 / Published in: PHP
 

URL: http://www.phpclasses.org/browse/file/23829.html

This class can be used to parse and extract values of properties defined in XML files in the format specified by Sun Microsystems.

It can take the path, file name and language of a XML properties file and parses it.

The class makes available the properties values by the means of an accessory function.

  1. <?php
  2.  
  3. /**
  4.  * 
  5. [email protected]
  6. [email protected]&copy; 2008, pizar
  7.  * 
  8.  * version 1.0
  9.  * Required PHP 5.0
  10.  * 
  11.  * The class is build to read from the xml structure define from the sun.
  12.  * 
  13.  * <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
  14.  * <?xml version="1.0" encoding="iso-8859-1"?>
  15.  * <properties> 
  16.  *     ...
  17.  *     <entry key="invio"><![CDATA[invio]]></entry>
  18.  *     ...
  19.  * </properties>
  20.  *  
  21.  */
  22. class XMLResourceBoundle{
  23.  
  24.     /**
  25.      * A private variable, who maintain the reference on the xml file
  26. [email protected]
  27. [email protected]
  28.      */
  29.     private $doc="";
  30.     
  31.     
  32.     
  33.     /**
  34.      * Constructor sets up the source file xml to read, and the language from where read.
  35.      * The structure bust be: ${path}/${language}/{$filename}
  36. [email protected]$path
  37. [email protected]$filename
  38. [email protected]$languageCode, the default is ""
  39.      */
  40.     function XMLResourceBoundle(string $path,string $filename, string $languageCode=""){
  41.         $complete_path="";
  42.         
  43.         $this->doc = new DomDocument;
  44.         $this->doc->preserveWhiteSpace = false;
  45.         
  46.         if ($languageCode!=""){
  47.             $complete_path=$path."/".$languageCode."/".$filename;
  48.         }else{
  49.             $complete_path=$path."/".$filename;
  50.         }
  51.         
  52.         $this->doc->load($complete_path);
  53.         $this->xpath = new DOMXPath($this->doc);
  54.     }
  55.     
  56.     /**
  57.      * Constructor sets up the source file xml to read, and the language from where read.
  58.      * The structure bust be: ${path}/${language}/{$filename}
  59. [email protected]
  60. [email protected]xml node, if the key is not found, 
  61.      * return the key with ??? before and after. 
  62.      */
  63.     function get(string $keyId){
  64.         $query = "//entry[@key='".$keyId."']";
  65.         $entries = $this->xpath->evaluate($query, $this->doc);
  66.         
  67.         if ($entries->item(0)->nodeValue!=""){
  68.             return($entries->item(0)->nodeValue);
  69.         }else{
  70.             return("???".$keyId."???");
  71.         }
  72.     }
  73. }
  74. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: NeoBlack on July 10, 2009

nice class, good job!

You need to login to post a comment.