按元素过滤XML [英] Filter XML by elements

查看:79
本文介绍了按元素过滤XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?php
$files = glob( 'docs/*.xml' );
if ( isset( $_GET['doctype'] ) == "all" ) {
    foreach ( $files as $file ) {
        $xml = new SimpleXMLElement( $file, 0, true );
        echo'
                <tr>
                <td id="'. $xml->doctype .'" name="'. $xml->doctype .'" class="mainTable">' . $xml->doctype . '</td>
                <td><a href="viewdoc.php?docname=' . basename( $file, '.xml' ) . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename( $file, '.xml' ) . '</a></td>
                <td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
                <td>'. $xml->date .'</td>
                <td>* * * * *</td>
                <td>'. filesize( $file ) .'kb</td>
                </tr>
                ';
    }
}
else if ( isset( $_GET['doctype'] ) == "doc" ) {
        foreach ( $files as $file ) {
            $xml = new SimpleXMLElement( $file, 0, true );
            // code filter the $xml->doctype by equal it to currect value - which i'm not sure how to do.
            echo'
                     <tr>
                     <td id="'. $xml->doctype .'" name="'. $xml->doctype .'" class="mainTable">' . $xml->doctype . '</td>
                     <td><a href="viewdoc.php?docname=' . basename( $file, '.xml' ) . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename( $file, '.xml' ) . '</a></td>
                     <td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
                     <td>'. $xml->date .'</td>
                     <td>* * * * *</td>
                     <td>'. filesize( $file ) .'kb</td>
                    </tr>
               ';
        }
    }
?>

我只有几个<a>标记(相同的链接,home.php),每个标记都有不同的$ _GET链接( home.php?doctype = doc,home.php?doctype = all等.). 现在,我希望使用if语句过滤每个文档类型,并检查$_GET['doctype']是否等于我的值(假设值是word,excel,powerpoint等.).

I got few <a> tags (same link, home.php), each one got different $_GET link (home.php?doctype=doc, home.php?doctype=all, etc..). Now i wish to filter each doctype by using if statement and check $_GET['doctype'] if he equals to my value (assuming the value is word, excel, powerpoint, etc..).

我的问题是:假设$ xml-> doctype等于我的值之一,如何过滤doctype?

My question is: how can I filter doctype assuming that I equal $xml->doctype to one of my values?

推荐答案

为此,我将使用位掩码: http://php.net/manual/en/language.operators.bitwise.php

i would use a bitmask for this: http://php.net/manual/en/language.operators.bitwise.php

<?php

// define your types and groups:
$listedTypes["TYPE_MSWORD"]             = 1;
$listedTypes["TYPE_MSEXCEL"]            = 2;
$listedTypes["TYPE_MSPOWERPOINT"]       = 4;
$listedTypes["TYPE_OFFICE"]             = $listedTypes["TYPE_MSWORD"] + $listedTypes["TYPE_MSEXCEL"] + $listedTypes["TYPE_MSPOWERPOINT"];

$listedTypes["TYPE_HTML"]               = 8;
$listedTypes["TYPE_SVG"]                = 16;
$listedTypes["TYPE_W3C"]                = $listedTypes["TYPE_HTML"] + $listedTypes["TYPE_SVG"];

$listedTypes["TYPE_ALL"]                = $listedTypes["TYPE_OFFICE"] + $listedTypes["TYPE_W3C"];


// try to open the page.php?doctype=TYPE_MSWORD
// or page.php?doctype=TYPE_ALL
if(!isset($_GET['doctype']))                        $_GET['doctype'] = "TYPE_ALL";
if(!isset($listedTypes[$_GET['doctype']]))          $_GET['doctype'] = "TYPE_ALL";
$requestedType = $listedTypes[$_GET['doctype']];



foreach ( $files as $file )
{
    $xml = new SimpleXMLElement( $file, 0, true );

    if($xml->doctype == "......")               $fileType = $listedTypes["TYPE_MSWORD"];
    elseif($xml->doctype == ".........")        $fileType = $listedTypes["TYPE_MSEXCEL"];
    elseif($xml->doctype == ".........")        $fileType = $listedTypes["TYPE_MSPOWERPOINT"];
    //... continue

    // then check if the $type matches the $selection of $_GET['doctype']
    // if the filetype is in the requested file type group, it will be shown
    if($fileType & $requestedType)
    {
        echo'
        <tr>
        <td id="'. $xml->doctype .'" name="'. $xml->doctype .'" class="mainTable">' . $xml->doctype . '</td>
        <td><a href="viewdoc.php?docname=' . basename( $file, '.xml' ) . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename( $file, '.xml' ) . '</a></td>
        <td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
        <td>'. $xml->date .'</td>
        <td>* * * * *</td>
        <td>'. filesize( $file ) .'kb</td>
        </tr>
        ';
    }
}

这篇关于按元素过滤XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆