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

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?
主站蜘蛛池模板: 出国| 和林格尔县| 宜城市| 梨树县| 革吉县| 土默特右旗| 紫金县| 剑河县| 汉沽区| 托克托县| 海伦市| 蚌埠市| 霍山县| 巴彦县| 东至县| 江西省| 灌南县| 通州市| 博乐市| 贵溪市| 通河县| 吉木萨尔县| 合阳县| 南和县| 河曲县| 隆子县| 济宁市| 平泉县| 大悟县| 阿勒泰市| 博白县| 北宁市| 桃园市| 颍上县| 光泽县| 于田县| 青田县| 开江县| 广灵县| 佛冈县| 米泉市|