【问题标题】:Filter XML by elements [duplicate]按元素过滤 XML [重复]
【发布时间】:2013-07-06 02:20:35
【问题描述】:
<?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>
               ';
        }
    }
?>

我有几个&lt;a&gt; 标签(相同的链接,home.php),每个都有不同的 $_GET 链接(home.php?doctype=doc,home.php?doctype=all,等等..)。 现在我希望使用 if 语句过滤每个 doctype 并检查 $_GET['doctype'] 是否等于我的值(假设值是 word、excel、powerpoint 等)。

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

【问题讨论】:

    标签: php html-parsing simplexml


    【解决方案1】:

    我会为此使用位掩码: 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>
            ';
        }
    }
    

    【讨论】:

    • 我不是这个意思..
    【解决方案2】:

    您的问题在于 IF:

    if ( isset( $_GET['doctype'] ) == "all" )

    此语句将评估 isset()'s 布尔结果 truefalsestring "all",您可能不是这个意思

    要解决此问题并消除冗余,请使用以下代码:

    $acceptedDocTypes = ['all', 'doc', 'excel'];
    
    if ((isset($_GET['doctype'])) && (in_array($_GET['doctype'], $acceptedDocTypes))) {
        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>
                    ';
        }
    }
    

    只要'doctype' 被设置并且是允许的类型,这段代码就会被它过滤

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多