Hi,

Is there any way in PHP to read an XML file in and bind the contents to an instance of a given class?

I've done something similar in .NET using serialization which reads a XML config file and then automatically populates the properties of a Config object.

E.g.

// Create an instance of the XmlSerializer specifying type and namespace.
XmlSerializer serializer = new XmlSerializer(typeof(Config));

// A FileStream is needed to read the XML document.
FileStream fs = new FileStream(filename, FileMode.Open);
XmlReader reader = new XmlTextReader(fs);

// Use the Deserialize method to restore the object's state.
Config appConfig;
appConfig = (Config) serializer.Deserialize(reader);

Hence you have an object called appConfig of type Config.

TIA

Dave

    If you're running PHP5, you might want to take a look at the [man]SimpleXML[/man] functions. For instance:

    class Example
    {
       var $xml = NULL;
       function getXml($fileName)
       {
          $xml = simplexml_load_file($fileName);
          if($xml !== FALSE)
          {
             $this->xml = $xml;
             return(TRUE);
          }
          return(FALSE);
       }
    }
    // use it:
    $myObject = new Example();
    $myObject->getXml("filename.xml");
    // view data:
    printf("<pre>%s</pre>", print_r($myObject->xml));
    

    I

      Thanks. So this will get me the XML contents. I'm assuming that I would need to loop through the XML contents and assign each one to a property of my class?

      If this is the case, is there not an easy way to load XML and pre-populate class properties?

      Thanks

      NogDog wrote:

      If you're running PHP5, you might want to take a look at the [man]SimpleXML[/man] functions. For instance:

      class Example
      {
         var $xml = NULL;
         function getXml($fileName)
         {
            $xml = simplexml_load_file($fileName);
            if($xml !== FALSE)
            {
               $this->xml = $xml;
               return(TRUE);
            }
            return(FALSE);
         }
      }
      // use it:
      $myObject = new Example();
      $myObject->getXml("filename.xml");
      // view data:
      printf("<pre>%s</pre>", print_r($myObject->xml));
      

      I

        The simplexml_load_file() function will create an object with a hierarchical data structure, for which you can then either directly access elements via a syntax like $xml->element_name->sub_element_name[0]->foobar, or you could walk through the data tree using the children() and attribute() methods.

        Or, I might be totally misunderstanding what you're trying to do. 🙂

        Check out the examples on the [man]SimpleXML[/man] page, as they give a decent introduction to what you can do with this class.

          NogDog, thanks for info. I think I'll take the approach of loading the XML file then looping through each element and assign them to the properties of the class.

          Cheers

          Dave

          NogDog wrote:

          The simplexml_load_file() function will create an object with a hierarchical data structure, for which you can then either directly access elements via a syntax like $xml->element_name->sub_element_name[0]->foobar, or you could walk through the data tree using the children() and attribute() methods.

          Or, I might be totally misunderstanding what you're trying to do. 🙂

          Check out the examples on the [man]SimpleXML[/man] page, as they give a decent introduction to what you can do with this class.

            Write a Reply...