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:

Michael Bloomberg Donates Record $1.8 Billion To Johns Hopkins University; Donation Will Be Devoted Exclusively To Undergraduate Financial Aid (2018-11-18T22:00:00+00:00)
Antares Successfully Launches ISS Re-Supply Cargo Ship (2018-11-18T21:11:00+00:00)
Google To Pay JavaScript Frameworks To Implement Performance-First Code (2018-11-18T20:08:00+00:00)
Amazon Releases A No-Cost Distribution of OpenJDK (2018-11-18T18:56:00+00:00)
GitHub's Annual Report Reveals This Year's Top Contributor: Microsoft (2018-11-18T17:47:00+00:00)
Apple Finally Signs A Big Deal With a Hollywood Movie Studio (2018-11-18T16:16:00+00:00)
YouTube Now Streams Free Ad-Supported Movies -- Including 'The Terminator' and 'Hackers' (2018-11-18T15:15:00+00:00)
Compelling New Suspect For DB Cooper Skyjacking Found By Army Data Analyst (2018-11-18T14:15:00+00:00)
The Boring Company's First Tunnel Is All Dug Up (2018-11-18T10:10:00+00:00)
Mars Opportunity Rover Appears To Contact Earth; Turns Out To Be a False Alarm (2018-11-18T07:07:00+00:00)
Some Birds Are Excellent Tool-Makers (2018-11-18T05:05:00+00:00)
Facebook Filed a Patent To Predict Your Household's Demographics Based On Family Photos (2018-11-18T03:30:00+00:00)
Man Spoofs GPS To Fake Shop Visits For Profit, Gets Caught (2018-11-18T01:30:00+00:00)
EFF, MuckRock Partner To See How Local Police Are Trading Your Car's Location (2018-11-17T23:23:00+00:00)
Bill Godbout, Early S-100 Bus Pioneer, Perished In the Camp Wildfire (2018-11-17T22:22:00+00:00)