我们如何使用TCL脚本运行xml文件?

时间:2016-07-27 21:09:49

标签: xml unix tcl

我在Unix服务器上有一个XML文件。如何使用XML脚本运行Tcl文件?

1 个答案:

答案 0 :(得分:2)

如果您正在寻找一种简单的解析方法(如果这就是运行时的意思)XML,请考虑使用tdom包。

参考:http://wiki.tcl.tk/8984

这是一个文件foo.xml,其中包含以下内容:

<my_root>
    <my_child1>
        <my_subchild1 foo="bar">bat</my_subchild1>
    </my_child1>
</my_root>

将xml数据读入变量(如果XML内容很大,则不是最佳方式):

%   
% set fd [open foo.xml]
file3
% set xml [read $fd]
<my_root>
    <my_child1>
        <my_subchild1 foo="bar">bat</my_subchild1>
    </my_child1>
</my_root>

% close $fd
% 

现在解析它:

% package require tdom
0.8.3
% 
% set documentHandle [dom parse $xml]
domDoc0x2510320
% set root [$documentHandle documentElement]
domNode0x2546d90
% $root asXML
<my_root>
    <my_child1>
        <my_subchild1 foo="bar">bat</my_subchild1>
    </my_child1>
</my_root>

% set child1 [$root firstChild]
domNode0x16dcec0
% $child1 asXML
<my_child1>
    <my_subchild1 foo="bar">bat</my_subchild1>
</my_child1>

% set tmp [$root selectNodes //my_subchild1]
domNode0x16dd630
% $tmp asXML
<my_subchild1 foo="bar">bat</my_subchild1>

% $tmp getAttribute foo
bar
% $tmp text
bat
% 

以下是带有解析的xml节点的受支持命令列表:

% $tmp
Usage nodeObj <method> <args>, where method can be:
    nodeType                     
    nodeName                     
    nodeValue ?newValue?         
    hasChildNodes                
    childNodes                   
    childNodesLive               
    parentNode                   
    firstChild ?nodeObjVar?      
    lastChild ?nodeObjVar?       
    nextSibling ?nodeObjVar?     
    previousSibling ?nodeObjVar? 
    hasAttribute attrName        
    getAttribute attrName ?defaultValue? 
    setAttribute attrName value ?attrName value ...? 
    removeAttribute attrName     
    hasAttributeNS uri localName 
    getAttributeNS uri localName ?defaultValue? 
    setAttributeNS uri attrName value ?attrName value ...? 
    removeAttributeNS uri attrName 
    attributes ?attrNamePattern?   
    appendChild new              
    insertBefore new ref         
    replaceChild new old         
    removeChild child            
    cloneNode ?-deep?            
    ownerDocument                
    getElementsByTagName name    
    getElementsByTagNameNS uri localname 
    getElementById id            
    find attrName attrValue ?nodeObjVar?   
    child      number|all ?type? ?attrName attrValue? 
    descendant number|all ?type? ?attrName attrValue? 
    ancestor   number|all ?type? ?attrName attrValue? 
    fsibling   number|all ?type? ?attrName attrValue? 
    psibling   number|all ?type? ?attrName attrValue? 
    root ?nodeObjVar?            
    target                       
    data                         
    text                         
    prefix                       
    namespaceURI                 
    getBaseURI                   
    baseURI ?URI?                
    localName                    
    delete                       
    getLine                      
    getColumn                    
    @<attrName> ?defaultValue?   
    asList                       
    asXML ?-indent <none,0..8>? ?-channel <channel>? ?-escapeNonASCII? ?-escapeAllQuot? ?-doctypeDeclaration <boolean>?
    asHTML ?-channel <channelId>? ?-escapeNonASCII? ?-htmlEntities?
    asText                       
    appendFromList nestedList    
    appendFromScript script      
    insertBeforeFromScript script ref 
    appendXML xmlString          
    selectNodes ?-namespaces prefixUriList? ?-cache <boolean>? xpathQuery ?typeVar? 
    toXPath                      
    disableOutputEscaping ?boolean? 
    precedes node                
    normalize ?-forXPath?        
    xslt ?-parameters parameterList? <xsltDocNode>
    readlock                     
    writelock                    

%