3

我正在寻找一种方法来获取kuler:swatchHexColor下面 XML 文件中的值。我尝试了几种方法,但没有运气。我在下面尝试了这个,但做得不好。

同样,尝试将hexColor值放入变量中。

$feed = simplexml_load_file('url');
$kuler = $feed->children('http://kuler.adobe.com/kuler/API/rss/');
foreach ($kuler->themeItem as $themeItem)   {
  echo $themeItem->swatch;
}

<rss xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:rss="http://blogs.law.harvard.edu/tech/rss" xmlns:kuler="http://kuler.adobe.com/kuler/API/rss/" version="2.0">
    <channel>
        <title>kuler search results for 'crazy'</title>
        <link>http://kuler.adobe.com/</link>
        <description>
        612 kuler themes were found. Displaying results 1 to 1.
        </description>
        <language>en-us</language>
        <pubDate />
        <lastBuildDate />
        <docs>http://blogs.law.harvard.edu/tech/rss</docs>
        <generator>Kuler Services</generator>
        <managingEditor>kulerfeedback@adobe.com</managingEditor>
        <webMaster>kulerfeedback@adobe.com</webMaster>
        <recordCount>612</recordCount>
        <startIndex>0</startIndex>
        <itemsPerPage>1</itemsPerPage>
        <item>
            <title>Theme Title: crazy lisa</title>
            <link>http://kuler.adobe.com/index.cfm#themeID/89760</link>
            <guid>http://kuler.adobe.com/index.cfm#themeID/89760</guid>
            <enclosure xmlns="http://www.solitude.dk/syndication/enclosures/">
                <title>crazy lisa</title>
                <link type="image/png" length="1">
                    <url>
        http://kuler-api.adobe.com/kuler/themeImages/theme_89760.png
        </url>
                </link>
            </enclosure>
            <description>
                <img src="http://kuler-api.adobe.com/kuler/themeImages/theme_89760.png" />
                <br /> Artist: sa<br /> ThemeID: 89760<br /> Posted: 09/24/2007<br /> Tags: crazy but sweet, crazy harmony <br />    Hex: FF0082, BF9F1B, B8495D, 520641, CC0000
        </description>
            <kuler:themeItem>
                <kuler:themeID>89760</kuler:themeID>
                <kuler:themeTitle>crazy lisa</kuler:themeTitle>
                <kuler:themeImage>...</kuler:themeImage>
                <kuler:themeAuthor>...</kuler:themeAuthor>
                <kuler:themeTags>crazy but sweet, crazy harmony</kuler:themeTags>
                <kuler:themeRating>3</kuler:themeRating>
                <kuler:themeDownloadCount>28</kuler:themeDownloadCount>
                <kuler:themeCreatedAt>20070924</kuler:themeCreatedAt>
                <kuler:themeEditedAt>20070926</kuler:themeEditedAt>
                <kuler:themeSwatches>
                    <kuler:swatch>
                        <kuler:swatchHexColor>FF0082</kuler:swatchHexColor>
                        <kuler:swatchColorMode>cmyk</kuler:swatchColorMode>
                        <kuler:swatchChannel1>0.0</kuler:swatchChannel1>
                        <kuler:swatchChannel2>1.0</kuler:swatchChannel2>
                        <kuler:swatchChannel3>0.49</kuler:swatchChannel3>
                        <kuler:swatchChannel4>0.0</kuler:swatchChannel4>
                        <kuler:swatchIndex>0</kuler:swatchIndex>
                    </kuler:swatch>
                    <kuler:swatch>
                        <kuler:swatchHexColor>BF9F1B</kuler:swatchHexColor>
                        <kuler:swatchColorMode>cmyk</kuler:swatchColorMode>
                        <kuler:swatchChannel1>0.0</kuler:swatchChannel1>
                        <kuler:swatchChannel2>0.17</kuler:swatchChannel2>
                        <kuler:swatchChannel3>0.86</kuler:swatchChannel3>
                        <kuler:swatchChannel4>0.25</kuler:swatchChannel4>
                        <kuler:swatchIndex>1</kuler:swatchIndex>
                    </kuler:swatch>
                    <kuler:swatch>
                        <kuler:swatchHexColor>B8495D</kuler:swatchHexColor>
                        <kuler:swatchColorMode>cmyk</kuler:swatchColorMode>
                        <kuler:swatchChannel1>0.01</kuler:swatchChannel1>
                        <kuler:swatchChannel2>0.61</kuler:swatchChannel2>
                        <kuler:swatchChannel3>0.5</kuler:swatchChannel3>
                        <kuler:swatchChannel4>0.27</kuler:swatchChannel4>
                        <kuler:swatchIndex>2</kuler:swatchIndex>
                    </kuler:swatch>
                    <kuler:swatch>
                        <kuler:swatchHexColor>520641</kuler:swatchHexColor>
                        <kuler:swatchColorMode>cmyk</kuler:swatchColorMode>
                        <kuler:swatchChannel1>0.0</kuler:swatchChannel1>
                        <kuler:swatchChannel2>0.926829</kuler:swatchChannel2>
                        <kuler:swatchChannel3>0.207317</kuler:swatchChannel3>
                        <kuler:swatchChannel4>0.678431</kuler:swatchChannel4>
                        <kuler:swatchIndex>3</kuler:swatchIndex>
                    </kuler:swatch>
                    <kuler:swatch>
                        <kuler:swatchHexColor>CC0000</kuler:swatchHexColor>
                        <kuler:swatchColorMode>cmyk</kuler:swatchColorMode>
                        <kuler:swatchChannel1>0.0</kuler:swatchChannel1>
                        <kuler:swatchChannel2>1.0</kuler:swatchChannel2>
                        <kuler:swatchChannel3>1.0</kuler:swatchChannel3>
                        <kuler:swatchChannel4>0.2</kuler:swatchChannel4>
                        <kuler:swatchIndex>4</kuler:swatchIndex>
                    </kuler:swatch>
                </kuler:themeSwatches>
            </kuler:themeItem>
            <pubDate>Mon, 24 Sep 2007 15:24:23 PST</pubDate>
        </item>
    </channel>
</rss>
4

1 回答 1

1

在 PHP 中有两种常用的方法。首先,使用简单的 xml 扩展(正如您已经在做的那样):

$feed = simple_xml_load_file('url');
// resister the kuler namespace
$feed->registerXpathNamespace('kuler', 'http://kuler.adobe.com/kuler/API/rss/');
// get elements regardless of their position in tree -> //
$result = $feed->xpath('//kuler:swatchHexColor');

// iterate through the result set 
foreach ($result as $item)   {
  echo $item, PHP_EOL;
}

您还可以使用 DOM 扩展:

$feed = new DOMDocument();
$feed->load('url');

// create an xpath selector for the document
$selector = new DOMXPath($feed);
// register the kuler namespace
$selector->registerNamespace('kuler', 'http://kuler.adobe.com/kuler/API/rss/');

// get elements regardless of postion in tree -> //
$result = $selector->query('//kuler:swatchHexColor');

// iterate trough the result set
foreach ($result as $node) {
    // do what you want with the node ...
    echo $node->nodeValue, PHP_EOL;
}
于 2013-02-02T11:01:18.283 回答