I am quite new to XML. I have an XML file which contains around 8000 products and I will import these products into my own system. I want to modify this XML file for my importer. Sample product on original XML file is below;
<product prodID="PD42BJ1000002020" prodName="Intermec PD42" prodBrand="INTERMEC" globProdID="PD42BJ1000002020">
<tax>KDV18</tax>
<pic>http://.....jpg</pic>
<prodSpec>
<prd spec="Connectivity Interfaces" DEGER="USB - Serial RS232 - Ethernet"/>
<prd spec="Print Width" DEGER="104"/>
<prd spec="Printing Method" DEGER="Direct Thermal - Thermal Transfer"/>
<prd spec="Resolution (DPI)" DEGER="203"/>
</prodSpec>
</product>
I want to change product specs to unique elements like below;
<product prodID="PD42BJ1000002020" prodName="Intermec PD42" prodBrand="INTERMEC" globProdID="PD42BJ1000002020">
<tax>KDV18</tax>
<pic>http://.....jpg</pic>
<pr001>Connectivity Interfaces:USB - Serial RS232 - Ethernet</pr001>
<pr002>Print Width:104</pr002>
<pr003>Printing Method:Direct Thermal - Thermal Transfer</pr003>
<pr004>Resolution (DPI):203</pr004>
</product>
Is it possible to change whole XML document like that?
If you don't mind using Perl for this, here is a way to do it using XML::Twig :
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $infile= shift @ARGV;
XML::Twig->new( twig_handlers => { prodSpec => \&tweak_product },
pretty_print => 'indented',
)
->parsefile( $infile);
exit;
sub tweak_product
{ my( $t, $prodspec)= @_;
my $i=0; # sub tag number
foreach my $prd ($prodspec->children( 'prd'))
{ my $tag= sprintf( 'pr%03d', ++$i);
$prd->set_tag( $tag)
->set_text( $prd->att( 'spec') . ':' . $prd->att( 'DEGER'))
->del_atts;
}
$prodspec->erase;
$t->flush;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.