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:

Philippine Lawmakers Worry China Telecom May Be a 'Trojan horse' (2018-01-17T14:08:00+00:00)
Salmonella Probably Killed the Aztecs (2018-01-17T13:00:00+00:00)
America's Fastest Spy Plane May Be Back -- And Hypersonic (2018-01-17T10:00:00+00:00)
Hackers Seem Close To Publicly Unlocking the Nintendo Switch (2018-01-17T07:00:00+00:00)
Lawsuit Filed By 22 State Attorneys General Seeks To Block Net Neutrality Repeal (2018-01-17T03:30:00+00:00)
New Study Claims That the 'Black Death' Was Spread By Humans, Not Rats (2018-01-17T01:45:00+00:00)
Many Enterprise Mobile Devices Will Never Be Patched Against Meltdown, Spectre (2018-01-17T00:50:00+00:00)
Google Home and Chromecast Could Be Overloading Your Home Wi-Fi (2018-01-17T00:10:00+00:00)
Lyft Says Nearly 250K of Its Passengers Ditched a Personal Car In 2017 (2018-01-16T23:30:00+00:00)
China Builds 'World's Biggest Air Purifier' That Actually Works (2018-01-16T22:50:00+00:00)
Bitcoin Plunges Below $12,000 To Six-Week Low Over Crackdown Fears (2018-01-16T22:10:00+00:00)
Researchers Uncover Android Malware With Never-Before-Seen Spying Capabilities (2018-01-16T21:30:00+00:00)
France Says 'Au Revoir' to the Word 'Smartphone' (2018-01-16T20:50:00+00:00)
The Astronomer Who Is Building the Largest Map of Space by Volume (2018-01-16T20:10:00+00:00)
The Human Cost of the Apple Supply Chain Machine (2018-01-16T19:26:00+00:00)