开发者

GettingAttribute Content with Xpath and DOM (php)

   $dom->load('2.xml');  
    $xpath = new DOMXPath($dom); 

    $questions = $xpath->query("questions/question"); 
    foreach($questions as $quest开发者_如何学Pythonion)  
        {
         if ($question->textContent == "")
            { 
            $question->nodeValue = "{$current_time}";
             break;
            }
        }

<questions>
<question id="1">a<question>
<question id="2"><question>
</questions>

Above code is searching for a node called question whose nodeValue is empty. It works perfectly. However, I cannot figure out how to get the current node's attribute's value. for instance 2 is the value I want to get.

I want to do this just right after this code:

$question->nodeValue = "{$current_time}";

Here is my xml file:

<?xml version="1.0" encoding="utf-8"?>
 <questions>
  <question id="1"></question>
  <question id="2"></question>
  <question id="3"></question>
 </questions>

Can anybody help me with this issue? Thanks!


Have you tried DOMElement::getAttribute ?

$id = $question->getAttribute('id');


I think you should give a sample extract of your XML file, so we could more easily find out what is your data and what you want to do with it.

BTW, have you tried using SimpleXML instead of DOM - I think it's a lot more easier to use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜