Loop through CSV to create XML

Hello all, first and foremost I want to apologize if this has been answered somewhere, but I am looking to do the following:

I have 500 records I need put in XML format. The records are currently in CSV format. I can use curl to upload a record in XML format using our applications API. I was under the impression that I could generate 1 large XML file and upload the entire group.

I have a script that will read in data from a CSV and output it to a file in XML format. I got the script the way I thought I needed it working, which was to create a new xml file, append a line to it, then loop through the csv and echo my string repeatedly inputing the variables from the CSV file. I could then upload the XML file where I needed it.

As it turns out, it seems the API will only allow me to upload 1 record at a time. (ALMOST pointless IMO.) So what I am looking to do is change the way the script loops through the CSV file, and instead of creating 1 large file, I would like the script to loop through the CSV one line at a time, create the XML file of only 1 record, upload it using curl, delete itself (or its contents) and repeat that process throughout the entire CSV file.

I am just getting familiar with loops, so Im not sure how I would even start this process, or even word what I want properly to search google. (Which I have been for the last two days, thats how I frankenstiened what I have below. Any help would be greatly appreciated.


The script is as follows:


#!/bin/bash
echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' >> xmldata.xml
#:Read variables from XMLInput.csv:#
while read inputline;do
line0="$(echo $inputline | cut -d, -f1)"
line1="$(echo $inputline | cut -d, -f2)"
#:Set auto increment ID:#
idAdd0=$((idAdd0 +1))
#:removes trailing alpha characters leaving only the IP address:#
cutLine0=${line0//[^0-9.]/}
#:Strips trailing octet and appends .1:#
startIP=`echo $cutLine0 | sed 's/.[0-9]*$/.1/'`
#:Strips trailing octet and appens 254:#
endIP=`echo $cutLine0 | sed 's/.[0-9]*$/.254/'`
#:echos line to txt file as XML:#
echo "<network_segment><id>"$idAdd0"</id><name>$line0</name><starting_address>$startIP</starting_address><ending_address>$endIP</ending_address><distribution_point/><url/><netboot_server/><swu_server/><building>$line1</building><department/><override_buildings>false</override_buildings><override_departments>false</override_departments></network_segment>" >> xmldata.xml
done < XMLInput.csv
exit 0

MacBookPro7,1, Mac OS X (10.6.5), Lots of other computers

Posted on Mar 2, 2011 6:59 PM

Reply
1 reply

Mar 2, 2011 10:37 PM in response to iDam81

What I ended up doing was having the script create 500 separate XML files in a folder by adding my auto increment variable to the XML file name,
then curling each file as a separate entry. A litte dirtier than I wanted, but in the end, I didn't have to manually enter 500 subnets
and define there locations in a website. Job Done.

Script if interested:


#!/bin/bash
#:Read variables from XMLInput.csv:#
while read inputline;do
line0="$(echo $inputline | cut -d, -f1)"
line1="$(echo $inputline | cut -d, -f2)"
echo
#:Set auto increment ID:#
idAdd0=$((idAdd0 +1))
#:removes trailing alpha characters leaving only the IP address:#
cutLine0=${line0//[^0-9.]/}
#:Strips trailing octet and appends .1:#
startIP=`echo $cutLine0 | sed 's/.[0-9]*$/.1/'`
#:Strips trailing octet and appens 254:#
endIP=`echo $cutLine0 | sed 's/.[0-9]*$/.254/'`
#:echos line to txt file as XML:#
echo '<?xml version="1.0" encoding="UTF-8" standalone="no"?>'"<network_segment><id>"$idAdd0"</id><name>$line0</name><starting_address>
$startIP</starting_address><ending_address>$endIP</ending_address><distribution_point/><url/><netboot_server/><swu_server/><building>$line1</building>
<department/><override_buildings>false</override_buildings><override_departments>false</override_departments></network_segment>" >> xmldata$idAdd0.xml
#:upload XML file to JSS:#
`curl -k -v -u account:Password https://server address/JSSResource/networksegments/name/SegmentName -T "xmldata$idAdd0.xml" -X POST`
done < XMLInput.csv
exit 0

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Loop through CSV to create XML

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.