2012-02-28 17 views
1

我需要幫助,我希望你能幫助我;) 我鼓勵一些大師尋求和平與知識嘿嘿。 好的讓我來解釋一下,我有一個XML,它有一個區段,其中的URL如下所示分段。我可以使用SimpleXML和XPath成功解析它。我的XPath查詢返回給我一個包含結果的數組,然後我可以回顯它們。沒有問題! =)解析XML並將分割的URL與SimpleXML和XPath連接起來

但是我想進一步嘗試減少代碼行並改進我的代碼。先來看看我的代碼,你可以複製/粘貼它,並給它一個嘗試:

<?php 

$xml_g=' 
<files> 
    <file> 
     <filename>itzafile</filename> 
     <ext>.tar.gz</ext> 
     <url protocol="http://">itzanexample.net/folder/subfolder/</url> 
    </file> 
    <file> 
     <filename>itzavideo</filename> 
     <ext>.mp4</ext> 
     <url protocol="ftp://">itzanotherurl.com/videos/</url> 
    </file> 
</files> 
'; 


function URLparts($xml) { 
$xmlData= simplexml_load_string("$xml"); 

$protocol = $xmlData->xpath('//file/url/@protocol'); 
$url = $xmlData->xpath('//file/url'); 
$filename = $xmlData->xpath('//file/filename'); 
$ext = $xmlData->xpath('//file/ext'); 

echo 'The following it\'s echoed calling 4 different arrays:'."\n\t".$protocol[0], $url[0], $filename[0], $ext[0]."\n\t".$protocol[1], $url[1], $filename[1], $ext[1]."\n\n"; //prints the entire url! Right! 


//These variables are arrays, so theorically it must be possible to create an array of arrays here: 
$completeURL = array($protocol,$url,$filename,$ext); 
//I've also tried this but it's just the same problem: 
//$completeURL = array(array($protocol),array($url),array($filename),array($ext)); 

echo 'The following should be echoed with a two-dimensional array, but something fails:'."\n\t".$completeURL[0][0][0][0]."\n\n"; //Just prints "http://" WRONG! u.u 
/* 
* $completeURL[1][0][0][0] prints the domain+subfolder 
* $completeURL[0][1][0][0] prints ftp:// 
* $completeURL[0][0][1][0] prints nothing... 
*/ 

} 

URLparts($xml_g); 
?> 

正如你所看到的,我想避免加入網址爲:回聲$協議[0],$網址[0],$ filename [0],$ ext [0],我想用更少的變量來完成它,比如completeURL [0] [0] [0] [0](第一個文件節點及其所有相應的部分) ,completeURL [1] [1] [1] [1](第二個文件節點與所有url部分)等...

但顯然我做錯了什麼... 有人可以告訴我哪裏的錯誤??應有關多維數組我試圖創造...

回答

0

如果做的是這樣的:

$completeURL = $xmlData->xpath('//file/url/@protocol | //file/url | //file/filename'); 
echo $completeURL[2].$completeURL[1].$completeURL[0]."\n"; 
echo $completeURL[5].$completeURL[4].$completeURL[3]."\n"; 

echo命令打印此:

http://itzanexample.net/folder/subfolder/itzafile.tar.gz 
ftp://itzanotherurl.com/videos/itzavideo.mp4 

使用聯合運營|使用PHP5 + SimpleXML + XPath獲得預期結果! 但是我不明白爲什麼結果必須向後回顯(2,1,0; 5,4,3)而不是0,1,2,3等。

另外我不明白爲什麼下面的表達式給出了相同的結果呼應了同樣的方式:

$completeURL = $xmlData->xpath('//file/filename | //file/url | //file/url/@protocol'); 

這也解決了這個問題,但對我來說,爲什麼這些2個表達式返回相同的仍是未知。所以訂單不重要?爲什麼需要向後回顯?

所以這確實是一個答案,但需要解釋。有人知道爲什麼?謝謝! =)

+0

很明顯,'echo'輸出結果是評估XPath表達式的節點集中每個節點的字符串值。知道執行'|'時,結果始終以文檔順序*顯示,這一點很重要。所以,'|'的參數順序並不重要。 – 2012-03-06 20:43:33

+0

AH所以這就是原因! 「文件順序」謝謝你已經幫了我很多=) – Metafaniel 2012-03-06 22:21:45

+0

Metafaniel:我很高興我一直有幫助。如果是這樣,你可以考慮更類似於感恩的表達(+1或接受)。 :) – 2012-03-06 23:11:06

1

這是可以做到更簡單,(幾乎)完全的XPath

concat(/files/file[$k]/url/@protocol, 
      /files/file[$k]/url, 
      /files/file[$k]/filename, 
      /files/file[$k]/ext 
     ) 

其中$k必須被取代1,2,...,count((/files/file) - 在僅通過1和這種特殊情況下2.

所以,當這個XPath表達式進行求值

concat(/files/file[1]/url/@protocol, 
      /files/file[1]/url, 
      /files/file[1]/filename, 
      /files/file[1]/ext 
     ) 

的通緝正確的結果產生

http://itzanexample.net/folder/subfolder/itzafile.tar.gz 

當第二XPath表達式求值

concat(/files/file[2]/url/@protocol, 
      /files/file[2]/url, 
      /files/file[2]/filename, 
      /files/file[2]/ext 
     ) 

再次有用,正確的結果產生

ftp://itzanotherurl.com/videos/itzavideo.mp4 
+0

感謝它似乎很有用嗯,但我不知道如何做到這一點在PHP5 =/ – Metafaniel 2012-02-28 18:28:08

+0

@Metafaniel:在C#我會這樣做'nav.Evaluate(string.Format(theExpression,k)) ;'。其中'theExpression'是concat(/ files/file [2]/url/@協議,/ files/file [{0}]/url,/ files/file [{0}]/filename,/ files/file [ {0}]/ext)'。在C中,只需使用'printf()'。當然有一種方法可以在PHP中執行相同的操作。 – 2012-02-28 18:33:04

+0

如果我嘗試按如下方式使用concat和XpAth和PHP: '$ completeURL = $ xmlData-> xpath('concat(// file/url/@ protocol,// file/url,// file/filename)' );」 我得到這些錯誤:'PHP Notice:Undefined offset:0'我找到了另一種方法來做我想做的事,我會在下面發佈它... – Metafaniel 2012-03-06 20:15:29