tDOM 未解析 XML
tDOM not parsing the XML
我有这个 XML
来自命令行输出的示例模板,
<config xmlns="http://tail-f.com/ns/config/1.0">
<random xmlns="http://random.com/ns/random/config">
<junk-id>1</junk-id>
<junk-ip-address>1.2.2.3</junk-ip-address>
<junk-state>success</junk-state>
<junk-rcvd>158558</junk-rcvd>
<junk-sent>158520</junk-sent>
<foobar>
<id1>1</id1>
<id2>1</id2>
</foobar>
</random>
</config>
我需要从这个 XML
中提取 junk-state
的值。
我用一个变量制作了一个 .tcl
脚本 运行 并使用单引号 just 用于测试目的,如下所示,
以下是我的脚本内容。我只是尝试绕过节点,但没有成功。
set XML "<config xmlns='http://tail-f.com/ns/config/1.0'>
<random xmlns='http://random.com/ns/random/config'>
<junk-id>1</junk-id>
<junk-ip-address>1.2.2.3</junk-ip-address>
<junk-state>success</junk-state>
<junk-rcvd>158558</junk-rcvd>
<junk-sent>158520</junk-sent>
<foobar>
<id1>1</id1>
<id2>1</id2>
</foobar>
</random>
</config>"
set doc [dom parse $XML]
set root [$doc documentElement]
set mynode [$root selectNodes "/config/random" ]
foreach node $mynode{
set temp1 [$node text]
echo "temp1 - $temp1"
}
以上脚本没有输出,
还尝试了如下所示的直接 xpath
表达式并打印文本
set node [$root selectNodes /config/random/junk-state/text()]
puts [$node nodeValue]
puts [$node data
这会产生错误
invalid command name ""
while executing
"$node nodeValue"
invoked from within
"puts [$node nodeValue]"
(file "temp.tcl" line 41)
我在这里做错了什么。想知道 use/modify 我的 xpath
表达方式,因为我觉得那样更整洁。
$ tclsh
% puts $tcl_version
8.5
% package require tdom
0.8.3
问题是由于 XML 命名空间(config
和 random
元素中的 xmlns
属性)引起的。您必须使用 selectNodes
操作的 -namespace
选项:
package require tdom
set XML {<config xmlns="http://tail-f.com/ns/config/1.0">
<random xmlns="http://random.com/ns/random/config">
<junk-id>1</junk-id>
<junk-ip-address>1.2.2.3</junk-ip-address>
<junk-state>success</junk-state>
<junk-rcvd>158558</junk-rcvd>
<junk-sent>158520</junk-sent>
<foobar>
<id1>1</id1>
<id2>1</id2>
</foobar>
</random>
</config>}
set doc [dom parse $XML]
set root [$doc documentElement]
set node [$root selectNodes -namespace {x http://random.com/ns/random/config} x:random/x:junk-state ]
puts [$node text]
EDIT:如果您希望 <random>
元素的命名空间自动从 XML 中检索,您可以按如下方式进行(假设<random>
是根元素的唯一子元素):
set doc [dom parse $XML]
set root [$doc documentElement]
set random [$root childNode]
set ns [$random namespace]
set node [$random selectNodes -namespace [list x $ns] x:junk-state]
puts [$node text]
我有这个 XML
来自命令行输出的示例模板,
<config xmlns="http://tail-f.com/ns/config/1.0">
<random xmlns="http://random.com/ns/random/config">
<junk-id>1</junk-id>
<junk-ip-address>1.2.2.3</junk-ip-address>
<junk-state>success</junk-state>
<junk-rcvd>158558</junk-rcvd>
<junk-sent>158520</junk-sent>
<foobar>
<id1>1</id1>
<id2>1</id2>
</foobar>
</random>
</config>
我需要从这个 XML
中提取 junk-state
的值。
我用一个变量制作了一个 .tcl
脚本 运行 并使用单引号 just 用于测试目的,如下所示,
以下是我的脚本内容。我只是尝试绕过节点,但没有成功。
set XML "<config xmlns='http://tail-f.com/ns/config/1.0'>
<random xmlns='http://random.com/ns/random/config'>
<junk-id>1</junk-id>
<junk-ip-address>1.2.2.3</junk-ip-address>
<junk-state>success</junk-state>
<junk-rcvd>158558</junk-rcvd>
<junk-sent>158520</junk-sent>
<foobar>
<id1>1</id1>
<id2>1</id2>
</foobar>
</random>
</config>"
set doc [dom parse $XML]
set root [$doc documentElement]
set mynode [$root selectNodes "/config/random" ]
foreach node $mynode{
set temp1 [$node text]
echo "temp1 - $temp1"
}
以上脚本没有输出,
还尝试了如下所示的直接 xpath
表达式并打印文本
set node [$root selectNodes /config/random/junk-state/text()]
puts [$node nodeValue]
puts [$node data
这会产生错误
invalid command name ""
while executing
"$node nodeValue"
invoked from within
"puts [$node nodeValue]"
(file "temp.tcl" line 41)
我在这里做错了什么。想知道 use/modify 我的 xpath
表达方式,因为我觉得那样更整洁。
$ tclsh
% puts $tcl_version
8.5
% package require tdom
0.8.3
问题是由于 XML 命名空间(config
和 random
元素中的 xmlns
属性)引起的。您必须使用 selectNodes
操作的 -namespace
选项:
package require tdom
set XML {<config xmlns="http://tail-f.com/ns/config/1.0">
<random xmlns="http://random.com/ns/random/config">
<junk-id>1</junk-id>
<junk-ip-address>1.2.2.3</junk-ip-address>
<junk-state>success</junk-state>
<junk-rcvd>158558</junk-rcvd>
<junk-sent>158520</junk-sent>
<foobar>
<id1>1</id1>
<id2>1</id2>
</foobar>
</random>
</config>}
set doc [dom parse $XML]
set root [$doc documentElement]
set node [$root selectNodes -namespace {x http://random.com/ns/random/config} x:random/x:junk-state ]
puts [$node text]
EDIT:如果您希望 <random>
元素的命名空间自动从 XML 中检索,您可以按如下方式进行(假设<random>
是根元素的唯一子元素):
set doc [dom parse $XML]
set root [$doc documentElement]
set random [$root childNode]
set ns [$random namespace]
set node [$random selectNodes -namespace [list x $ns] x:junk-state]
puts [$node text]