doutui7955 2013-06-25 01:12
浏览 36

需要帮助批量查找/替换文本文件的一部分

I have a rather unique situation where I need to make mass edits to an XML (or plain text) file. Within the XML file, there are several image references such as this one:

<image>
  <id>image1</id>
  <dateTaken>Sun Sep 9 10:57:31 GMT-0700 2012</dateTaken>
  <dateAdded>Mon Jun 24 17:44:50 GMT-0700 2013</dateAdded>
  <filename>0881DAF2-80EC-7A25-9CF3-BFD67F4EF764.jpg</filename>
</image>

What I need to do is take the file name between <filename> and </filename>, and use that to replace the portion between <id> and </id>. For example in the case above, I would need to replace "image1" with 0881DAF2-80EC-7A25-9CF3-BFD67F4EF764.jpg so it would look like this:

<image>
  <id>0881DAF2-80EC-7A25-9CF3-BFD67F4EF764.jpg</id>
  <dateTaken>Sun Sep 9 10:57:31 GMT-0700 2012</dateTaken>
  <dateAdded>Mon Jun 24 17:44:50 GMT-0700 2013</dateAdded>
  <filename>0881DAF2-80EC-7A25-9CF3-BFD67F4EF764.jpg</filename>
</image>

There are several hundred of these image references I need to change in this same document. Using PHP or another method, is there a quick way I can mass edit the document and replace the every ID between <id> and </id> with the content between <filename> and </filename>, in every <image> </image> reference within the document? Thanks a bunch for any possible help!

  • 写回答

1条回答 默认 最新

  • dtng25909 2013-06-25 01:33
    关注

    Assuming you have a root to this xml, you could do something like the following by loading the xml as $xml_string:

    $xml = new SimpleXMLElement($xml_string);
    foreach($xml->children() as $child){
    $child->id = $child->filename;
    }
    $xml->asXml('updated.xml');
    

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度