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:

Google is Adding Anti-Tampering DRM To Android Apps in the Play Store (2018-06-24T20:01:00+00:00)
DC Comics Returns To Supermarket Shelves With New, Giant-Sized Comics (2018-06-24T19:00:00+00:00)
Warner Bros Is Cracking Down On Harry Potter Festivals (2018-06-24T18:00:00+00:00)
The Rise of the Video-Game Gambler (2018-06-24T17:01:00+00:00)
'Snapdragon 1000' Chip May Be Designed For PCs From the Ground Up (2018-06-24T16:00:00+00:00)
Some Science Journals That Claim To Peer Review Papers Do Not Do So (2018-06-24T15:00:00+00:00)
8 Months After a Surge of Complaints, Apple Announces a Repair Program For Its Flawed MacBooks and MacBook Pros (2018-06-24T14:00:00+00:00)
eBay and Amazon Delist Faulty Carbon Monoxide Alarms (2018-06-24T11:34:00+00:00)
NYT: 'Firefox Is Back. It's Time to Give It a Try.' (2018-06-24T07:34:00+00:00)
How Should Open Source Development Be Subsidized? (2018-06-24T03:34:00+00:00)
George Lucas's Terrible Idea for Star Wars Episodes 7-9 (2018-06-24T01:36:00+00:00)
Can Two Injections of Tuberculosis Vaccine Cure Diabetes? (2018-06-23T23:37:00+00:00)
Tesla Autopilot Safety Defeat Device Gets a Cease-and-Desist From NHTSA (2018-06-23T22:41:00+00:00)
Can NASA Protect Earth from Catastrophic Asteroid Collisions? (2018-06-23T21:45:00+00:00)
America's 'CyberWar' With Foreign Governments Could Get More Aggressive (2018-06-23T20:37:00+00:00)