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:

Facebook Tool To Export Pictures To Google Photos Now Available (2020-06-06T00:10:00+00:00)
Never-Before-Seen 'Black Nitrogen' Plugs Puzzle in Periodic Table (2020-06-05T23:30:00+00:00)
And That's Really It For Google+ (2020-06-05T22:50:00+00:00)
IBM Releases Fully Homomorphic Encryption Toolkit For iOS and MacOS (2020-06-05T22:10:00+00:00)
Apple Watch Fall Detection Credited With Saving Unresponsive Arizona Man (2020-06-05T21:30:00+00:00)
Solving Online Events (2020-06-05T20:50:00+00:00)
States Are Leaning Toward a Push To Break Up Google's Ad Tech Business (2020-06-05T20:50:00+00:00)
Instacart Makes Changes To Tip Policy Following Shopper Complaints (2020-06-05T20:10:00+00:00)
Under Pressure, UK Government Releases NHS COVID Data Deals With Big Tech (2020-06-05T19:33:00+00:00)
Matthew Green on Zoom Not Offering End-To-End Encryption To Free Users (2020-06-05T18:42:00+00:00)
Apple Launches Open Source Project to Let Password Management Apps Create Strong Passwords (2020-06-05T18:12:00+00:00)
Reddit Co-founder Ohanian Resigns From Board, Urges Company To Replace Him With a Black Candidate (2020-06-05T17:37:00+00:00)
How Apple Decides Which Products Are 'Vintage' and 'Obsolete' (2020-06-05T16:50:00+00:00)
Slack Removed a Blog Post Showing How Police Use its Tech (2020-06-05T16:10:00+00:00)
Uber's Self-driving AI Predicts the Trajectories of Pedestrians, Vehicles, and Cyclists (2020-06-05T15:41:00+00:00)