zoukankan      html  css  js  c++  java
  • js操作xml

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        
    <title>js操作xml</title>
        
    <script type="text/javascript">
            
    var iIndex = -1;
            
    var objectDoc = new ActiveXObject("MSXML2.DOMDocument.3.0");
            objectDoc.load(
    "root.xml");
            
    var objectItem = objectDoc.selectNodes("/Root/Item");
            
    function getNode(objectDoc, strPath);
            {
                
    var returnValue="";
                
    var strValue = objectDoc.selectNodes(strPath);
                
    if(strValue)
                returnValue 
    = strValue.Text;
                
    return returnValue;
            }
            
    function getDataNext()
            {
                iIndex
    ++;
                
    if(iIndex> objectItem.length -1
                    iIndex 
    =0;
                document.forms[
    0].ProductTag.value=getNode(objectDoc,"/root/item["+iIndex+"]/ProductTag");
                document.forms[
    0].Price.value=getNode(objectDoc,"/root/item["+iIndex+"]/Price");
                document.forms[
    0].Quantity.value = getNode(objectDoc,"/root/item["+iIndex+"]/Quantity");
                document.forms[
    0].Total.value = getNode(objectDoc,"/root/item["+iIndex+"]/Total");
            }
            
    function getDataPrev()
            {
                iIndex
    --;
                
    if(iIndex > objectItem.length -1)
                    iIndex 
    =0;
                document.forms[
    0].ProductTag.value=getNode(objectDoc,"/root/item["+iIndex+"]/ProductTag");
                document.forms[
    0].Price.value=getNode(objectDoc,"/root/item["+iIndex+"]/Price");
                document.forms[
    0].Quantity.value = getNode(objectDoc,"/root/item["+iIndex+"]/Quantity");
                document.forms[
    0].Total.value = getNode(objectDoc,"/root/item["+iIndex+"]/Total");
            }
            
            
    function init()
            {
                document.all.XMLTitle.innerText
    =getNode(objectDoc,"/root/Cigarette");
                document.all.XMLDate.innerText 
    = getNode(objectDoc,"/root/Date");
            }
        
    </script>
    </head>
    <body onload="init();GetDataNext()">
        
    <form>
            
    <div id="XMLTitle"></div>
            
    <br />
            
    <table border="0">
                
    <tr><td>产品名称</td><td><input type="text" name="ProductTag" /></td></tr>
                
    <tr><td>产品价格</td><td><input type="text" name="Price" /></td></tr>
                
    <tr><td>产品数量</td><td><input type="text" name="Quantity" /></td></tr>
                
    <tr><td>金额合计</td><td><input type="text" name="Total" /></td></tr>
            
    </table>
            
    <br />
            
    <div id="XMLDate"></div>
            
    <br />
            
    <input type="button" value="<<" onclick="getDataPrev();" />
            
    <input type="button" value=">>" onclick="getDataNext();" />
        
    </form>
    </body>
    </html>

    节点对象属性

    childNodes—返回节点到子节点的节点列表

    firstChild—返回节点的首个子节点。

    lastChild—返回节点的最后一个子节点。

    nextSibling—返回节点之后紧跟的同级节点。

    nodeName—返回节点的名字,根据其类型。

    nodeType—返回节点的类型。

    nodeValue—设置或返回节点的值,根据其类型。

    ownerDocument—返回节点的根元素(document对象)。

    parentNode—返回节点的父节点。

    previousSibling—返回节点之前紧跟的同级节点。

    text—返回节点及其后代的文本(IE独有)。

    xml—返回节点及其后代的XML(IE独有)。


    节点对象的方法

    appendChild()—向节点的子节点列表的结尾添加新的子节点。

    cloneNode()—复制节点。

    hasChildNodes()—判断当前节点是否拥有子节点。

    insertBefore()—在指定的子节点前插入新的子节点。

    normalize()—合并相邻的Text节点并删除空的Text节点。

    removeChild()—删除(并返回)当前节点的指定子节点。

    replaceChild()—用新节点替换一个子节点。


    IE6独有方法

    selectNodes()—用一个XPath表达式查询选择节点。

    selectSingleNode()—查找和XPath查询匹配的一个节点。

    transformNode()—使用XSLT把一个节点转换为一个字符串。transformNodeToObject()—使用XSLT把一个节点转换成为一个文档。


    原文网址:http://www.ykjm.net/article.asp?id=244 

  • 相关阅读:
    IBatis学习总结之动态拼sql
    IBatis学习总结
    帝都残暴的.net 之旅 (Martin Fowler 有留言哦)
    简洁实用的WordPress模板
    发现一位同行特牛
    app爬虫--mitmproxy用法梳理
    [转]数据库设计中的14个技巧
    [转] sql数据类型 varchar与nvarchar的区别
    web工作原理
    Xcode6 LaunchImage尺寸
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/1717271.html
Copyright © 2011-2022 走看看