如何创建生成一个XML事后运行DOS命令C / C ++程序? [英] How to create a C/C++ program that generates an XML and runs a DOS command afterwards?

查看:120
本文介绍了如何创建生成一个XML事后运行DOS命令C / C ++程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要拿出生成一个XML文件中像这样的程序:

I need to come up with a program that generates an xml file like this:

<?xml version="1.0"?>
<xc:XmlCache xmlns:xc="XmlCache" xc:action="Update">
    <xc:XmlCacheArea xc:target="AllSubFields" xc:value="MarketParameters">
        <mp:nickName xmlns:mp="mx.MarketParameters" xc:value="MDS">
            <mp:date xc:value="TODAY">
                <fx:forex xmlns:fx="mx.MarketParameters.Forex">
                    <fxsp:spot xmlns:fxsp="mx.MarketParameters.Forex.Spot">
                        <fxsp:pair type="Fields" value="USD/BRL">
                            <mp:ask xc:type="Field" xc:keyFormat="N">1.890</mp:ask>
                            <mp:bid xc:type="Field" xc:keyFormat="N">1.800</mp:bid>
                        </fxsp:pair>
                    </fxsp:spot>
                </fx:forex>
            </mp:date>
        </mp:nickName>
    </xc:XmlCacheArea>
</xc:XmlCache>

在各节点的值熔点:问熔点:出价随机生成而是两个predefined值(1.65和1.99)之间的

with the values in the nodes mp:ask and mp:bid randomly generated but between two predefined values (1.65 and 1.99).

后,程序应该指出cmd命令行运行命令:

After the xml is generated in the same directory of the program, the program should run a command in the cmd command line that states:

cachetool.bat -i cacheBody.xml -u REALTIME

其中, cachetool.bat 是不能改变的,而且也放在程序的同一目录下,并在 cacheBody.xml 是previously生成的xml。

where cachetool.bat is an already done bash script that cannot be changed and that is also place in the same directory of the program, and where cacheBody.xml is that previously generated xml.

这里的窍门是,这应该反复运行每次都覆盖用新值xml文件,然后再次运行该命令调用带新值的XML。

The trick here is that this should run repeatedly overwriting the xml file with new values each time and then running the command again calling the xml with the new values.

应该有一种方法来容易打断循环,但除此之外,这应该无限期地运行下去。

There should be a way to easy interrupt the loop, but besides that, this should run indefinitely.

注:没有使用C或C ++严格的规则,如果它不是在这些语言可行或是否有其他方式轻松做到这一点,请随时提出。我最初的建议是在这些语言,因为这是两个,我是用一点点来处理。

Note: there isn't a strict rule to use c or c++, if it isn't feasible in these languages or if there other ways to do it easily, please feel free to suggest. My initial proposal is in these languages because these are the two that I'm a little used to deal with.

推荐答案

我正在学习如何使用JavaScript的Windows本地脚本,所以这里是一个JavaScript的解决方案。

I'm learning how to use javascript for Windows local scripting, so here's a solution in javascript.

看起来你并不真的需要动态生成XML,而XML结构是静态的,只有一对夫妇的数据字段是动态的。考虑到这一点,我走近了问题的搜索和替换使用的模板文件。

It looks like you don't really need to generate the XML dynamically, but rather the XML structure is static and only a couple data fields are dynamic. With that in mind, I approached the problem with search-and-replace using a template file.

模板文件( Template.xml中)包含XML内容的一些变量来搜索和替换。格式变量格式为: $ RANDOM_X_Y $ ,其中 X 是随机数的下限和上限。为了帮助这个例子中,我产生的要求和投标价格略有不同的模板文件:

The template file (template.xml) contains xml content with some variables to search and replace. The format variable format is $RANDOM_X_Y$, where X and Y are the lower and upper bounds for the random number. To help the example, I generated the ask and bid prices slightly differently in the template file:

<?xml version="1.0"?>
<xc:XmlCache xmlns:xc="XmlCache" xc:action="Update">
    <xc:XmlCacheArea xc:target="AllSubFields" xc:value="MarketParameters">
        <mp:nickName xmlns:mp="mx.MarketParameters" xc:value="MDS">
            <mp:date xc:value="TODAY">
                <fx:forex xmlns:fx="mx.MarketParameters.Forex">
                    <fxsp:spot xmlns:fxsp="mx.MarketParameters.Forex.Spot">
                        <fxsp:pair type="Fields" value="USD/BRL">
                            <mp:ask xc:type="Field" xc:keyFormat="N">1.$RANDOM_65_99$0</mp:ask>
                            <mp:bid xc:type="Field" xc:keyFormat="N">1.$RANDOM_650_990$</mp:bid>
                        </fxsp:pair>
                    </fxsp:spot>
                </fx:forex>
            </mp:date>
        </mp:nickName>
    </xc:XmlCacheArea>
</xc:XmlCache>

JavaScript文件被称为 replace.js 。所有版本的Windows应该能够无需安装任何额外的组件本地执行。

The javascript file is called replace.js. All versions of Windows should be able to execute it natively without installing any extra components.

if( WScript.Arguments.Count() != 2 || WScript.Arguments.Item(0) == WScript.Arguments.Item(1) )
{
    WScript.Echo("Usage: replace.js <template> <output filename>");
    WScript.Quit();
}

var template_filename = WScript.Arguments.Item(0);
var output_filename = WScript.Arguments.Item(1);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var ForReading = 1;
var file, file_contents, lower, upper;
var var_regex = /\$RANDOM_(\d+)_(\d+)\$/g;

if( fso.FileExists(template_filename) )
{
    file = fso.OpenTextFile(template_filename, ForReading, false);

    file_contents = file.ReadAll().replace(var_regex,
            function(str, lower, upper) {
                return Math.floor(
                    Math.random() * (+upper - +lower + 1)) + +lower;
            });
    file.Close();

    file = fso.CreateTextFile(output_filename, true);
    file.Write(file_contents);
    file.Close();

}
else
{
    WScript.Echo("Template does not exist: " + template_filename);
}

现在无限期运行脚本,只需创建一个名为批处理文件的的run.bat 或什么,并在一个循环中运行的JavaScript和批处理文件。 <大骨节病> CTRL - <大骨节病> C 将退出脚本

Now to run your scripts indefinitely, just create a batch file called run.bat or whatever and have it run the javascript and batch files in a loop. CTRL-C will exit the script.

@echo off
echo Starting.  Press CTRL-C to exit.
:loop
        replace.js template.xml cacheBody.xml
        cachetool.bat -i cacheBody.xml -u REALTIME
goto loop

这篇关于如何创建生成一个XML事后运行DOS命令C / C ++程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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