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: Creating a DOM from file and by using methods

Example #1

Comment:

Although very basic, this code tests several aspects of phpdomxml: it creates a new DOM by loading and parsing an external file. The resulting object is then showed by using the toString method on the root-level element of the created object. toString excepts one optional argument: a boolean, telling the method to create pretty layout (true) or layout without whitespace (the default no argurment, or false).

Code:

1
2
3
4
5
6
7
8
9
10
<?php
    
// include xml lib
    
include_once('../lib.xml.inc.php');

    
// get some xml
    
$xml = new XML('includes/some.xml');

    
// show it
    
echo $xml->toString(1);
?>

Output:

<xml>
    <item id="item1">
        this is a text-node
    </item>
    <item id="item2">
        and here is another one
    </item>
    <item id="item3">
        this is the 3rd element
    </item>
    <item id="item4">
        and this is the last item
    </item>
</xml>

Example #2

Comment:

Another way to create the same document would be by using dom-methods. For this, several methods need to be used: createElement to create a node element, createTextNode to create a text node, appendChild to append a child to a parent node and finally setAttribute to set an attribute of a node.

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
38
39
40
41
42
<?php
    
// include xml lib
    
include_once('../lib.xml.inc.php');

    
// create new xml object
    
$xml = new XML();

    
// create xml element
    
$root $xml->createElement('xml');
    
$xml->appendChild($root);

    
// create 1st item
    
$item1 $xml->createElement('item');
    
$item1->setAttribute('id''item1');
    
$item1->appendChild(
        
$xml->createTextNode('this is a text-node'));
    
$root->appendChild($item1);

    
// create 2nd item
    
$item2 $xml->createElement('item');
    
$item2->setAttribute('id''item2');
    
$item2->appendChild(
        
$xml->createTextNode('and here is another one'));
    
$root->appendChild($item2);

    
// create 3rd item
    
$item3 $xml->createElement('item');
    
$item3->setAttribute('id''item3');
    
$item3->appendChild(
        
$xml->createTextNode('this is the 3rd element'));
    
$root->appendChild($item3);

    
// create 4th item
    
$item4 $xml->createElement('item');
    
$item4->setAttribute('id''item4');
    
$item4->appendChild(
        
$xml->createTextNode('and this is the last item'));
    
$root->appendChild($item4);

    
// show the dom
    
echo $xml->toString(1);
?>

Output:

<xml>
    <item id="item1">
        this is a text-node
    </item>
    <item id="item2">
        and here is another one
    </item>
    <item id="item3">
        this is the 3rd element
    </item>
    <item id="item4">
        and this is the last item
    </item>
</xml>

Example #3

Comment:

When data is retreived from a list, you'll have to be careful not to overwrite previously created elements. appendChild uses a reference to the appended child when adding it to the parent object. So if the variable name is reused, the previous child will be killed. The solution to this is using php's variable variables, as shown in the code below (at line 23 and on).

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
38
39
40
41
42
43
<?php
    
// include xml lib
    
include_once('../lib.xml.inc.php');

    
// define list
    
$list = array(
        
=> '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);
?>

Output:

<xml>
    <item id="item1">
        this is a text-node
    </item>
    <item id="item2">
        and here is another one
    </item>
    <item id="item3">
        this is the 3rd element
    </item>
    <item id="item4">
        and this is the final item!
    </item>
</xml>