将[$ _POST] var插入DOM搜索字符串?

时间:2010-11-21 22:45:16

标签: php simplexml nodes

我不确定我是否正确命名了这个标题,但基本上我有一个来自$ _POST(一个乐队的名字)的变量,我想在这样的一行中插入一个节点的名称 -

$lastvisits = $address->$band->getElementsByTagName("lastvisit");

其中$band

$band = $_POST['band']

因为$ address中的节点可能是任何名称,并且可能不存在。 我确信必须有一个简单的方法,但我不确定格式化。

如果它存在或不存在,$ lastvisits = $ address-> $ band> getElementsByTagName(“lastvisit”)带来错误 - 致命错误:调用成员函数getElementsByTagName( )在......中的非对象上。

XML -

<?xml version="1.0"?>
<addresses>
<address>
    <ip>127.0.0.1</ip>
    <Beatles>
        <lastvisit>12/08/2006</lastvisit>
    </Beatles>
</address>
    <address>
    <ip>125.0.0.1</ip>
</address>
 </addresses>

这是完整的代码:

    $doc = new DOMDocument();
$doc->load("votingxml/addresses.xml");
$addresses = $doc->getElementsByTagName("address");
$band = strval($_POST['band']);
$pVoted = false;
$pFound = false;

//Loop through the addresses nodes and see if the person has voted before for each( $addresses as $address )
{

$ips = $address->getElementsByTagName("ip");
            $ip = $ips->item(0)->nodeValue;
                            if ($ip == $domain){

                $pFound = true;             
            if ($address->$band == 'NULL'){
                 $bandfound= false;
                 $newBandElement = $doc->createElement($_POST['band']);
                 $newLastVisitElement = $doc->createElement('lastvisit');
                 $dayvalue = $doc->createTextNode($today);
                 $dayvalue = $newLastVisitElement->appendChild($dayvalue);

                 $newBandElement->appendChild($newLastVisitElement);
                 $address->appendChild($newBandElement);
                 $doc->save("votingxml/addresses.xml");
                 $pVoted = false;
            }
            else{

                 $bandfound =true;
                                 $lastvisits = $address->$band->getElementsByTagName("lastvisit");
                 $lastvisit = $lastvisits->item(0)->nodeValue;

                 if ($lastvisit == $today){
                        echo "alreadyvoted";
                        $pVoted = true;
                     }else{
                    $lastvisits->item(0)->nodeValue = $today;
                    $doc->save("votingxml/addresses.xml");
                    $pVoted = false;
                     }

            }
        }
        else if ($ip != $domain)
        {
            $pFound = false;
        }

}

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

编辑#1 :您在添加的示例中看起来像是指DOM,而不是SimpleXML。不过,这里是使用XML结构使用SimpleXML对象的方法......


$band = $_POST['band'];

if (property_exists($address, $band)) {
    $lastvisits = $address->$band->lastvisit;
}
else {
    /* handle the fact that the name of the element which the user passed is invalid */
}

注意:只有1个lastvisit元素。如果还有更多,则需要将这些lastvisit元素包装到XML文件中的单个“lastvisits”父元素中。有效的XML以及所有这些。

编辑#2 :您的xml结构存在一些问题。 “甲壳虫乐队”太独特了,不能成为元素的名称。你可能想要改变它:

<addresses>
   <address>
      <ip>127.0.0.1</ip>
      <visits>
         <visit>
            <band>Beatles</band>
            <lastvisit>12/08/2006</lastvisit>
         </visit>
         <visit>
            <band>New Kids On the Block</band>
            <lastvisit>1/14/2008</lastvisit>
         </visit>
      </visits>
   </address>
   <address>
      <ip>24.135.9.2</ip>
      <visits>
...
      </visits>
   </address>
</addresses>

然后,您将要搜索具有您要搜索的IP值的ip节点,然后在band节点下搜索值“Beatles”。

编辑#3,4:使用DOMXPath类为DOM添加方法,对上述XML结构进行搜索,如果找不到波段,则添加新记录...


$band = $_POST['band'];
$client_IP = $_SERVER["REMOTE_ADDR"];
$doc = new DOMDocument();
$doc->load("votingxml/addresses.xml");
$xpath = new DOMXPath($doc);
$ip_query = $xpath->query('//addresses/address/ip[. = "' . $client_IP . '"]');
if ($ip_query->length === 1) {
    // client's IP found, now grab and work with the client's address record
    $address = $ip_query->item(0)->parentNode;
    // search for the visit record within $address for band element value = $band
    $band_query = $xpath->query('visits/visit/band[. = "'.$band.'"]', $address);
    if ($band_query->length === 1) {
        // $band found, now get visit parent record for more details
        $visit = $band_query->item(0)->parentNode;
        // with that, now do something with the record's lastvisit value
        echo $visit->getElementsByTagName("lastvisit")->item(0)->nodeValue;
        }
    else {
        // $band not found.  Add new visit record to the XML file
        // point to 'visits' group element
        $visits = $address->getElementsByTagName("visits")->item(0);
        // create a new 'visit' element, complete with 'band' and 'lastvisit' child elements
        $visit = $doc->createElement("visit");
        $band = $doc->createElement("band", $band);
        $lastvisit = $doc->createElement("lastvisit", date("n/d/Y"));
        $visit->appendChild($band);
        $visit->appendChild($lastvisit);
        // now add new 'visit' element to 'visits' element and save
        $visits->appendChild($visit);
        $doc->save("votingxml/addresses.xml");
        }
    }