TCL TDom:循环对象
使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你确实走在正确的道路上。您可能想要这样做:
我使用 Tcl 的(关联)数组功能来保存提取的属性。还有其他方法也可以工作,例如,iTcl 或XOTcl 或TclOO 对象,或字典,或任意数量的其他可能性。请注意,考虑到实际使用 tDOM 是多么容易,我实际上很想保留文档本身并直接对其进行处理;不需要为了好玩而将所有内容提取到其他数据结构中。
You are indeed on the right track. You probably want to do this:
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.
查看快速参考 https://tdom.github.io/domDoc.html
check this out for the quick reference https://tdom.github.io/domDoc.html