<?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
*/
Exemple #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";
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("ipaddress");
$parser = new XML_PullParser("DNS.xml", $tags, $child_tags);
while ($token = $parser->XML_PullParser_getToken()) {
    $ipaddress = $parser->XML_PullParser_getElement('ipaddress');
    echo "IP Adress: " . $parser->XML_PullParser_getText() . "\n";
}
/*  Result
        IP Adress: 172.20.19.6
    */
function parse_files($file)
{
    global $pattern, $constants_pattern;
    XML_PullParser_excludeBlanks(true);
    XML_PullParser_trimCdata(true);
    $tags = array("formalpara", "blockquote", "simpara");
    $child_tags = array('programlisting', "title", "para", "simplelist", "member");
    $parser = new XML_PullParser($file, $tags, $child_tags);
    XML_PullParser_excludeBlanks(true);
    XML_PullParser_trimCdata(true);
    $token = "";
    while ($token = $parser->XML_PullParser_getToken()) {
        if ($parser->XML_PullParser_isTypeOf("formalpara", $token)) {
            $title = $parser->XML_PullParser_getElement('title');
            $title = $parser->XML_PullParser_getText($title);
            if ($title) {
                if (preg_match_all('/XML_PullParser(_\\w+)/', $title, $matches)) {
                    index_functions($matches[1]);
                }
            }
            $para = $parser->XML_PullParser_getElement('para');
            if ($text = $parser->XML_PullParser_getText($para)) {
                if (preg_match_all($constants_pattern, $text, $matches)) {
                    index_constants($matches[1]);
                } elseif (preg_match_all('/XML_PullParser(_\\w+)/', $text, $matches)) {
                    index_functions($matches[1]);
                } elseif (preg_match_all($pattern, $text, $matches)) {
                    index_terms($matches[1]);
                }
            }
        } elseif ($parser->XML_PullParser_isTypeOf("blockquote", $token)) {
            $title = $parser->XML_PullParser_getText('title');
            if ($title) {
                if (preg_match_all('/XML_PullParser(_\\w+)/', $title, $matches)) {
                    index_functions($matches[1]);
                }
            }
        } elseif ($parser->XML_PullParser_isTypeOf("simpara", $token)) {
            $text = $parser->XML_PullParser_getText($token);
            if (preg_match_all($constants_pattern, $text, $matches)) {
                index_constants($matches[1]);
            } elseif (preg_match_all('/XML_PullParser(_\\w+)/', $text, $matches)) {
                index_functions($matches[1]);
            }
        }
    }
    $parser->XML_PullParser_free();
}
<?php

//  Listing 18
require_once "XML_PullParser.inc";
//  XML_PullParser_excludeBlanks(true);
$tags = array("entry");
$child_tags = array("server", "domain");
$parser = new XML_PullParser("DNS.xml", $tags, $child_tags);
$token = $parser->XML_PullParser_getToken();
echo $parser->XML_PullParser_getText() . "\n";
$el = $parser->XML_PullParser_getElement("server");
echo $parser->XML_PullParser_getText() . "\n";
$parser->XML_PullParser_getElement("domain");
echo $parser->XML_PullParser_getText() . "\n";
/*
Result
 
 172.20.19.6
  example.com
  example_1.com
  example_2.com
  example_3.com
  www.example.com

 example_1.com example_2.com example_3.coM
 example.com
*/
?>

function getBody($file)
{
    $tags = array("formalpara", "blockquote", "simpara", "indexdiv");
    $child_tags = array("classname", "ulink", "link", "indexentry");
    $parser = new XML_PullParser($file, $tags, $child_tags);
    $token = "";
    while ($token = $parser->XML_PullParser_getToken()) {
        if ($parser->XML_PullParser_isTypeOf("formalpara", $token)) {
            $td_prev = "";
            $td_next = "";
            $link_text = "";
            if ($link = $parser->XML_PullParser_getElement('ulink')) {
                while ($link = $parser->XML_PullParser_nextElement()) {
                    $link_text = $parser->XML_PullParser_getText($link);
                    $url_array = $parser->XML_PullParser_getAttributes($link);
                    $url = $parser->XML_PullParser_getAttrVal('url', $url_array);
                    $type = $parser->XML_PullParser_getAttrVal('type', $url_array);
                    if ($type == "next") {
                        $td_next = "<TD align='right' class='navigation'><b>Next: </b>";
                        $td_next .= "<a href='article2html.php?fn={$url}' class='navigation'>{$link_text}</a></td>";
                    }
                    if ($type == "prev") {
                        $td_prev = "<TD align='left' class='navigation'><b>Prev: </b>";
                        $td_prev .= "<a href='article2html.php?fn={$url}'>{$link_text}</a></td>";
                    }
                }
                if ($td_prev || $td_next) {
                    echo "<TABLE width='800'><TR>{$td_prev}" . $td_next;
                    echo "</table>&nbsp;&nbsp;&nbsp;\n";
                    continue;
                }
            }
            $parser->XML_PullParser_resetCurrentElement($token);
            //current element has to be restored from ulink
            $mark_up = $parser->XML_PullParser_getCSSSpans(array("code" => "code", "emphasis" => "emphasis", "classname" => "classname", "envar" => "code", "superscript" => "super"));
            //  $mark_up += $parser->XML_PullParser_getHTMLTags(array("classname"=>"b"));
            $anchor = "";
            if ($parser->XML_PullParser_isChildOf('anchor', $token)) {
                $anchor_id = $parser->XML_PullParser_getAttributes('anchor');
                $anchor_id = $parser->XML_PullParser_getAttrVal('id', $anchor_id);
                //echo "<A Name='$anchor_id'></a>";
                $anchor = "<A Name='{$anchor_id}'>";
            }
            if ($parser->XML_PullParser_isChildOf('simplelist', $token)) {
                $list = $parser->XML_PullParser_getChild('simpleList');
                $attr_array_role = $parser->XML_PullParser_getAttributes('simpleList');
                if ($attr_array_role) {
                    $start_num = $parser->XML_PullParser_getAttrVal('role', $attr_array_role);
                }
                if (!$start_num) {
                    $start_num = 1;
                }
                $which = 1;
                $items = "";
                while ($member = $parser->XML_PullParser_getChild('member', $which, $list)) {
                    $member_xcl = $parser->XML_PullParser_childXCL($member, 'phrase');
                    $member_text = $parser->XML_PullParser_getTextMarkedUp($mark_up, $member_xcl);
                    if ($phrase = $parser->XML_PullParser_getChild('phrase', 1, $member)) {
                        if ($phrase_text = $parser->XML_PullParser_getTextMarkedUp($mark_up, $phrase)) {
                            $member_text .= "\n<table width=600><td class='list_phrase'>" . trim($phrase_text) . "\n</table>\n";
                            rewrite_HTML_Link($member_text);
                        }
                    }
                    $items .= "<LI>" . trim($member_text) . "\n";
                    $which++;
                }
                $title = $parser->XML_PullParser_getTextMarkedUp($mark_up, 'title');
                if ($title) {
                    echo "<br>" . $title . "<br>";
                }
                /*
                  text in this para is assumed to be an intro to the list,
                  which means that any text following the list would be concatenated with the
                  text at top of list and printed at top of list   
                */
                $para = $parser->XML_PullParser_childXCL($token, 'simpleList', 'title');
                //remove simplelist
                if ($list_intro_text = trim($parser->XML_PullParser_getTextMarkedUp($mark_up, $para))) {
                    echo '<TABLE width="800"><TR><TD class="white_block">' . $list_intro_text . "</table>\n";
                }
                if ($anchor) {
                    echo "{$anchor} <OL TYPE = '1' START = '{$start_num}'> </a>";
                } else {
                    echo "<OL TYPE = '1' START = '{$start_num}'>\n";
                }
                echo $items;
                echo "</OL>\n";
                continue;
            }
            if ($parser->XML_PullParser_isChildOf('token', $token)) {
                $len = strlen($parser->XML_PullParser_getText('token'));
                $len *= 10;
                $len = round((800 - $len) / 2);
                if ($len < 10) {
                    $len = 20;
                }
                $len = $len . "px";
                $mark_up += $parser->XML_PullParser_getStyledTags(array("token" => "h4"), array("style" => "left:{$len}"));
            }
            echo '<p><div class="para">';
            if ($anchor) {
                echo $anchor . "</a>";
            }
            if ($parser->XML_PullParser_isChildOf('title', $token)) {
                $title = $parser->XML_PullParser_getTextMarkedUp($mark_up, 'title');
                if ($title) {
                    echo "<br>" . $title . "<br>";
                }
            }
            if ($parser->XML_PullParser_isChildOf('para', $token)) {
                $text = $parser->XML_PullParser_getTextMarkedUp($mark_up, "para");
                rewrite_HTML_Link($text);
                /*
                
                            if(preg_match('/href\s*=\s*".*?\.xml/',$text)) {
                               if($text = preg_replace('/href\s*="(.*?)xml/','href="article2html.php?fn=\\1xml',$text)) {
                                  echo "\n<!--   REPLACEMENT MADE -->\n";   
                               }
                            }
                */
                $text = preg_replace('/<\\s/', '&lt;', $text);
                $text = preg_replace('/\\s>/', '&gt;', $text);
                echo $text;
            }
            echo "</p></div>\n\n";
        } elseif ($parser->XML_PullParser_isTypeOf("blockquote", $token)) {
            if ($parser->XML_PullParser_isChildOf('anchor', $token)) {
                $anchor_id = $parser->XML_PullParser_getAttributes('anchor');
                $anchor_id = $parser->XML_PullParser_getAttrVal('id', $anchor_id);
                echo "<A Name='{$anchor_id}'>";
            }
            if ($attr = $parser->XML_PullParser_getAttributes("blockquote")) {
                $role = $parser->XML_PullParser_getAttrVal('role', $attr);
                if ($role == 'box' || $role == 'blank_box') {
                    $bgcolor = '#eeeeee';
                    $class = 'block';
                    if ($role == 'blank_box') {
                        $bgcolor = '#ffffff';
                        $class = 'white_block';
                    }
                    $mark_up = $parser->XML_PullParser_getCSSSpans(array("code" => "code", "emphasis" => "emphasis", "classname" => "classname"));
                    // $mark_up += $parser->XML_PullParser_getHTMLTags(array("classname"=>"b"));
                    $title = $parser->XML_PullParser_getText("title");
                    $list = $parser->XML_PullParser_getChild('simpleList');
                    $which = 1;
                    $items = "";
                    while ($member = $parser->XML_PullParser_getChild('member', $which, $list)) {
                        $member_text = $parser->XML_PullParser_getTextMarkedUp($mark_up, $member);
                        rewrite_HTML_Link($member_text);
                        $items .= "<tr><td class='{$class}'>" . trim($member_text) . "</td>\n";
                        $which++;
                    }
                    echo "<table border=1 bgcolor='" . $bgcolor . "' width = 750 cellpadding ='6'>\n" . "<tr><th align='left' class='{$class}'>{$title}" . $items . "</table>\n";
                    continue;
                }
            }
            $tab_x_2 = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
            $code = false;
            echo '<p><div class="block">';
            if ($anchor = $parser->XML_PullParser_getChild("anchor")) {
                $anchor_id = $parser->XML_PullParser_getAttributes($anchor);
                echo '<A Name="' . $parser->XML_PullParser_getAttrVal('id', $anchor_id) . '"></A>';
            }
            $title = $parser->XML_PullParser_getText("title");
            if ($attr = $parser->XML_PullParser_getAttributes("title")) {
                $role = $parser->XML_PullParser_getAttrVal('role', $attr);
                if ($role && preg_match('/^code$/i', $role)) {
                    $title = '<span class="code_title" style="font-style:italic;">' . $title . "</span>";
                    $code = true;
                }
            }
            $text = $parser->XML_PullParser_getText("programlisting");
            $text = preg_replace('/<\\s/', '&lt;', $text);
            $text = preg_replace('/\\s>/', '&gt;', $text);
            if (!$code) {
                $text = preg_replace('/^[\\t ]+/ms', "", $text);
                // remove leading spaces
            }
            // replace dummy lines of single dot with bullets
            $text = preg_replace('/^\\.\\s*$/ms', "{$tab_x_2}&bull;", $text);
            print "<center><b>{$title}</b></center>\n";
            print "<pre>{$text}</pre></div>\n";
        } elseif ($parser->XML_PullParser_isTypeOf("indexdiv", $token)) {
            XML_PullParser_excludeBlanks(true);
            XML_PullParser_trimCdata(true);
            $title = $parser->XML_PullParser_getText('title');
            echo "\n<P><b>{$title}</b>\n";
            $entries = $parser->XML_PullParser_getElement('indexentry');
            $seq = $parser->XML_PullParser_getSequence();
            $close_prevList = "";
            foreach ($seq as $index_entry) {
                list($index_tag, $index_val) = each($index_entry);
                if ($text = $parser->XML_PullParser_getText($index_tag, $index_val)) {
                    if ($url_array = $parser->XML_PullParser_getAttributes("ULINK", $index_val)) {
                        $url = $parser->XML_PullParser_getAttrVal('url', $url_array);
                    }
                    if (preg_match('/\\w/', $text)) {
                        write_index_element($index_tag, $text, $close_prevList, $url);
                        if (!$close_prevList) {
                            $close_prevList = "</DL>\n";
                        }
                    }
                }
            }
            echo $close_prevList;
        }
        if ($parser->XML_PullParser_isTypeOf("simpara", $token)) {
            if ($attr = $parser->XML_PullParser_getAttributes("simpara", 1)) {
                $role = $parser->XML_PullParser_getAttrVal('role', $attr);
                if ($role == "hr") {
                    echo "<hr width='800' align='left'>\n";
                }
                continue;
                // this will also skip over the xml contents page which has role ="contents"
            }
            $text = $parser->XML_PullParser_getText();
            $mark_up = $parser->XML_PullParser_getCSSSpans(array("code" => "code", "emphasis" => "emphasis", "classname" => "classname"));
            //   $mark_up += $parser->XML_PullParser_getHTMLTags(array("classname"=>"b"));
            $text = $parser->XML_PullParser_getTextMarkedUp($mark_up);
            echo "<table width='750' bgcolor='#eeeeee'><td class='block'><pre>{$text}</pre></table>";
        }
    }
    $parser->XML_PullParser_free();
}