UP PREVIOUS NEXT   Technologies Internet et Education, © TECFA
  4. Document Object Model (DOM ) définition

4. Document Object Model (DOM ) définition

l'API DOM de PhP qui ressemble à celui d'autres langages de programmation, définit 5 objets:

(Ici on introduit DomDocument et DomNode)

et il a 3 fonctions (qui rétournent DomDocument):

4.1 L'objet "DomDocument"

Propriétés:
Méthodes:

4.2 L'objet "DomNode"

Propriétés:
Méthodes:

Exemple 4-1: Extraction des attributs

 
<?php 
 
/* This is an example for getting the attribute "id" of a node "para" using the xmldocfile - function. */
 
$doc = xmldocfile("test.xml") or die("*** There is no such file ***");

 
$node = $doc->children(); 
$node = $node[0]->children(); 
$attr = $node[1]->attributes(); 
 
printf("%s=%s", $attr[0]->name, $attr[0]->children[0]->content); 
 
?>

Exemple 4-2: (complet) Extraction des travaux staf14 d'un étudiant staf-g

# iterate through an array of nodes
# looking for a text node
# return its content
function get_content($parent)
{
    $nodes = $parent->children();
    while($node = array_shift($nodes)) {
        if ($node->type == XML_TEXT_NODE)
            return $node->content;
    }    
    return ""; }
 
# get the content of a particular node
function find_content($parent,$name)
{
    $nodes = $parent->children();
    while($node = array_shift($nodes)) {
        if ($node->name == $name)
            return get_content($node);
    }
    return ""; }
# get an attribute from a particular node
function find_attr($parent,$name,$attr)
{
    $nodes = $parent->children();
    while($node = array_shift($nodes))
        if ($node->name == $name)
            return $node->getattr($attr);
    return "";
}
# load xml doc
$doc = xmldocfile("travaux.xml") or die("*** There is no such file ***");
 
# get root Node (student)
$root = $doc->root();
 
# get an array of students' children
$student_children = $root->children();
 
# go through the array of students' children
   while($curr_node = array_shift($student_children)) {
    # if you find "personal-data" extract several info    
        if ($curr_node->name == "personal-data") {
        # get some data from personal-data
        $first_name = find_content($curr_node,"first-name");
        $family_name = find_content($curr_node,"family-name");
        $email = find_content($curr_node,"email");
        echo "<h2><a href='mailto:$email'>".$first_name." ".$family_name."</a></h2>\n";
    }
    # if you find "exercises" extract some info
        if ($curr_node->name == "exercises") {
        $exercises = $curr_node->children();
        while($exercise = array_shift($exercises)) {
                if ($exercise->type == XML_TEXT_NODE)
                    continue;
            # get some data from each exercise
            $staf = find_attr($exercise,"staf","no");
            $no = find_attr($exercise,"staf","ex-number");    
            $title = find_content($exercise,"title");
            $url = find_content($exercise,"url");
            if ($staf == "14") {
                echo "<b>Staf14-ex".$no.": </b> <a href='".$url."'>".$title."</a><br>\n";
            }
        }
    }
}

UP PREVIOUS NEXT -- TIE