-1

I have a remote xml file: http://my-site/my-file.xml This file has this values:

<files>

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

</files>

I need to use php to replace the value of <unique>xxxxxx</unique> to be half its value, so that the file should be change to

<files>

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

</files>

I got part of the function to open and save the file but not the find&replace code:

$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
JPashs
  • 13,044
  • 10
  • 42
  • 65

3 Answers3

1

you can use preg_replace_callback for a pattern:

$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);
miglio
  • 2,048
  • 10
  • 23
  • sorry but it is not working. Can you insert your code inside the xml functions of my code? I guess I get lost there. – JPashs Jul 06 '15 at 15:29
  • not working, can you insert this code to start your code with these 2 lines: '$xml_external_path = 'http://my-site/my-file.xml;' and '$xml = simplexml_load_file($xml_external_path);' – JPashs Jul 06 '15 at 16:23
0
<?
$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();

result

<?xml version="1.0"?>
<files>
  <file>
     <unique>222222</unique>
  </file>
  <file>
     <unique>333333</unique>
  </file>
  <file>
     <unique>444444</unique>
  </file>
</files>
splash58
  • 26,043
  • 3
  • 22
  • 34
0

A way with 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);

where divide.xsl is:

<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>
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125