in reply to Pulling from a list and inserting into XML documents
The library that immediately comes to mind, for me, is XML::LibXML, which is a binding for the libxml2 binary library in all of its glory. I make this recommendation because, not only do you want to parse an XML file, but you also want to modify it and to create a new file containing the modified document. Your approach would be to parse the XML document into a DOM data-structure, then modify the data-structure, then write it out as a new XML file. You may also wish to use XPath expressions and/or XSLT as a means of locating items and possibly also of making the necessary modifications to it. All of which this module can do. Furthermore, since libxml2 is an industry-standard heavy-hitter, you can be confident that the output files will be universally accepted.
Of course, XML::Twig is also a well-known power-hitter in the Perl world of XML, and it has a feature called “XML filters” which might also be useful here. This approach would be more along the line of in-line changes to the XML text, and I think that its applicability to your situation would depend very much on just how precisely you can identify the nodes that need to be modified. (The perldoc contains an excellent example.)
Both of these modules are “go to” modules for XML processing in Perl. (I frankly don’t use XML::Simple very often, but I will mention it here as a third-baseman.)
As for parsing the input file, I agree that Text::CSV is a solid power-tool for this sort of thing, and I have nothing further to add to the prior comments.
Oh yes, one more thought: You list three XML nodes which I presume occur in sequence. I would also presume that all three of them occur as children of some single node-type which you did not mention in your OP description. An XPath or XSLT-oriented approach would zero in on this parent node-type and then locate and modify all three of the nodes that are underneath it. (I am not entirely sure how a Twig filter would do the same thing.)
And, do be sure to carefully examine how XSLT technology might be applicable to this situation: it may well prove to be the case that you can bypass a lot of otherwise-messy Perl programming. This Interactive Periodic Table of the Elements web page was constructed entirely(!!) using XSLT!
| Replies are listed 'Best First'. |
|---|