文件中的动态内容

文件中的动态内容

本文介绍了替换 XML 文件中的动态内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速总结:我需要创建一个 Bash 脚本来每周自动更改节点内的文本.脚本将匹配节点并替换其中的文本(如果可能)?我该怎么做?

Quick Summary: I need to create a Bash script to change the text within a node automatically every week. The script will match the node and replace the text inside them (if this is possible)? How would I do this?

长摘要:我托管了一个 Minecraft 服务器,里面有商店,每个商店在/ShowcaseStandalone/ffs-storage/目录中都有自己的 .xml 文件.每个星期天我的服务器都会重新启动并在终端中执行几个命令以重置一些东西.我试图改变的一件事是其中一家商店.我想更改节点 <itemstack> 中的文本.以及节点<price>中的文本.我只是想从不同文件夹中的 .txt 文件中获取文本,并将其插入到该节点中.问题是,节点中的文本每周都会更改.有没有办法用bash替换两个节点内的特定行或文本?

Long Summary:I host a Minecraft server which has shops, each of which have their own .xml file in the /ShowcaseStandalone/ffs-storage/ directory. Every Sunday my server restarts and executes several commands into the terminal to reset several things. One thing that I am trying to make change is one of the shops. I am wanting to change the text in the node <itemstack> and the text in the node <price>. I am simply wanting to take text from a .txt file in a different folder, and insert it into that node. The problem is, that the text in the node will change every week. Is there any way to replace a specific line or text within two nodes using bash?

XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<scs-shop usid="cac8480951254352116d5255e795006252d404d9" version="2" type="storage">
    <enchantments type="string"/>
    <owner type="string">Chadward27</owner>
    <world type="string">Frisnuk</world>
    <itemStack type="string">329:0</itemStack>
    <activity type="string">BUY</activity>
    <price type="double">55.0</price>
    <locX type="double">487.5</locX>
    <locY type="double">179.0</locY>
    <locZ type="double">-1084.5</locZ>
    <amount type="integer">0</amount>
    <maxAmount type="integer">0</maxAmount>
    <isUnlimited type="boolean">true</isUnlimited>
    <nbt-storage usid="23dffac5fb2ea7cfdcf0740159e881026fde4fa4" version="2" type="storage"/>
</scs-shop>

操作系统:Linux Ubuntu 12.04

Operating System: Linux Ubuntu 12.04

推荐答案

您可以使用 xmlstarletshell 中编辑 XML 文件,如下所示:

You can use xmlstarlet to edit a XML file in a shell like this :

xmlstarlet edit -L -u "/scs-shop/price[@type='double']" -v '99.66' file.xml

注意

  • "/scs-shop/price[@type='double']" 是一个 Xpath 表达
  • xmlstarlet ed --help
  • "/scs-shop/price[@type='double']" is a Xpath expression
  • see xmlstarlet ed --help

这篇关于替换 XML 文件中的动态内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 22:13