2012-02-08 20 views
-1

我有一個文檔,它的結構如下所示。
有很多<entry>。我的問題是我怎樣才能輸出每個條目的<uri>?還有一個問題,我怎樣才能輸出USERNAME如何在XML文檔中提取<uri></uri>的內容?

這是我想要得到的用戶名http://search.twitter.com/search.atom?q=yankees

<?xml version="1.0" encoding="UTF-8"?> 
<feed xmlns:google="http://base.google.com/ns/1.0" xml:lang="en-US" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns="http://www.w3.org/2005/Atom" xmlns:twitter="http://api.twitter.com/" xmlns:georss="http://www.georss.org/georss"> 

<entry> 
    <author> 
    <name></name> 
     <uri>http://twitter.com/USERNAME</uri> 
    </author> 
</entry> 
文件

+0

[用簡單的XML閱讀Twitter的RSS搜索飼料(可能重複http://stackoverflow.com/questions/1787064/reading-twitters-rss-search- feed-with-simple-xml) – salathe 2012-02-08 18:43:28

+0

請參閱http://php.net/simplexml.examples-basic並將示例修改爲您想要使用的twitter搜索xml。 – salathe 2012-02-08 18:44:15

回答

0
<?php 
$xml = new DOMDocument; 

// link to ur file 
$xml->load('');  

foreach ($xml->getElementsByTagName('entry') as $product) 
{ 
    $append = array(); 

    foreach($product->getElementsByTagName('uri') as $name) { 
     // Stick $name onto the array 
     $append[] = $name; 
    } 
} 

$result = $xml->saveXML(); 
print_r(str_replace('http://twitter.com/','',$result)); 
?> 
+0

我試過用http://search.twitter.com/search.atom?q=ankees但沒有運氣 – EnexoOnoma 2012-02-08 18:27:23

+0

好了在我編寫代碼之後,您編輯了您的問題並更改了問題的上下文。所以,也許你應該提出一個新的問題,而不是下一次改變環境。 – 2012-02-08 18:28:10

+0

我添加的唯一的東西是實際的鏈接... – EnexoOnoma 2012-02-08 18:29:02

0

您應該使用SimpleXML的某種一個循環所有的低谷秒。 (foreach($xml->entry as $entry)循環應該工作正常,我認爲。)

而對於第二個:如果它總是http://twitter.com/USERNAME,只需計算前綴的長度比使用substr。

資源使用:substrSimpleXMLSimpleXML

+0

你能告訴我一個例子嗎? – EnexoOnoma 2012-02-08 19:30:14

相關問題