You are here:  » Update a field


Update a field

Submitted by Bay_Oz on Wed, 2013-04-03 14:13 in

Hi David,

my xml :

<product>
     <product_code><![CDATA[1742]]></product_code>
     <category><![CDATA[cell-phones]]></category>
     <price><![CDATA[500]]></price>
</product>

I want to update price amount %5 for all records. How can I do that? Is this possible to update records with magicparser

Thanks,

Submitted by support on Wed, 2013-04-03 14:25

Hi,

It's not really a parser function, but your myRecordHandler function() can easily multiply the price field by 5%, you would do something like this:

function myRecordHandler($record)
{
  $record["PRICE"] = sprintf("%.2f",($record["PRICE"] * 1.05));
  // rest of code
}

Hope this helps!
Cheers,
David
--
MagicParser.com

Submitted by Bay_Oz on Wed, 2013-04-03 14:32

is this one updating xml file? I guess not.

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
      
$record["PRICE"] = sprintf("%.2f",($record["PRICE"] * 1.05));
  }
  
MagicParser_parse("file.xml","myRecordHandler","xml|URUNLER/URUN/");
?>

Submitted by Bay_Oz on Wed, 2013-04-03 14:51

i need to update xml file which i parsed.

Submitted by support on Wed, 2013-04-03 15:02

Hi Bay Oz,

I'm afraid not - constructing a modified XML document would be an output function of your script, the parser doesn't modify the input XML document at all.

Constructing XML is reasonably straight forward in PHP, you can send an appropriate header, e.g.

  header("Content-Type: text/xml");

You would have to print suitable XML document elements either side of your parse function, and then your myRecordHandler can re-write each field of $record as valid XML (using <![CDATA[ ... ]]> encapsulation for simplicity.

But please bear in mind XML formation is complex and I can only give you a basic example, but the following script would parse, multiply the price field by 5% and output a _similar_ XML document, but not update the actual source - that is not part of the function of a parser...

<?php
  
require("MagicParser.php");
  
header("Content-Type: text/xml");
  function 
myRecordHandler($record)
  {
    
$record["PRICE"] = sprintf("%.2f",($record["PRICE"] * 1.05));
    print 
"<PRODUCT>";
    foreach(
$record as $k => $v)
    {
      print 
"<".$k."><![CDATA[";
      print 
$v;
      print 
"]]></".$k.">";
    }
    print 
"</PRODUCT>";
  }
  print 
"<PRODUCTS>";
  
MagicParser_parse("file.xml","myRecordHandler","xml|URUNLER/URUN/");
  print 
"</PRODUCTS>";
  exit();
?>

Hope this points you in the right direction!

Cheers,
David

Submitted by Bay_Oz on Wed, 2013-04-03 15:11

Thanks David for great support.