$tags = array("Entry");
$child_tags = array();
$parser = new XML_PullParser("DNS.xml", $tags, $child_tags);
while ($token = $parser->XML_PullParser_getToken()) {
    $seq = $parser->XML_PullParser_getSequence();
    for ($i = 0; $i < count($seq); $i++) {
        list($element, $which) = each($seq[$i]);
        switch ($element) {
            case 'IPADDRESS':
                echo "{$element}: {$which}\n";
                echo $parser->XML_PullParser_getText($element, $which) . "\n";
                break;
            case 'SERVER':
                echo "{$element}: {$which}\n";
                echo $parser->XML_PullParser_getText($element, $which) . "\n";
                $ip = $parser->XML_PullParser_getAttributes($element, $which);
                echo "\tIP: " . $parser->XML_PullParser_getAttrVal('ip', $ip) . "\n";
                break;
            case 'DOMAIN':
                echo "{$element}: {$which}\n";
                echo $parser->XML_PullParser_getText($element, $which) . "\n";
                break;
            case 'ALIAS':
                echo "{$element}: {$which}\n";
                echo $parser->XML_PullParser_getText($element, $which) . "\n";
                break;
            default:
                echo "default: {$element}: {$which}\n";
        }
    }
}
Beispiel #2
0
function toc($file)
{
    global $handle, $html_handle;
    global $files;
    $tags = array("para");
    $child_tags = array("ulink");
    $parser = new XML_PullParser($file, $tags, $child_tags);
    $next_url = "";
    $prev_url = "";
    while ($token = $parser->XML_PullParser_getToken()) {
        if ($link = $parser->XML_PullParser_getElement('ulink')) {
            while ($link = $parser->XML_PullParser_nextElement()) {
                $url_array = $parser->XML_PullParser_getAttributes($link);
                $url = $parser->XML_PullParser_getAttrVal('url', $url_array);
                $link_text = trim($parser->XML_PullParser_getText($link));
                $type = $parser->XML_PullParser_getAttrVal('type', $url_array);
                if ($type == "next") {
                    echo "<A href=\"article2html.php?fn={$url}\">{$link_text}</A><br>\n";
                    writeEntry($handle, "{$url}", $link_text);
                    writeHTMLEntry($html_handle, "{$url}", $link_text);
                    $next_url = $url;
                    $files[$next_url] = true;
                }
                if ($type == "prev") {
                    //  echo "Prev:  $link_text\n";
                }
            }
        }
    }
    $parser->XML_PullParser_free();
    return $next_url;
}
<?php

require_once "XML_PullParser.inc";
$tags = array("entry");
$child_tags = array("server", "ipaddress", "domain");
$parser = new XML_PullParser("DNS.xml", $tags, $child_tags);
while ($token = $parser->XML_PullParser_getToken()) {
    $parser->XML_PullParser_getElement('server');
    $seq = $parser->XML_PullParser_getSequence();
    for ($i = 0; $i < count($seq); $i++) {
        list($server, $which) = each($seq[$i]);
        $name = $parser->XML_PullParser_getText($server, $which);
        echo "Name: {$name}\n";
        $ip = $parser->XML_PullParser_getAttributes($server, $which);
        echo "\tIP: " . $parser->XML_PullParser_getAttrVal('ip', $ip) . "\n";
    }
}
/*
 Result
        Name:  example_1.com
                IP: 192.168.10.1
        Name:  example_2.com
                IP: 192.168.10.2
        Name:  example_3.com
                IP: 192.168.10.3
*/
<?php

require_once "XML_PullParser.inc";
XML_PullParser_excludeBlanks(true);
XML_PullParser_trimCdata(true);
$tags = array("DNS");
$child_tags = array("Entry");
$parser = new XML_PullParser("DNS.xml", $tags, $child_tags);
while ($token = $parser->XML_PullParser_getToken()) {
    $entry = $parser->XML_PullParser_getElement("entry");
    $child = $parser->XML_PullParser_getChild("server", 2, $entry);
    $child = $parser->XML_PullParser_getText("server", 2, $entry);
    echo "{$child}\n";
    $attr_array = $parser->XML_PullParser_getAttributes("server", 2, $child);
    if ($attr_array) {
        foreach ($attr_array as $n => $v) {
            echo "{$n} -- {$v}\n";
        }
    } elseif ($attr_array === FALSE) {
        $err = XML_PullParser_Errors_userDefined("Bad Attributes array");
        echo XML_PullParser_Errors_getUserDefined($err) . "\n";
        echo XML_PullParser_Errors_Trace() . "\n";
        exit;
    }
}
/* Result
example_2.com
User defined error: Bad Attributes array
Line: 26, Top Level

------Error Trace------------
<?php

require_once "XML_PullParser.inc";
$tags = array("Entry");
$child_tags = array("server");
$parser = new XML_PullParser("DNS.xml", $tags, $child_tags);
while ($token = $parser->XML_PullParser_getToken()) {
    $parser->XML_PullParser_getElement('server');
    while ($server = $parser->XML_PullParser_nextElement()) {
        $attr_array = $parser->XML_PullParser_getAttributes($server);
        $ip = $parser->XML_PullParser_getAttrVal("ip", $attr_array);
        echo "Server IP: {$ip}\n";
    }
    echo "\n";
}
/* Result 
    Server IP: 192.168.0.1
    Server IP: 192.168.0.2
    Server IP: 192.168.0.3
    Server IP: 192.168.0.4
    
*/
function getHeader($file)
{
    $tags = array("title", "articleinfo");
    $child_tags = array("");
    $parser = new XML_PullParser($file, $tags, $child_tags);
    echo "<div class='header'>";
    while ($token = $parser->XML_PullParser_getToken()) {
        if ($parser->XML_PullParser_isTypeOf("title", $token)) {
            $title = $parser->XML_PullParser_getText();
            $subtitle = $parser->XML_PullParser_getAttributes("title");
            echo "<span class='title'>{$title}</span><br>\n";
            if ($subtitle) {
                echo '<span class="subtitle">' . $subtitle['ROLE'] . "</span><br>\n";
            }
        } elseif ($parser->XML_PullParser_isTypeOf("articleinfo", $token)) {
            $surname = $parser->XML_PullParser_getText("surname");
            $firstname = $parser->XML_PullParser_getText("firstname");
            $version = $parser->XML_PullParser_getText("releaseinfo");
            $email = $parser->XML_PullParser_getText("email", 1);
            $subtitle_2 = $parser->XML_PullParser_getText("subtitle");
            if ($version) {
                echo "<b>{$version}</b><br>\n";
            }
            echo "<b>{$firstname} {$surname}</b><br>\n";
            if ($email) {
                echo "<b>{$email}</b><br><br>\n";
            }
            echo "<span class='subtitle_2'>{$subtitle_2}</span><br>\n";
            break;
        }
    }
    $parser->XML_PullParser_free();
    echo "</div>";
    echo "<p><div class='block'>";
    echo '<table width=800 cellpadding = 8><tr><td align = "right"><A href="contents.html" class="navigation_2">Contents</a>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td></table>';
    echo "</div>";
}
<?php

require_once "XML_PullParser.inc";
XML_PullParser_trimCdata(true);
$tags = array("Entry");
$child_tags = array("server");
$parser = new XML_PullParser("DNS.xml", $tags, $child_tags);
while ($token = $parser->XML_PullParser_getToken()) {
    $parser->XML_PullParser_getElement('server');
    $n = 1;
    while ($server = $parser->XML_PullParser_getText('server', $n)) {
        $ip = $parser->XML_PullParser_getAttributes('server', $n);
        echo "Name: {$server}\n";
        echo "\tIP: " . $parser->XML_PullParser_getAttrVal('ip', $ip) . "\n";
        $n++;
    }
}
/*
 Result 
        Name: example_1.com
                IP: 192.168.10.1
        Name:  example_2.com
                IP: 192.168.10.2
        Name:  example_3.com
                IP: 192.168.10.3
*/