开发者

How to get the title? From this XML using Zend framework?

开发者 https://www.devze.com 2023-04-01 11:38 出处:网络
I am very new to Zend Framework. And i have tried to get the XML value but cant make it work. XML: <?xml version=\"1.0\" encoding=\"UTF-8\"?>

I am very new to Zend Framework. And i have tried to get the XML value but cant make it work.

XML:

<?xml version="1.0" encoding="UTF-8"?>
<result count="2">
    <blocks>
        <listing>
            <title>Title 1</title>
            <id>1</id>
        </listing>
        <listing>
            <title>Title 2</title>
            <id>2</id>
        </listing>

    </blocks>
</result>

PHP (to find all the title):

    $d开发者_如何学运维om = new Zend_Dom_Query();
    $dom->setDocumentXml($result);

    $results = $dom->queryXpath('/result/blocks/listing/title'); 
    //$dom->queryXpath('/*/*/listing'); no luck
    //$dom->queryXpath('///listing'); no luck 

    foreach($results as $k)
    {
        Zend_Debug::dump($k->getAttribute('title')); // empty
        echo $k->getDocument(); // shows none
    }

Any help?


Using queryXpath('/result/blocks/listing/title') your $k already is the DOMElement that represents the <tile>...</title> elements.
You can retrieve the value via $k->nodeValue. For a DOMElement that's the concatenation of all text nodes in the descendant axis.

foreach($results as $k)
{
    Zend_Debug::dump($k->nodeValue); // empty
}


title is a node - not an attribute - attributes are placed inside the tag :)

0

精彩评论

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