添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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.

Thank you for your help, this is exactly what I needed. You are correct, I will probably be best to use each attribute directly from tDOM. My next adventure is building each parsed object into database object. Fun :-) – pws5068 Apr 10, 2010 at 16:22 The main thing is to plan out exactly what you want to achieve, which pieces should go where, etc. I find the back of an envelope a good place to do these plans. :-) – Donal Fellows Apr 10, 2010 at 19:53 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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.