2013-02-02 60 views
3

我正在尋找一種方法來獲取以下XML文件中的kuler:swatchHexColor值。我嘗試了幾種方法,但沒有運氣。我在下面嘗試了這一點,但做得不好。如何在XML中包含名稱空間時將PHP用於PHP?

再次嘗試將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>[email protected]</managingEditor> 
     <webMaster>[email protected]</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> 
+0

這應該被標記爲[標籤:PHP]而不是[標籤:jquery] – Alexander

+0

你說過你在使用jQuery,但是你正在顯示PHP代碼。 –

+0

對不起,我的意思是PHP。很抱歉。全新的這東西。 – kangoroo

回答

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; 
} 
+0

不能簡單。謝謝。 – kangoroo

+0

你需要接受答案,如果你工作,似乎是.. –

相關問題