STAX XML 解析
利用stax xml文件可以提交Job给staf执行。下面结合具体实例讲述XML如何编写。 1 STAX基本的Job模板 1: <?xml version="1.0" encoding="UTF-8" standalone="no"?>2: <!DOCTYPE stax SYSTEM "stax.dtd"> 3: 4: <stax> 5: 6: <defaultcall function="main"/> 7: 8: <function name="main"> 9: <nop/> 10: </function> 11: 12: </stax> 第一行和第二行一般都是一样的,每一个stax Job以stax标签开头。function 元素是Job中的主要的结构化机制,仅仅能stax根目录进行定义。defaultcall用来定义当Job提交的时候被调用的函数。nop是个空元素,表示什么都不做。 2利用stax启动notepad 1: <?xml version="1.0" encoding="UTF-8" standalone="no"?> process元素:包含两个子元素location和command,其中location用来申明任务执行的机器,可以是本地的机器也可以是远程的机器,如果是远程的机器,必须在staf的配置文件中申明安全等级为5.本地直接用local表示。command为执行的具体命令,两个元素的值都用单引号,表示为Python的字符串。 上述实例是用来打开notepad的job。利用STAF Monitor可以用来提交job以及检测执行。 首先启动STAX检测器:C:STAFservicesstax>java -jar STAXMon.jar 选择"文件"->"提交新任务",在出现的界面中选择需要提交的job对应的XML文件,并设置相关job的名称,然后提交即可 3 stafcmd <stafcmd> 获取stafcmd命令执行的返回值: 成功执行的时候信息显示如下: 20110915-10:09:54 Great! STAF/Config/OS/Name = WinXP process也可以去验证执行的返回值,并且可以使用env元素申明环境变量。returnstdout用来申明process 完成时候的标准输出,而stderr则是表示标准错误输出。 <!DOCTYPE stax SYSTEM "stax.dtd"> <stax> <defaultcall function="main"/> <function name="main"> <function-prolog> This function is used as an example in the "Getting Started with STAX" document. It starts the TestProcess,and allows the parms,machine, java_command,java_class,processName,and classpath to be passed as arguments to the </function-prolog> <function-map-args> <function-required-arg name="parms"> The three parameters to pass to the process. </function-required-arg> <function-optional-arg name="machine" default="'local'"> The name of machine where the test process should run. </function-optional-arg> <function-optional-arg name="java_command" default="'java'"> The name of java executable that should be used to execute the test process. <function-optional-arg name="java_class" default="'com.ibm.staf.service.stax.TestProcess'"> The name of java class for the test process. <function-optional-arg name="processName" default="'My Test Process'"> The name of the process. <function-optional-arg name="classpath" default="'{STAF/Config/STAFRoot}/bin/JSTAF.jar;{STAF/Config/STAFRoot}/services/stax/STAXMon.jar'"> The CLASSPATH that should be used when the test process is started.. </function-map-args> <sequence> <process name="processName"> <location>machine</location> <command>'%s %s %s' % (java_command,parms)</command> <env>'CLASSPATH=%s' % classpath</env> <stderr mode="'stdout'"/> <returnstdout/> </process> <if expr="RC != 0"> <message>'Error: RC=%s,STAXResult)</message> <else> <message>'Process RC was 0. STAXResult=%s' % STAXResult</message> </else> </if> </sequence> </function> </stax> function-prolog:函数的说明,相当于注释 1: <?xml version="1.0" encoding="UTF-8" standalone="no"?> import可以从其他STAX xml文件中import函数。上面的例子是import 所有的函数,call元素则用来调用,script元素中用来定义变量。上述例子中调用两次。如果想并行运行job,可以使用parallel 元素。 <parallel> <call function="'main'">{ 'parms' : '40 1 0' }</call> <call function="'main'">{ 'parms' : '15 2 0' }</call> <call function="'main'">{ 'parms' : '10 2 0' }</call> </parallel> 使用循环: <loop from="1" to="3" var="index"> <block name="'Block #%s' % index"> <call function="'main'">{ 'parms' : '10 %s 0' % index }</call> </block> </loop> 在机器执行的时候可以使用loop来控制执行,其中from和to分别表示循环的起始,var表示循环迭代的变量。 在XML中还可以使用testcase元素: <testcase name="'Test Process'"> <sequence> <script>r = randint(1,100)</script> <call function="'main'">{ 'parms' : '1 1 %s' % r }</call> <if expr="STAXResult <= 50"> <tcstatus result="'pass'"/> <else> <tcstatus result="'fail'"/> </else> </if> </sequence> </testcase> 使用testcase之后可以嵌入tcstatus来确认执行的结果。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |