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: XML_Node's removeChild method test

Example #1

Comment:

This code tests the removeChild method of the XML_Node interface. removeChild removes the supplied child from it's parent in the XML-tree. The first code snippet loads a piece of XML and displays it.

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:

This second code snippet retreives one item from the XML-tree, using the getElementById method, and displays it.

Code:

1
2
3
4
5
6
7
<?php
    
// get an item (from root)
    
$item $xml->getElementById('item3');

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

Output:

<item id="item3">
    this is the 3rd element
</item>

Example #3

Comment:

And this code snippet finally removes the selected item from the tree and displays the new XML structure.

Code:

1
2
3
4
5
6
7
<?php
    
// remove item item from its parent
    
$xml->firstChild->removeChild($item);

    
// show new tree
    
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="item4">
        and this is the last item
    </item>
</xml>