in reply to how to update an xml file by read data from csv file
Text::CSV makes reading and writing CSV files very easy. It has a simple example in the synopsis that will probably do what you need just by copying and changing the filename. That takes care of the first half of your problem.
If you're always using exactly the same input XML file you can probably get away with using a regex (regex tutorial)) to search and replace the elements that you want to change-- simply read in the file as text, make the change, and write it out.
If you start making a habit of modifying XML files, especially if they're variable and may be a little unpredictable, you'll probably want to look into XML::TreeBuilder or XML::LibXML
Try putting something together yourself, and if you run into trouble, post back in this thread and show your code and describe what it's doing wrong
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: how to update an xml file by read data from csv file
by Vijay81 (Acolyte) on Nov 29, 2012 at 21:38 UTC | |
Thanks bitingduck (Pilgrim) for you input. As you suggested i used Text::CSV module to read csv file. Main pl file :I was able to achive almost expect for few things. Anyhelp would be much appreciated. Achieved so far : 1. able to read data from csv file 2. ouput the xml file with data read from csv file. Now Issues are : 1. Don't want to print first 7 lines of the xml format everytime. It should be printed only once at top of the file. 2. Don't want to print last 2 line everytime. it just need to be printed only once at bottom of the file 3. Not sure where i made mistake, but when ever i try to open the result.xml, it doesn't open. even program doesn't give any error thou. | [reply] [Watch: Dir/Any] [d/l] [select] |
by choroba (Cardinal) on Nov 29, 2012 at 21:48 UTC | |
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
| [reply] [Watch: Dir/Any] [d/l] |
by bitingduck (Chaplain) on Nov 30, 2012 at 10:00 UTC | |
Here's a solution that uses XML::TreeBuilder (which uses XML::Element methods as well). It seemed easier than trying to build it up from print statements and regex replacements. One maybe major caveat: your example input file isn't valid XML-- the two "container" tags don't get closed. I took the liberty of assuming (maybe incorrectly) that they should get closed and that you want valid XML. Here's the code:And here's the output:
| [reply] [Watch: Dir/Any] [d/l] [select] |
by Vijay81 (Acolyte) on Dec 01, 2012 at 23:23 UTC | |
Thanks bitingduck (Pilgrim). Your suggested solution is very clear and neat way of producing xml format file. But only problem is that i don't want to display last 2 closing container tags. I did try removing that from wrapper but it didn't work. Is ther any way we can't avoid writing the last 2 container tags please.. Error we get if we remove the closing tag :Main program :
| [reply] [Watch: Dir/Any] [d/l] [select] |
by bitingduck (Chaplain) on Dec 03, 2012 at 16:08 UTC | |
by Vijay81 (Acolyte) on Dec 03, 2012 at 21:09 UTC | |
|