华域联盟 linux shell shell生成简单格式的xml实例

shell生成简单格式的xml实例

以下是shell代码:
create_xml.sh

复制代码 代码如下:

#! /bin/bash

#author: dengzhaoqun
#date: 201202/14

outfile=out
tabs=0

put(){
 echo '<'${*}'>' >> $outfile
}

put_head(){
 put '?'${1}'?'
}

out_tabs(){
 tmp=0
 tabsstr=""
 while [ $tmp -lt $((tabs)) ]  do
  tabsstr=${tabsstr}'\t'
  tmp=$((tmp+1))
 done
 echo -e -n $tabsstr >> $outfile
}

tag_start(){
 out_tabs
 put $1
 tabs=$((tabs+1))
}

tag_end(){
 tabs=$((tabs-1))
 out_tabs
 put '/'${1}
}

tag_value(){
 out_tabs
 str=""
 str=${1}' value="'${2}'"/'
 put $str
}

以下是测试代码:
ts_xml.sh

复制代码 代码如下:

#! /bin/bash

#author: dengzhaoqun
#date: 20120215

source './create_xml.sh'
put_head 'xml version='1.0' encoding="GBK"'
tag_start '投递信息'
tag_start '硬件'
tag_value '网卡' 1
tag_end '硬件'
tag_end '投递信息'

生成的xml文件如下

复制代码 代码如下:

<?xml version=1.0 encoding="GBK"?>

<投递信息>

 <硬件>

  <网卡 value="1"/>

 </硬件>

</投递信息>

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » shell生成简单格式的xml实例

转载请保留出处和原文链接:https://www.cnhackhy.com/16792.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部