开发者

Extracting Attributes from a SimpleXML Object

How do I extract attributes from this xml object

<designs>
    <tags>
    .
    .
    .
    </tags>
    <templates>
        <template id="photographysite" image="http://example.com/en/previews/photographysitePreview434x326.jpg" name="Shutter" thumb="http://example.com/en/previews/photographysitePreview182x137.jpg">
            <tag>all</tag>
            <tag>featured</tag>
            <tag>personal</tag>
            <tag>portfolio</tag>
            <tag>photography</tag>
            <tag>business</tag>
        </template>
    </templates>
</designs>

If I consider each object as $template, then this syntax wont work.

foreach ($xmldoc->templates as $template) {
    $attributes = $template->attributes开发者_如何学Python();
        echo '<img src="' . $attributes['thumb'] . '" />';
}


foreach($template->foo[0]->attributes() as $a => $b):

http://php.net/manual/en/simplexmlelement.attributes.php


You can access individual attributes using the array notation, e.g.

foreach ($xmldoc->templates->template as $template) {
    echo '<img src="', $template['thumb'], '"/>';
}

See http://www.php.net/manual/en/simplexml.examples-basic.php#example-4587

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜