2011-06-21 115 views
0

我有一個.out文件,其中有這樣的XML內容。使用PHP的XML解析

<header stub> 
    <article type="audio"> 
     <addedDate>2010-03-11 05:11:57</addedDate> 
     <thumbnail>http://fgsdfff/4588/thumbnail_9.jpg</thumbnail> 
     <asset="blarga.mp3" addedDate="2009-01-07 01:48:37"> 
      <size>3289048</size> 
      <duration>206000</duration> 
      <mime_type>audio/mpeg</mime_type> 
     </asset> 
    </article> 
</footer stub> 
  1. 我要在XML的開始和分別在末尾添加<?xml version="1.0"?></xml>
  2. 我必須分別用<channel></channel>標籤替換<header stub></footer stub>
  3. 您可能已經注意到<asset>標記中的XML格式不正確。它必須像<asset url="blarga.mp3" addedDate="2009-01-07 01:48:37">。如何添加url屬性?
  4. 在縮略圖標記中,我必須從jpg的名稱中刪除_9。

最後,我必須將.out轉換爲.xml文件。

請幫我看看這些

+4

非XML內容......至少,你給樣本不 – Endophage

+2

究竟什麼是你的問題? –

+0

啊更好,如果代碼格式化有幫助。 – Endophage

回答

1
$content = file_get_content OR mysql_query;//"YOUR XML CONTENT FROM FILE OR MYSQL"; 
$content = "<?xml version="1.0"?>" . $content . "</xml>"; 
$content = str_replace("<header stub>", "<channel>", $content); 
$content = str_replace("</footer stub>", "</channel>", $content); 
$content = str_replace("<asset=", "<asset url=", $content); 
$content = str_replace("thumbnail_9.jpg", "thumbnail.jpg", $content); 
1

可以使用DOMDocument類。

其用法的例子可以發現here

+1

我懷疑SimpleXML在這裏很有用,因爲內容不是格式良好的XML。 –

+0

是的,當問題發生變化時,我發現這一點。我也修改了我的答案。 –