PHP SimpleXML Functions

    PHP supports some SimpleXML extension functions that allow you to use the toolset that converts the XML to an object. Then these objects deal with property selectors and array iterators. These extensions are enabled by default, and you can use the default command line to disable them at the compile time. There is no requirement for the configuration directives to be defined in the php.ini file.

    Function & Description

    Simplexml_import_dom

    This function will allow you to provide a SimpleXMLElement object from a DOM node.

    Syntax

    simplexml_import_dom(node,classname);

    Example

    <?php
       $dom_data = new domDocument;
       $dom_data->loadXML("<data><local><feature>
          feature1</feature></local><local><feature>
          feature2</feature></local></data>");
         $x = simplexml_import_dom($dom_data);   
       echo $x->local[1]->feature;
    ?>

    Simplexml_load_file

    This function will allow you to do the conversion of a simple XML file into its SimpleXMLElement object.

    Syntax

    simplexml_load_file(file,classname,options,ns,is_prefix);

    Example

    <?php
      $demo_file = simplexml_load_file("demo.xml");
       print_r($demo_file);
    ?>

    Simplexml_load_string

    This function will allow you to do the conversion of the formatted XML string into a SimpleXMLElement object.

    Syntax

    simplexml_load_string(data,classname,options,ns,is_prefix);

    Example

    <?php
       $note = <<<XML
       <note>
          <to>Jacob</to>
          <from>CEO</from>
          <heading>Subject</heading>
          <body>Send file</body>
       </note>
       XML;
       $xml = simplexml_load_string($note);
       echo $xml->to . "<br>";
       echo $xml->from . "<br>";
       echo $xml->heading . "<br>";
       echo $xml->body;
    ?>

    People are also reading: