开发者

Retrieving single node value from a nodelist

I'm having difficulty extracting a single node value from a nodelist.

My code takes an xml file which holds several fields, some containing text, file paths and full image names with extensions.

I run an expath query over it, looking for the node item with a certain id. It then stores the matched node item and saves it as $oldnode

Now my problem is trying to extract a value from that $oldnode. I have tried to var_dump($oldnode) and print_r($oldnode) but it returns the following: "object(DOMElement)#8 (0) { } "

Im guessing the $oldnode variable is an object, but how do I access it?

I am able to echo out the whole node list by using: echo $oldnode->nodeValue;

This displays all the nodes in the list.

Here is the code which handles the xml file. line 6 is the line in question...

 $xpathexp = "//item[@id=". $updateID ."]";
            $xpath = new DOMXpath($xml);
            $nodelist = $xpath->query($xpathexp);
            if((is_null($nodelist)) || (! is_numeric($nodelist))) {
              $oldnode = $nodelist->item(0);
              echo $oldnode->nodeValue;
              //$imgUpload = strchr($oldnode->nodeValue, ' ');
              //$imgUpload = strrchr($imgUpload, '/');
              //explode('/',$imgUpload);

              //$imgUpload = trim($imgUpload); 


              $newItem = new DomDocument;
              $i开发者_开发问答tem_node = $newItem ->createElement('item');

               //Create attribute on the node as well
              $item_node ->setAttribute("id", $updateID);

              $largeImageText = $newItem->createElement('largeImgText');
              $largeImageText->appendChild( $newItem->createCDATASection($largeImgText));
              $item_node->appendChild($largeImageText);

              $urlANode = $newItem->createElement('urlA');
              $urlANode->appendChild( $newItem->createCDATASection($urlA));
              $item_node->appendChild($urlANode);

              $largeImg = $newItem->createElement('largeImg');
              $largeImg->appendChild( $newItem->createCDATASection($imgUpload));
              $item_node->appendChild($largeImg);

              $thumbnailTextNode = $newItem->createElement('thumbnailText');
              $thumbnailTextNode->appendChild( $newItem->createCDATASection($thumbnailText));
              $item_node->appendChild($thumbnailTextNode);

              $urlB = $newItem->createElement('urlB');
              $urlB->appendChild( $newItem->createCDATASection($urlA));
              $item_node->appendChild($urlB);

              $thumbnailImg = $newItem->createElement('thumbnailImg');
              $thumbnailImg->appendChild( $newItem->createCDATASection(basename($_FILES['thumbnailImg']['name'])));
              $item_node->appendChild($thumbnailImg);

              $newItem->appendChild($item_node);

              $newnode = $xml->importNode($newItem->documentElement, true);

                       // Replace
              $oldnode->parentNode->replaceChild($newnode, $oldnode);

              // Display
              $xml->save($xmlFileData);
                               //header('Location: index.php?a=112&id=5');

Any help would be great.

Thanks


Wasn't it supposed to be echo $oldnode->firstChild->nodeValue;? I remember this because technically you need the value from the text node.. but I might be mistaken, it's been a while. You could give it a try?

After our discussion in the comments on this answer, I came up with this solution. I'm not sure if it can be done cleaner, perhaps. But it should work.

$nodelist = $xpath->query($xpathexp);
if((is_null($nodelist)) || (! is_numeric($nodelist))) {
    $oldnode = $nodelist->item(0);

    $largeImg       = null;
    $thumbnailImg   = null;
    foreach( $oldnode->childNodes as $node ) {
        if( $node->nodeName == "largeImg" ) {
            $largeImg       = $node->nodeValue;
        } else if( $node->nodeName == "thumbnailImg" ) {
            $thumbnailImg   = $node->nodeValue;
        }
    }

    var_dump($largeImg);
    var_dump($thumbnailImg);
}

You could also use getElementsByTagName on the $oldnode, then see if it found anything (and if a node was found, $oldnode->getElementsByTagName("thumbnailImg")->item(0)->nodeValue). Which might be cleaner then looping through them.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜