在PHP中搜索XML数据

时间:2013-11-02 18:18:37

标签: php html xml

大家好我知道使用LIKE '"%% $ _POST [" txtbuscar "]%%"'使用MySQL进行搜索,我现在用XML执行。我需要提出一个输入txtbuscar。

下面的脚本我已经设法列出并页面,并搜索最重要的单词,但我相信这是一些东西,但是没有用,你能帮我吗?

XML

<listaccts>
    <acct>
        <disklimit>100M</disklimit>
        <diskused>100M</diskused>
        <domain>dominio.com.br</domain>
        <ip>123.124.125.126</ip>
        <user>Fulano</user>
        <plan>Plano1</plan>
    </acct>
    <acct>
        <disklimit>200M</disklimit>
        <diskused>200</diskused>
        <domain>dominio.com.br</domain>
        <ip>123.124.125.126</ip>
        <user>Fulano2</user>
        <plan>Plano2</plan>
    </acct>
</listaccts>

PHP

$accounts = $xmlapi->listaccts();
$xml = simplexml_load_string($accounts);

if ($_POST["txtBuscar"] == "ligar") {

    $busc = $_POST["buscarURL"];

    foreach ($xml->xpath('//acct') as $acct) {

        $minhaArray = $xml->xpath('//user[.>$busc]');

        $minhaArray = array();
        foreach ($xml->acct as $acct) {
            $minhaArray[] = array(
                    'domain' => (string)$acct->domain,
                    'user' => (string)$acct->user,
                    'ip' => (string)$acct->ip,
                    'disklimit' => (string)$acct->disklimit,
                    'diskused' => (string)$acct->diskused,
                    'plan' => (string)$acct->plan);
        }
    }

HTML

<form id="formBuscaClienteUrl" name="formBuscaClienteUrl" method="post" action="" class="sky-form" onsubmit="return false">
    <input name="txtBuscar" type="hidden" value="ligar" />
    <script language="javascript">document.formBuscaClienteUrl.buscarURL.focus();</script>
    <table width="100%" cellspacing="0" cellpadding="5">
        <tr>
            <td width="95%" style="padding-left:10px;">
                <input name="buscarURL" type="text"/>
            </td>
            <td width="5%"><button style="padding-left:10px;" type="submit" onclick="buscar('formBuscaClienteUrl')" class="button" >Localizar</button></td>
        </tr>
    </table>
</form>

// ========================那没关系==============

我做了一些调整,现在已经开始,万一有人想要。感谢

$ BUSC = $ _ POST [ “buscarURL”];

$expression = sprintf("/*/acct[contains(user, '$busc')]", $_POST["buscarURL"]);
$result = $xml->xpath($expression);

$minhaArray = array();
foreach($result as $account){
    $minhaArray[] = array(
        'domain'=>(string)$account->domain,
        'user'=>(string)$account->user,
        'ip'=>(string)$account->ip,
        'disklimit'=>(string)$account->disklimit,
        'diskused'=>(string)$account->diskused,
        'plan'=>(string)$account->plan,

    );

}

1 个答案:

答案 0 :(得分:0)

如果您需要从XML文档进行查询,最常见的是通过Xpath进行查询(正如您已经想到的那样),您在SQL中认识的LIKE可能与{{1}相当。在Xpath:

contains()

使用示例集输出:

$listaccts = new SimpleXMLElement($accounts);
$txtBuscar = 'lano';

$expression = sprintf('/*/acct[contains(user, %s)]', xpath_string($txtBuscar));
$result = $listaccts->xpath($expression);

foreach($result as $account) {
    printf("User..: %s\nDomain: %s\n", $account->user, $account->domain);
}

注意:就像在需要处理SQL注入的SQL中一样,我通过将搜索值正确编码为字符串来关注xpath注入(这可能会错过现实生活中的UTF-8清理程序)您从User..: Fulano Domain: dominio.com.br User..: Fulano2 Domain: example.com.br 获取并且您的系统已经配置为通常无法清理输入。