开发者

XPath _ PHP -- Parsing XHTML -- Trouble?

I have made significant progress in the past hours and have finally hit a wall.

Here is my code:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$info = curl_exec($ch);

$html = new DOMDocument();
$html->loadHTML($info);


$xpath = new DOMXPath($html);
$texts = $xpath->query("//div[@class='summary-gems']/ul/li");
$imgs = $xpath->query("//div[@class='summary-gems']/ul/li");

for ($i = 0; $i < $texts->length; $i++) {
$gems[$i]['text'] = $texts->item($i)->nodeValue;
$gems[$i]['img'] = $imgs->getAttribute('href');

echo $gems[$i]['img'];
die;
}

Here is what 开发者_C百科the XHTML looks like at this point:

<div class="summary-gems">
    <ul>
        <li>
            <span class="value">5</span>
            <span class="times">x</span>
            <span class="icon">
                <span class="icon-socket socket-2">
                    <a href="/wow/en/item/52207" class="gem">
                        <img src="http://us.battle.net/wow-assets/static/images/icons/18/inv_misc_cutgemsuperior6.jpg" alt="" />
                        <span class="frame"></span>
                    </a></span></span>
            <a href="/wow/en/item/52207" class="name color-q3">Brilliant Inferno Ruby</a>
            <span class="clear">
                <!-- -->
            </span>
        </li>
        <li>
            <span class="value">3</span>
            <span class="times">x</span>
            <span class="icon">
                <span class="icon-socket socket-10">
                    <a href="/wow/en/item/52236" class="gem">
                        <img src="http://us.battle.net/wow-assets/static/images/icons/18/inv_misc_cutgemsuperior3.jpg" alt="" />
                        <span class="frame"></span>
                    </a></span></span>
            <a href="/wow/en/item/52236" class="name color-q3">Purified Demonseye</a>
            <span class="clear">
                <!-- -->
            </span>
        </li>
        <li>
            <span class="value">3</span>
            <span class="times">x</span>
            <span class="icon">
                <span class="icon-socket socket-6">
                    <a href="/wow/en/item/68356" class="gem">
                        <img src="http://us.battle.net/wow-assets/static/images/icons/18/inv_misc_cutgemsuperior4.jpg" alt="" />
                        <span class="frame"></span>
                    </a></span></span>
            <a href="/wow/en/item/68356" class="name color-q3">Willful Ember Topaz</a>
            <span class="clear">
                <!-- -->
            </span>
        </li>
        <li>
            <span class="value">1</span>
            <span class="times">x</span>
            <span class="icon">
                <span class="icon-socket socket-1">
                    <a href="/wow/en/item/52298" class="gem">
                        <img src="http://us.battle.net/wow-assets/static/images/icons/18/inv_misc_metagem_b.jpg" alt="" />
                        <span class="frame"></span>
                    </a></span></span>
            <a href="/wow/en/item/52298" class="name color-q3">Destructive Shadowspirit Diamond</a>
            <span class="clear">
                <!-- -->
            </span>
        </li>
    </ul>
</div>

When I get the 'text' part of it, I get the plain text in that particular node (4 of them in this event). What I want is ALL of the XHTML that is right there, if possible. If not, then what I want to do is get the "image source" and "hyperlink class 'gem'" for each node. I am bit baffled as to how to get anything but the plain text of the node in this event.

Any help would be appreciated! Please let me know if you have any questions.


The XPath for the link is

//div[@class='summary-gems']/ul//a[@class='gem']

and you can access the attribute with

(string)$simplexmlelement['href']

Do the same with the <img src="..">.

To get the full XML of an element, use $simplexmlelement->asXML().

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜