2011-06-21 114 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

是的,当问题发生变化时,我发现这一点。我也修改了我的答案。 –