【问题标题】:Replace values in xml remote file and save [duplicate]替换xml远程文件中的值并保存[重复]
【发布时间】:2015-09-23 19:18:14
【问题描述】:

我有一个远程 xml 文件:http://my-site/my-file.xml 此文件具有以下值:

<files>

  <file>
     <unique>444444</unique>
  </file>
  <file>
     <unique>666666</unique>
  </file>
  <file>
     <unique>888888</unique>
  </file>

</files>

我需要用php将&lt;unique&gt;xxxxxx&lt;/unique&gt;的值替换为它的一半,所以文件应该改为

<files>

  <file>
     <unique>222222</unique>
  </file>
  <file>
     <unique>333333</unique>
  </file>
  <file>
     <unique>444444</unique>
  </file>

</files>

我得到了打开和保存文件的部分功能,但没有找到和替换代码:

$xml_external_path = 'http://my-site/my-file.xml'; // THIS LINE MUST EXIST
$xml = simplexml_load_file($xml_external_path);// THIS LINE MUST EXIST

$searches = array();
$replacements = array();
foreach (....) {
    $searches[] = ....
    $replacements[] = ....
}
$newXml = simplexml_load_string( str_replace( $searches, $replacements, $xml->asXml() ) );

$newXml->asXml('new-xml.xml');// THIS LINE MUST EXIST

【问题讨论】:

  • 这是不正确的关闭标签 - &lt;file/&gt; &lt;files/&gt; simplexml 无法使用它
  • @splash58 是一个错字,我刚刚编辑了我的帖子。
  • 仅适用于唯一元素?
  • @miglio 是的,只有那个
  • 文件名无效,示例在到达该行的那一刻停止,对加载操作的返回值进行操作。

标签: php xml replace find preg-replace


【解决方案1】:

您可以将 preg_replace_callback 用于模式:

$txt = <<<XML
<?xml version='1.0'?>
<files>
  <file>
     <unique>444444</unique>
  </file>
  <file>
     <unique>666666</unique>
  </file>
  <file>
     <unique>888888</unique>
  </file>
</files>
XML;
// load the xml like a text
$xml_external_path = 'http://my-site/my-file.xml;'; 
$txt = file_get_contents($xml_external_path);

$pattern = '/<unique>(.*?)<\/unique>/';
$response = preg_replace_callback($pattern,function($match){
    $value = intval(trim($match[1]))/2;
    return '<unique>'.$value.'</unique>';
},$xml);

$newXml = simplexml_load_string( $response );//
$newXml->asXml('new-xml.xml');//create the xml

//print_r($newXml);

【讨论】:

  • 抱歉,它不起作用。您可以在我的代码的 xml 函数中插入您的代码吗?我想我迷路了。
  • 不工作,您可以插入此代码以使用以下 2 行开始您的代码:'$xml_external_path = 'my-site/my-file.xml;'和'$xml = simplexml_load_file($xml_external_path);'
【解决方案2】:
<?
$str = '<files>    
  <file>
     <unique>444444</unique>
  </file>
  <file>
     <unique>666666</unique>
  </file>
  <file>
     <unique>888888</unique>
  </file>
</files>';

$xml = simplexml_load_string($str);
$set = $xml->xpath('//unique');
foreach ($set as &$item)
  $item[0] = $item/2;

echo $xml->asXml();

结果

<?xml version="1.0"?>
<files>
  <file>
     <unique>222222</unique>
  </file>
  <file>
     <unique>333333</unique>
  </file>
  <file>
     <unique>444444</unique>
  </file>
</files>

【讨论】:

  • 看起来不错,但是如何将新文件文件另存为'new-xml.xml'
  • asxml 文件名作为参数
  • $xml->asXml('new-xml.xml');
  • 你能不能用这 2 行开始你的代码:'$xml_external_path = 'my-site/my-file.xml';'和'$xml = simplexml_load_file($xml_external_path);'
  • 只将我的第一行更改为您的两行。一切都会工作
【解决方案3】:

xslt 的一种方式:

define('XML_PATH', 'http://my-site/my-file.xml'); 
define('XSL_PATH', 'divide.xsl');

$xml = new DOMDocument;
$xml->load(XML_PATH);

$xsl = new DOMDocument;
$xsl->load(XSL_PATH);

$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); 

echo $proc->transformToXML($xml);

divide.xsl 在哪里:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="node()|@*">
    <xsl:copy>
      <xsl:apply-templates select="node()|@*" />
    </xsl:copy>
  </xsl:template>
  <xsl:template match="unique">
    <xsl:value-of select=". div 2"/>
  </xsl:template>
</xsl:stylesheet>

【讨论】:

    猜你喜欢
    • 2011-05-22
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 2017-09-22
    • 2019-11-10
    • 2014-06-26
    相关资源
    最近更新 更多