Location: PHPKode > scripts > XML Array > xml-array/class.XmlArray.php
<?

/**
  * Class for store arrays in XML files.
  * @author Augusto Cesar Castoldi <hide@address.com>
  * @date 2003-11-21
  */
class XMLArray
{
  /**
    * Save the $array into the XML $xmlPath
    * @param array $array array of strings
    * @param string $xmlPath complete path of the xml file. Ex.: /home/user/test/XmlArrayTest.xml
    */
  function saveXML($array, $xmlPath)
  {
    $xml = domxml_new_doc("1.0");
    $elements = $xml->create_element("elements");
    $elementsNode = $xml->append_child($elements);
    for ($i = 0; $i < count($array); $i++)
    {
      $element = $xml->create_element("element");
      $element->set_content($array[$i]);
      $elementsNode->append_child($element);
    }
    $xml->dump_file($xmlPath, false, true);
  }

  /**
    * Reads the $xmlPath file and returns an array.
    * @param string $xmlPath complete path of the xml file. Ex.: /home/user/test/XmlArrayTest.xml
    * @return array
    */
  function getArray($xmlPath)
  {
    $i = 0;

    $xml = domxml_open_file ($xmlPath);
    $node = $xml->document_element();

    $ChildDomNode = $node->first_child();

    while($ChildDomNode)
    {
      if ($ChildDomNode->type == 1)
      {
        $array[$i] = trim($ChildDomNode->get_content());
        $i++;
      }
      $ChildDomNode = $ChildDomNode->next_sibling();
    }
    return $array;
  }
}

?>
Return current item: XML Array