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: Read and show Slashdot's RDF-feed

Example #1

Comment:

This code reads the Resource Description Framework (RDF) of Slashdot and presents it in the browser, using a small piece of javascript. A list of all items in the RDF is retreived using the getElementdBytagName method. In a loop, all items are traversed and the properties of each item are put in an associative array for easier access.

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
<script type="text/javascript">
    function dspSwitch(id) {
        var d = document.getElementById(id).style;
        d.display = (d.display == 'block')?'none':'block';
    }
</script>
<?php
    
// include xml lib
    
include_once('../lib.xml.inc.php');

    
// convert an rdf item to an associative array
    
function asArray($item) {
        
$a = array();
        while (
$item) {
            
$a[$item->nodeName] = $item->firstChild->nodeValue;
            
$item $item->nextSibling;
        }
        return 
$a;
    }

    
// get the feed
    
$rdf = new XML('http://slashdot.org/index.rss');

    
// get article items
    
$items $rdf->getElementsByTagName('item');

    
// loop and gather info
    
$out '';
    foreach (
$items as $k => $item) {
        
$a asArray($item->firstChild);
        
$out .= '<a href="javascript:;" '.
            
'onclick="dspSwitch(\'item'.$k.'\')">'.
            
$a['title'].'</a> ('.$a['dc:date'].')<br />'.
            
'<div id="item'.$k.'" style="display:none">'.
            
$a['description'].
            
'&nbsp;[<a href="'.$a['link'].'">more</a>]'.
            
'<br /><br /></div>';
    }

    
// display info
    
echo $out;

?>

Output:

Sean Parker Builds Beach-Access App To Atone For His Rule-Violating Wedding (2018-12-16T11:34:00+00:00)
Study Reveals The Most Googled 'Should I' Questions In Each State (2018-12-16T08:34:00+00:00)
Is The World Shifting To 'Ambient Computing'? (2018-12-16T05:34:00+00:00)
How Microsoft Embraced Python (2018-12-16T02:34:00+00:00)
People Are Harassing Waymo's Self-Driving Vehicles (2018-12-16T00:34:00+00:00)
CNN Contributor Urges: Stop Calling Facebook a Tech Company (2018-12-15T23:35:00+00:00)
Do Alternative Software Licenses Represent Open Source's 'Midlife Crisis'? (2018-12-15T22:34:00+00:00)
Study Suggests Too Much Collaboration Actually Hurts Productivity (2018-12-15T21:34:00+00:00)
Ask Slashdot: Is There An Open Source Tool Measuring The Sharpness of Streaming Video? (2018-12-15T20:34:00+00:00)
One Year After Net Neutrality Repeal, America's Democrats Warn 'The Fight Continues' (2018-12-15T19:34:00+00:00)
Was Commodore's Amiga 'A Computer Ahead of Its Time'? (2018-12-15T18:34:00+00:00)
Doctor Who Won't Return Until 2020 (2018-12-15T17:34:00+00:00)
Screen Time Changes Structure of Kids' Brains, NIH Study Shows (2018-12-15T16:34:00+00:00)
OpenJDK Bug Report Complains Source Code 'Has Too Many Swear Words' (2018-12-15T15:34:00+00:00)
Could You Live Without a Smartphone For a Year? (2018-12-15T13:00:00+00:00)