TCL TDom:循环对象

发布于 2024-08-28 10:35:36 字数 1144 浏览 5 评论 0原文

使用 TDom,我想循环浏览以下格式的对象列表:

    <object>
      <type>Hardware</type>
      <name>System Name</name>
      <description>Basic Description of System.</description>
      <attributes>
          <vendor>Dell</vendor>
          <contract>MM/DD/YY</contract>
          <supportExpiration>MM/DD/YY</supportExpiration>
          <location>Building 123</location>
          <serial>xxx-xxx-xxxx</serial>
          <mac>some-mac-address</mac>
      </attributes>
    </object>

    <object>
      <type>Software</type>
      <name>Second Object</name>
    ...

然后我使用 TDom 制作对象列表:

set dom [dom parse $xml]
set doc [$dom documentElement]

set nodeList [$doc selectNodes /systems/object]

到目前为止,我已经这样做了(理论上)从列表中选择每个“对象”节点。我怎样才能循环它们?难道只是:

foreach node $nodeList { 

对于每个对象,我需要检索每个属性的关联。从示例中,我需要记住“名称”是“系统名称”,“供应商”是“Dell”等。

我是 TCL 新手,但在其他语言中,我会使用对象或关联列表来存储这些。这可能吗?您能否向我展示以这种方式选择属性的语法示例?

Using TDom, I would like to cycle through a list of objects in the following format:

    <object>
      <type>Hardware</type>
      <name>System Name</name>
      <description>Basic Description of System.</description>
      <attributes>
          <vendor>Dell</vendor>
          <contract>MM/DD/YY</contract>
          <supportExpiration>MM/DD/YY</supportExpiration>
          <location>Building 123</location>
          <serial>xxx-xxx-xxxx</serial>
          <mac>some-mac-address</mac>
      </attributes>
    </object>

    <object>
      <type>Software</type>
      <name>Second Object</name>
    ...

Then I use TDom to make a list of objects:

set dom [dom parse $xml]
set doc [$dom documentElement]

set nodeList [$doc selectNodes /systems/object]

So far I've done this to (theoretically) select every "Object" node from the list. How can I loop through them? Is it just:

foreach node $nodeList { 

For each object, I need to retrieve the association of each attribute. From the example, I need to remember that the "name" is "System Name", "vendor" is "Dell", etc.

I'm new to TCL but in other languages I would use an object or an associative list to store these. Is this possible? Can you show me an example of the syntax to select an attribute in this manner?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小清晰的声音 2024-09-04 10:35:36

你确实走在正确的道路上。您可能想要这样做:

foreach node [$doc selectNodes "/systems/object"] {
    set name [[$node selectNodes "./name\[1\]"] text]
    lappend listOfNames $name
    foreach attr {vendor serial} {
        set aNodes [$node selectNodes "./attributes/$attr"]
        if {[llength $aNodes]} {
            set data($name,$attr) [[lindex $aNodes 0] text]
        }
    }
}

我使用 Tcl 的(关联)数组功能来保存提取的属性。还有其他方法也可以工作,例如,iTcl 或XOTcl 或TclOO 对象,或字典,或任意数量的其他可能性。请注意,考虑到实际使用 tDOM 是多么容易,我实际上很想保留文档本身并直接对其进行处理;不需要为了好玩而将所有内容提取到其他数据结构中。

You are indeed on the right track. You probably want to do this:

foreach node [$doc selectNodes "/systems/object"] {
    set name [[$node selectNodes "./name\[1\]"] text]
    lappend listOfNames $name
    foreach attr {vendor serial} {
        set aNodes [$node selectNodes "./attributes/$attr"]
        if {[llength $aNodes]} {
            set data($name,$attr) [[lindex $aNodes 0] text]
        }
    }
}

I'm using Tcl's (associative) array capabilities to hold the extracted attributes. There are other ways that will also work, e.g., an iTcl or XOTcl or TclOO object, or a dictionary, or any number of other possibilities. Mind you, I'd actually be tempted to keep the document itself around and process directly off that, given how easy it is to actually work with tDOM; no need to extract everything into some other data structure just for the heck of it.

画骨成沙 2024-09-04 10:35:36
set doc [$dom documentElement]
set nodeList [$doc selectNodes /systems/object]

foreach node [$nodeList childNodes] {
    set nodename [$node nodeName]
    if {$nodename eq "attributes"} {
        foreach attr_node [$node childNodes] {
            set attr_nodename [$attr_node nodeName]
            set attr_nodetext [[$attr_node selectNodes text()] nodeValue]
            puts "$attr_nodename : $attr_nodetext"
        }
    } else {
        set node_text [[$node selectNodes text()] nodeValue]
        puts "$nodename : $node_text"
    }
}

查看快速参考 https://tdom.github.io/domDoc.html

set doc [$dom documentElement]
set nodeList [$doc selectNodes /systems/object]

foreach node [$nodeList childNodes] {
    set nodename [$node nodeName]
    if {$nodename eq "attributes"} {
        foreach attr_node [$node childNodes] {
            set attr_nodename [$attr_node nodeName]
            set attr_nodetext [[$attr_node selectNodes text()] nodeValue]
            puts "$attr_nodename : $attr_nodetext"
        }
    } else {
        set node_text [[$node selectNodes text()] nodeValue]
        puts "$nodename : $node_text"
    }
}

check this out for the quick reference https://tdom.github.io/domDoc.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文