Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks
Im new to Perl and started to learn it very recently. Im working with XML files and I was told my best friend in this case would be XML::LibXML. I did use it to first parse and play around with my XML files. Its pretty neat and easy.
However, what I need to do now is first create a new XML file then copy XML elements over from another XML file. I looked for few examples on line and found that creating and XML file would be achieved using
XML::LibXML::Document->new('1.0', 'utf-8') then I can use createAttribute and createElement ... etc. to construct my file.
what im trying to achieve should look like the following example
<Key="1234"> ***XML ELEMENTS HERE FROM ANOTHER FILE0*** <Status="in use"/> <Features> <File1> ***XML ELEMENTS HERE FROM ANOTHER FILE1*** </File1> <File2> ***XML ELEMENTS HERE FROM ANOTHER FILE2*** </File2> <File3> ***XML ELEMENTS HERE FROM ANOTHER FILE3*** </File3> </Features> <Other_Status/> </Key>
The approach I have in mind is a loop through all my source files create my new elements and while doing that I copy source elements over accordingly. Then copy from the next file so on as so forth. I just don't know how to copy the element itself.
my XML files are hundreds of lines otherwise I could have used "findnodes" get my values and create my elements again from scratch in the new file. Maybe I can still use it here I don't know and that's what im looking for, what is the best Perl module I can stick with for such job? Or what is the best approach to achieve such result. Some code sample would be appreciated.
my source files are all like this, all have the same structure and share the element <ABC id=" ">:
<doc> <ABC id="1"> <Feature> <Number>86839</Number> <Prefix>419</Prefix> <Alt>73924/Alt> </Feature> </ABC> <ABC id="2"> <Feature> <Number>82783</Number> <Prefix>826</Prefix> <Alt>27800</Alt> </Feature> </ABC> <ABC id="3"> <Feature> <Number>82783</Number> <Prefix>827</Prefix> <Alt>26433</Alt> </Feature> </ABC> <doc/>
|
|---|