Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Create an XML file from input text file

by szpt9m (Novice)
on Apr 26, 2022 at 18:14 UTC ( [id://11143318]=perlquestion: print w/replies, xml ) Need Help??

szpt9m has asked for the wisdom of the Perl Monks concerning the following question:

Hello All, Need a help. I have an input text file as below Input.txt
Item1 Item2 Item3 Item4 Item5 -- -- -- ItemN
I want to iterate the text file and create an xml file as below. Basically all other fields are same in output XML file but just need to update attrValue field for attrName=ID" using the input text file. Output Expected:
<?xml version=1.0" encoding="UTF-8"?> <BulkUpdate> <UpdateSets> <UpdateSet> <type name="MyType" /> <where> <cond_prop attrName=ID" attrValue="Item1" cond_operator="="/> </where> <update> <update_prop attrName="sh4_SAP_ID" attrValue="" /> </update> </UpdateSet> <UpdateSet> <type name="MyType" /> <where> <cond_prop attrName=ID" attrValue="Item2" cond_operator="="/> </where> <update> <update_prop attrName="sh4_SAP_ID" attrValue="" /> </update> </UpdateSet> <UpdateSet> <type name="MyType" /> <where> <cond_prop attrName=ID" attrValue="Item3" cond_operator="="/> </where> <update> <update_prop attrName="sh4_SAP_ID" attrValue="" /> </update> </UpdateSet> <UpdateSet> <type name="MyType" /> <where> <cond_prop attrName=ID" attrValue="Item4" cond_operator="="/> </where> <update> <update_prop attrName="sh4_SAP_ID" attrValue="" /> </update> </UpdateSet> <UpdateSet> <type name="MyType" /> <where> <cond_prop attrName=ID" attrValue="Item5" cond_operator="="/> </where> <update> <update_prop attrName="sh4_SAP_ID" attrValue="" /> </update> </UpdateSet> -- -- -- <UpdateSet> <type name="MyType" /> <where> <cond_prop attrName=ID" attrValue="ItemN" cond_operator="=" / +> </where> <update> <update_prop attrName="sh4_SAP_ID" attrValue="" /> </update> </UpdateSet> </UpdateSets> </BulkUpdate>

Replies are listed 'Best First'.
Re: Create an XML file from input text file
by afoken (Chancellor) on Apr 26, 2022 at 18:21 UTC

    Again, show your code so far. Perlmonks is not a code writing service.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re: Create an XML file from input text file
by choroba (Cardinal) on Apr 26, 2022 at 21:55 UTC
    I usually use XML::LibXML to handle XML.
    #!/usr/bin/perl use strict; use warnings; use XML::LibXML; my $item_list = shift; my $xml = << '__XML__'; <BulkUpdate> <UpdateSets> <UpdateSet> <type name="MyType" /> <where> <cond_prop attrName="ID" cond_operator="="/> </where> <update> <update_prop attrName="sh4_SAP_ID" attrValue="" /> </update> </UpdateSet> </UpdateSets> </BulkUpdate> __XML__ my $dom = 'XML::LibXML'->load_xml(string => $xml); my ($sets) = $dom->findnodes('/BulkUpdate/UpdateSets'); my ($update) = $sets->findnodes('UpdateSet'); open my $in, '<', $item_list or die $!; while (<$in>) { chomp; my $set; if ($. > 1) { $set = $update->cloneNode(1); $sets->appendChild($set); } else { ($set) = $dom->findnodes('/BulkUpdate/UpdateSets/UpdateSet[las +t()]'); } my ($cond_prop) = $set->findnodes('where/cond_prop'); $cond_prop->{attrValue} = $_; } print $dom;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: Create an XML file from input text file
by perlfan (Vicar) on May 01, 2022 at 02:32 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11143318]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2024-04-25 15:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found