繁体   English   中英

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

[英]How can we run an xml file using TCL Script?

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

如果您正在寻找一种简单的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                    

% 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM