<?php

// include xml lib
include_once '../lib.xml.inc.php';
// define list
$list = array(1 => 'this is a text-node', 'and here is another one', 'this is the 3rd element', 'and this is the final item!');
// create new xml object
$xml = new XML();
// create xml element
$root = $xml->createElement('xml');
$xml->appendChild($root);
// loop through list
foreach ($list as $i => $text) {
    // set name of variable variable
    $item = 'item' . $i;
    // create 'item' element
    ${$item} = $xml->createElement('item');
    // set an attribute, called 'id'
    ${$item}->setAttribute('id', $item);
    // append a text-node to 'item'
    ${$item}->appendChild($xml->createTextNode($text));
    // append the new item to the root element
    $root->appendChild(${$item});
}
// show the dom
echo $xml->toString(1);
Beispiel #2
0
 /**
  * При открытии тега, добавляем дите и устанавливаем указатель на него
  */
 private function tag_open($parser, $tag, $attributes)
 {
     $this->pointer =& $this->pointer->appendChild($tag, $attributes);
 }
Beispiel #3
0
 /**
  * @param XML $xml
  * @param array $data
  * @return void
  * @SuppressWarnings(PHPMD.ElseExpression)
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function appendDataXml(XML &$xml, array &$data)
 {
     foreach ($data as $key => &$value) {
         $child = new XML();
         $child->setName($key);
         $child->setCData(true);
         $child->setRoot(false);
         $type = gettype($value);
         $child->setAttribute('type', $type);
         switch ($type) {
             case 'boolean':
                 $child->setValue((int) $value);
                 break;
             case 'integer':
                 $child->setValue((int) $value);
                 break;
             case 'double':
             case 'float':
                 $child->setValue((double) $value);
                 break;
             case 'string':
                 $child->setValue((string) $value);
                 break;
             case 'array':
                 $this->appendDataXml($child, $value);
                 break;
             case 'object':
                 $child->setAttribute('class', get_class($value));
                 if ($value instanceof self) {
                     $array = $value->getArray();
                     $this->appendDataXml($child, $array);
                 } else {
                     $serialize = serialize($value);
                     $child->setValue($serialize);
                 }
                 break;
             case 'resource':
             case 'null':
             case 'unknown type':
             default:
                 $child->setValue('null');
                 break;
         }
         $xml->appendChild($child);
     }
 }
<?php

// include xml lib
include_once '../lib.xml.inc.php';
// define a list
$list = array('first in line', 'second in line', 'third in line', 'fourth in line', 'fifth in line');
// create empty xml dom object
$xml = new XML();
// create a tree
$refChild = null;
$doc = $xml->createElement('document');
for ($i = 0; $i < count($list); $i++) {
    // create new child
    $child = 'node' . ($i + 1);
    ${$child} = $xml->createElement('text-node');
    ${$child}->setAttribute('id', $child);
    ${$child}->appendChild($xml->createTextNode($list[$i]));
    // insert in tree & update refChild
    $refChild = $doc->insertBefore(${$child}, $refChild);
}
// append the created 'doc' element to the root
$xml->appendChild($doc);
// dump dom using toString
echo $xml->toString(1);