官术网_书友最值得收藏!

How to do it...

Let's begin our activity as follows:

  1. Open a terminal and create the data-csv-to-xml.sh script with the following contents. Then, execute the script after saving it using $ bash data-csv-to-xml.sh:
#!/bin/bash

# Import template variables
source xml-parent.tpl
source word.tpl

OUTPUT_FILE="words.xml"
INPUT_FILE="words.csv"
DELIMITER=','

# Setup header
echo ${XML_HDR} > ${OUTPUT_FILE}
echo ${SRT_CONTR} >> ${OUTPUT_FILE}

# Enter content
echo ${ELM} | \
sed '{:q;N;s/\n/\\n/g;t q}'| \
awk \
'{ print "awk \x27 BEGIN{FS=\"'${DELIMITER}'\"}{print "$0"}\x27 '${INPUT_FILE}'"}' | \
sh >> ${OUTPUT_FILE}

# Append trailer
echo ${END_CONTR} >> ${OUTPUT_FILE}

cat ${OUTPUT_FILE}
  1. Examine the output, but be aware that "pretty" XML isn't necessary and in fact, we don't even need to have the XML on multiple lines. If pure data is required for a web application, the extra new lines and tabs are unnecessary data to be transmitted.
  2. Create another script named data-xml-to-json.sh with the following contents. Then, execute the script after saving it using  data-xml-to-json.sh:
!#/bin/bash
INPUT_FILE"words.xml"
OUTPUT_FILE="words.json"

# Easy one line!
xml2json < ${INPUT_FILE} ${OUTPUT_FILE}
  1. Review the output and see how it easy it is! Are there areas you could improve on in both of the scripts?
主站蜘蛛池模板: 皮山县| 龙门县| 静乐县| 乌兰浩特市| 崇礼县| 博湖县| 安丘市| 大化| 义乌市| 大同市| 定远县| 金坛市| 华容县| 西宁市| 阳高县| 格尔木市| 鸡东县| 灵山县| 岳池县| 凤翔县| 墨江| 乐亭县| 巨鹿县| 保德县| 保山市| 健康| 乐清市| 芮城县| 蓬安县| 尤溪县| 巫溪县| 东丰县| 汉沽区| 龙陵县| 云和县| 沛县| 探索| 德江县| 南溪县| 肃南| 南乐县|