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: CharacterData interface tests

Example #1

Comment:

This series of code snippets test the functionality of the XML_CharacterData interface.
The first test creates a simple comment tag and shows the result.

Code:

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

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

    
// create comment tag and show it
    
$comment $xml->createComment('Hello World!');
    echo 
$comment->toString();
?>

Output:

<!-- Hello World! -->

Example #2

Comment:

This snippet inserts a piece of text in the previously created comment, using the insertData method, and shows the new comment after the insertion.

Code:

1
2
3
4
<?php
    $comment
->insertData(6'Beautiful ');
    echo 
$comment->toString();
?>

Output:

<!-- Hello Beautiful World! -->

Example #3

Comment:

Tests the substringData method, which grabs a piece of the data with a given length (2nd argument), starting at a given offset (1st argument).

Code:

1
2
3
<?php
    
echo $comment->substringData(015);
?>

Output:

Hello Beautiful

Example #4

Comment:

The appendData method adds a piece of data at the end of the existing data.

Code:

1
2
3
4
<?php
    $comment
->appendData(' And Good Bye...');
    echo 
$comment->toString();
?>

Output:

<!-- Hello Beautiful World! And Good Bye... -->

Example #5

Comment:

The deleteData method deletes a piece of data, starting at given offset (1st argument) and with given length (2nd argument).

Code:

1
2
3
4
<?php
    $comment
->deleteData(156);
    echo 
$comment->toString();
?>

Output:

<!-- Hello Beautiful! And Good Bye... -->

Example #6

Comment:

The replaceData method replaces a piece of data with the supplied string. This method expects three arguments: the offset, the length of the data to be replaced, and the new text to insert.

Code:

1
2
3
4
<?php
    $comment
->replaceData(69'Ugly');
    echo 
$comment->toString();
?>

Output:

<!-- Hello Ugly! And Good Bye... -->