如何使用 unix shell 脚本将 xml 文件转换为属性文件 [英] How to convert xml file in to a property file using unix shell script

查看:34
本文介绍了如何使用 unix shell 脚本将 xml 文件转换为属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xml 文件 source.xml.我想用这个 xml 文件信息中的键值对创建一个属性文件.

I have an xml file source.xml. I want to create an property file with key-value pairs from this xml file info.

<?xml version="1.0" ?>
<persons>
  <person>
    <name>John</name>
    <family-name>Smith</family-name>
  </person>
  <person>
    <name>Morka</name>
    <family-name>Ismincius</family-name>
  </person>
</persons>

属性文件应该看起来像

"John" = "Smith";
"Morka" = "Ismincius";

任何人使用 Unix Shell 脚本进行过类似的转换,请提出建议..

Anyone did similar kind of conversion using Unix Shell Script please suggest..

是否可以简单地使用 grep 和 sed 命令来完成.

Can it be done simply using grep and sed command.

推荐答案

搜索了很多,终于能够使用简单的 grep 和 sed unix 命令从 xml 创建属性文件.

Searched a lot and finally able to create properties file from xml using simple grep and sed unix commands.

几个链接足以参考:

如何使用 sed 替换换行符 (\n)?

将每个备用行放在一列中使用一些 Unix 命令

脚本有效:

grep -e "<name>" -e "<family-name>" source.xml  | 
sed -e 's/<name>/"/' -e 's/<\/name>/"/' | 
sed -e 's/<family-name>/"/' -e 's/<\/family-name>/";/' |
sed 's/ //g' |
sed 'N;s/\n/ = /' > my.properties

如果有任何其他方法可以使用简单的 unix 命令执行相同操作,请提出建议.

Anyone having any other way to do the same using simple unix commands please suggest.

这篇关于如何使用 unix shell 脚本将 xml 文件转换为属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆