需要能够使用shell脚本将字符串(路径)添加到XML文件

时间:2017-12-06 12:22:52

标签: xml shell sed xml-parsing xmlstarlet

我需要能够使用shell脚本将字符串(路径)添加到XML文件中, 这是我的XML:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<configurations>
    <smtpHost>smtp3.gmail.com</smtpHost>
    <smtpPort>25</smtpPort>
    <emailFrom>GitPushNotifier@gmail.com</emailFrom>
    <emailSubject>Push notification</emailSubject>
    <!-- Stash general URL-->
    <gitViewerURL>http://server0005.gmail.net:7990/projects/</gitViewerURL>
    <rule>
        <name>test_12.55.4</name>
        <repo>test</repo>
        <branch>refs/heads/12.55.4</branch>
        <emailTo>test@gmail.com</emailTo>
        <path>Server/.*/resources/schema/v.*/.*/.*-dbSchemaDescriptor\.xml,Server/.*/resources/SpringIOC/dataupgrader/v.*/.*/.*-dataUpgrader\.xml,Server/.*/java/com/hp/test/dataupgrader/v.*/.*/.*\.java,Server/.*/resources/indexes/v.*/.*\.index,Server/.*/resources/SpringIOC/vanilla/.*\.xml</path>
    </rule>
    <rule>
        <name>test_12.55.10</name>
        <repo>test</repo>
        <branch>refs/heads/12.55.10</branch>
        <emailTo>test@gmail.com</emailTo>
        <path>Server/.*/resources/schema/v.*/.*/.*-dbSchemaDescriptor\.xml,Server/.*/resources/SpringIOC/dataupgrader/v.*/.*/.*-dataUpgrader\.xml,Server/.*/java/com/hp/test/dataupgrader/v.*/.*/.*\.java,Server/.*/resources/indexes/v.*/.*\.index,Server/.*/resources/SpringIOC/vanilla/.*\.xml</path>
    </rule>
    <rule>
        <name>test_12.55.6</name>
        <repo>test</repo>
        <branch>refs/heads/12.55.6</branch>
        <emailTo>test@gmail.com</emailTo>
        <path>Server/.*/resources/schema/v.*/.*/.*-dbSchemaDescriptor\.xml,Server/.*/resources/SpringIOC/dataupgrader/v.*/.*/.*-dataUpgrader\.xml,Server/.*/java/com/hp/test/dataupgrader/v.*/.*/.*\.java,Server/.*/resources/indexes/v.*/.*\.index,Server/.*/resources/SpringIOC/vanilla/.*\.xml</path>
    </rule>
</configurations>

我需要为一个版本添加一个子节点的额外路径(比如说test_12.55.10),我要添加的路径是:

服务器/ /资源/架构/ v12_55_10 /. / -dbSchemaDescriptor.xml, 服务器/. /资源/ SpringIOC / dataupgrader / v12_55_10 / / -dataUpgrader.xml, 服务器/ / JAVA / COM / HP / MQM / dataupgrader / v12_55_10 /. / 的.java, 服务器/. /资源/索引/ v12_55_10 /.*。索引

我想使用sed或“xmlstarlet”,因为我看到其他人的建议, 所以我想得到的输出是:

<path>Server/.*/resources/schema/v.*/.*/.*-dbSchemaDescriptor\.xml,Server/.*/resources/SpringIOC/dataupgrader/v.*/.*/.*-dataUpgrader\.xml,Server/.*/java/com/hp/test/dataupgrader/v.*/.*/.*\.java,Server/.*/resources/indexes/v.*/.*\.index,Server/.*/resources/SpringIOC/vanilla/.*\.xml,Server/.*/resources/schema/v12_55_10/.*/.*-dbSchemaDescriptor\.xml,
Server/.*/resources/SpringIOC/dataupgrader/v12_55_10/.*/.*-dataUpgrader\.xml,
Server/.*/java/com/hp/mqm/dataupgrader/v12_55_10/.*/.*\.java,
Server/.*/resources/indexes/v12_55_10/.*\.index</path>

2 个答案:

答案 0 :(得分:2)

可以用sed完成。这是一个小的sed脚本,只要在path name rule后面# filter for block from name to end of rule with the version: /<name>test_12.55.10<\/name>/,/<\/rule>/ { /<\/path>/ { # this is a multiline second argument to s: s+<\/path>+,\ Server/./resources/schema/v12_55_10/./.-dbSchemaDescriptor.xml,\ Server/./resources/SpringIOC/dataupgrader/v12_55_10/./.-dataUpgrader.xml,\ Server/./java/com/hp/mqm/dataupgrader/v12_55_10/./..java,\ Server/./resources/indexes/v12_55_10/.*.index\ </path>\ +; # remove the whitespaces inserted above for readability s/,[ \n]+Server//g; } }; 就可以运行。

<强> script.sed

sed -rf script.sed yourfile.xml

您可以这样使用:Server

您可以从shell脚本填充版本号和其他路径(请注意每个namespace SGP.Dto.Custo { public class PlanilhaCusto { public int id{ get; set; } public int parenteId{ get; set; } public string name { get; set; } public decimal amount{ get; set; } public decimal price{ get; set; } public PlanilhaCusto(int pId, int pParenteId, pName, decimal pAmount, decimal pPrice) { id = pId; parentId = pParentId; name = pName; amount = pAmount; price = pPrice; } } } namespace SGP.Dto.Custo { public class ShowList { List<Dto.Custo.PlanilhaCusto> myList = new List<PlanilhaCusto>(); public void Show() { myList.Add(new PlanilhaCusto(1, null, "Projetos", 0, 0)); myList.Add(new PlanilhaCusto(2, 1, "Arquitetura", 5,10)); myList.Add(new PlanilhaCusto(3, 1, "Estrutura", 0, 0)); myList.Add(new PlanilhaCusto(4, 3, "Civil", 1, 50)); myList.Add(new PlanilhaCusto(5, 3, "Infra", 3, 75)); myList.Add(new PlanilhaCusto(6, null, "Pessoal", 0, 0)); myList.Add(new PlanilhaCusto(7, 6, "Mão de Obra", 20, 5700)); /*In this loop the value of the parent items must be updated (calculated). The hierarchy of the list can be unlimited, like a tree. I tried using a recursive method but I could not do it.*/ foreach (var itemList in myList) { } } } } 行后面的附加反斜杠)。

答案 1 :(得分:1)

这是您使用xmlstarlet的一种方式,就像您最初要求的那样......

name="test_12.55.10"
path=", Server/./resources/schema/v12_55_10/./.-dbSchemaDescriptor.xml, Server/./resources/SpringIOC/dataupgrader/v12_55_10/./.-dataUpgrader.xml, Server/./java/com/hp/mqm/dataupgrader/v12_55_10/./..java, Server/./resources/indexes/v12_55_10/.*.index"

xmlstarlet ed -L -a "/*/rule[name='$name']/path/text()" --type text -n "" -v "$path" input.xml

警告:这会修改文件。(因为-L

如果您不使用变量,这可能是一个单行。