Index  |  Quick Docs  |  Usage Examples

phpdomxml-0.9.0 Usage Examples (and test-case)

This set of usage examples shows live output of the presented code snippets. And besides being a useful example base, it also serves as a unit test system for the functionality of phpdomxml. Please pick a test batch to run from the list below.

1. Creating a DOM from file and by using methods (3 tests)
2. Dynamic DOM creation with XML_Node's insertBefore method (3 tests)
3. CharacterData interface tests (6 tests)
4. XML_Node's removeChild method test (3 tests)
5. Read and show Slashdot's RDF-feed (1 test)


Running batch: Dynamic DOM creation with XML_Node's insertBefore method

Example #1

Comment:

This code tests the functionality of the insertBefore method of the XML_Node interface. insertBefore inserts a new child (1st argument) in the node list, before a given reference child (2nd argument). In the process nextSibling, previousSibling and childNodes are re-assigned or rebuilt.
The first code snippet builds the DOM object, using insertBefore, and shows the object with toString. toString parses the object with the childNodes array, so when the output verifies with what you expect, the childNodes array is built correctly from within the insertBefore method.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?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);
?>

Output:

<document>
    <text-node id="node5">
        fifth in line
    </text-node>
    <text-node id="node4">
        fourth in line
    </text-node>
    <text-node id="node3">
        third in line
    </text-node>
    <text-node id="node2">
        second in line
    </text-node>
    <text-node id="node1">
        first in line
    </text-node>
</document>

Example #2

Comment:

This code snippet parses the DOM object with nextSibling, starting at firstChild, to check if references are correctly set.

Code:

1
2
3
4
5
6
7
<?php
    $item 
$xml->firstChild->firstChild;
    while (
$item) {
        echo 
$item->toString()."\n";
        
$item $item->nextSibling;
    }
?>

Output:

<text-node id="node5">fifth in line</text-node>
<text-node id="node4">fourth in line</text-node>
<text-node id="node3">third in line</text-node>
<text-node id="node2">second in line</text-node>
<text-node id="node1">first in line</text-node>

Example #3

Comment:

This code snippet parses the DOM object with previousSibling, starting at lastChild, to check if references are correctly set.

Code:

1
2
3
4
5
6
7
<?php
    $item 
$xml->firstChild->lastChild;
    while (
$item) {
        echo 
$item->toString()."\n";
        
$item $item->previousSibling;
    }
?>

Output:

<text-node id="node1">first in line</text-node>
<text-node id="node2">second in line</text-node>
<text-node id="node3">third in line</text-node>
<text-node id="node4">fourth in line</text-node>
<text-node id="node5">fifth in line</text-node>