Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml tcl dom以列表形式获取属性 tc1_arg1 tc2_arg2 tc2_arg1 tc2_arg2_Xml_Xpath_Tcl_Tdom - Fatal编程技术网

Xml tcl dom以列表形式获取属性 tc1_arg1 tc2_arg2 tc2_arg1 tc2_arg2

Xml tcl dom以列表形式获取属性 tc1_arg1 tc2_arg2 tc2_arg1 tc2_arg2,xml,xpath,tcl,tdom,Xml,Xpath,Tcl,Tdom,上面是我的xml,我想使用tdom首先读取所有“tc_name”属性作为tcl列表。然后使用列表中的每个tc_名称查找相应的参数。请告诉我怎么做,谢谢 你的标题是tcl dom,但你的问题是tdom,它被标记为tdom,所以我将以多数票决定并给出一个tdom答案-它们是不同的包 首先,您需要使用 <Testcases> <Testcase tc_name="tc1"> <Parameters> <Input_Arguments>tc1_

上面是我的xml,我想使用tdom首先读取所有“tc_name”属性作为tcl列表。然后使用列表中的每个tc_名称查找相应的参数。请告诉我怎么做,谢谢

你的标题是tcl dom,但你的问题是tdom,它被标记为tdom,所以我将以多数票决定并给出一个tdom答案-它们是不同的包

首先,您需要使用

<Testcases>
<Testcase tc_name="tc1">
  <Parameters>
    <Input_Arguments>tc1_arg1</Input_Arguments>                 
    <Input_Arguments>tc2_arg2</Input_Arguments>
  </Parameters>  
</Testcase>
<Testcase tc_name="tc2">
  <Parameters>
    <Input_Arguments>tc2_arg1</Input_Arguments>
    <Input_Arguments>tc2_arg2</Input_Arguments>
  </Parameters>  
</Testcase>
</Testcases>
现在获取tc_名称节点的列表

# Assumes that xmlText holds your raw XML
dom parse $xmlText doc
现在,围绕这些节点循环访问参数节点

set nodes [$doc selectNodes {Testcases/Testcase[@tc_name]}
完成文档后别忘了把它扔掉。(请注意,如果您在proc中执行此操作,则当proc退出时,将自动执行此操作。)

我不完全确定您最后想要做什么,所以这里有一些代码可以输出每个参数,每个参数的tc_名称,每行一个:

$doc delete
dom解析{
tc1_arg1
tc2_arg2
tc2_arg1
tc2_arg2
}医生
foreach测试用例[$doc selectNodes测试用例/testcase]{
集合名称[$testcase getAttribute tc_name]
foreach参数[$testcase selectNodes参数/输入参数]{
foreach子节点[$arg childNodes]{
将stdout$name\[$child nodeValue]
}
}
}
$doc delete;#整理已解析的文档

我希望这能为您提供足够的指示,使您能够完成任务

这里有一个使用XPath的替代解决方案

dom parse {<Testcases>
             <Testcase tc_name="tc1">
               <Parameters>
                 <Input_Arguments>tc1_arg1</Input_Arguments>                 
                 <Input_Arguments>tc2_arg2</Input_Arguments>
               </Parameters>  
             </Testcase>
             <Testcase tc_name="tc2">
               <Parameters>
                 <Input_Arguments>tc2_arg1</Input_Arguments>
                 <Input_Arguments>tc2_arg2</Input_Arguments>
               </Parameters>  
             </Testcase>
           </Testcases>
        } doc
foreach testcase [$doc selectNodes Testcases/Testcase] {
    set name [$testcase getAttribute tc_name]
    foreach arg [$testcase selectNodes Parameters/Input_Arguments] {
        foreach child [$arg childNodes] {
            puts stdout $name\ [$child nodeValue]
        }
    }
}
$doc delete;          # Tidy away the parsed document
包需要时差
proc getTestCases{xmlText}{
#获取具有属性tc_name的节点列表
set doc[dom parse$xmlText]
设置tcNodes[$doc selectNodes/Testcase/Testcase\[@tc_name\]]
#对于每个节点,获取tc_name属性,然后获取
#争论。
设置案例[dict create]
foreach tcNode$tcNodes{
设置tcName[$tcNode@tc_name]
设置参数{}
foreach argNode[$tcNode selectNodes参数/输入参数]{
lappend参数[$argNode text]
}
dict append cases$tcName$参数
}
$doc删除
退回$cases
}
#主要
#从文件中检索XML文本
set f[打开testcases.xml]
设置xmlText[读取$f]
收盘价$f
#检索测试用例并显示它们
设置测试用例[getTestCases$xmlText]
{tcName argList}$testCases的dict{
放入“$tcName:$argList”
}
讨论
  • 函数
    getTestCases
    首先将XML解析为DOM文档
  • 然后调用
    selectNodes
    和XPath表达式来检索\节点列表
  • 对于每个节点,它检索tc_name属性以及参数
  • 返回值是一个
    dict
    对象。有关dict的更多信息,请查阅Tcl手册

尝试
[$testcase@tc\u name]
获取属性的快捷方式。
dom parse {<Testcases>
             <Testcase tc_name="tc1">
               <Parameters>
                 <Input_Arguments>tc1_arg1</Input_Arguments>                 
                 <Input_Arguments>tc2_arg2</Input_Arguments>
               </Parameters>  
             </Testcase>
             <Testcase tc_name="tc2">
               <Parameters>
                 <Input_Arguments>tc2_arg1</Input_Arguments>
                 <Input_Arguments>tc2_arg2</Input_Arguments>
               </Parameters>  
             </Testcase>
           </Testcases>
        } doc
foreach testcase [$doc selectNodes Testcases/Testcase] {
    set name [$testcase getAttribute tc_name]
    foreach arg [$testcase selectNodes Parameters/Input_Arguments] {
        foreach child [$arg childNodes] {
            puts stdout $name\ [$child nodeValue]
        }
    }
}
$doc delete;          # Tidy away the parsed document
package require tdom

proc getTestCases {xmlText} {
    # Get a list of <Testcase> nodes that have attribute tc_name
    set doc [dom parse $xmlText]
    set tcNodes [$doc selectNodes /Testcases/Testcase\[@tc_name\]]

    # For each <Testcase> node, get the tc_name attribute, then get the
    # arguments.
    set cases [dict create]
    foreach tcNode $tcNodes {
        set tcName [$tcNode @tc_name]
        set arguments {}
        foreach argNode [$tcNode selectNodes Parameters/Input_Arguments] {
            lappend arguments [$argNode text]
        }
        dict append cases $tcName $arguments
    }

    $doc delete
    return $cases
}

# Main

# Retrieve the XML text from a file
set f [open testcases.xml]
set xmlText [read $f]
close $f

# Retrieve the test cases and display them
set testCases [getTestCases $xmlText]
dict for {tcName argList} $testCases {
    puts "$tcName: $argList"
}