设为首页
加入收藏
联系站长
您现在的位置: 网贝 >> 建站学院 >> 网页设计 >> XML >> 文章正文 用户登录 新用户注册
JS访问XML的简单例子         ★★★★
JS访问XML的简单例子
相关网站:
作者:数字金刚 文章来源:CSDNBlog|http://blog.csdn.net/gjd111686 点击数: 更新时间:2004-9-23

JS_XML.htm

<html>
<head>
<script>
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.selectSingleNode(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+"]/PricePer");
 document.forms[0].Quantity.value=getNode(objectDoc,"/Root/Item["+iIndex+"]/Quantity");
 document.forms[0].Total.value=getNode(objectDoc,"/Root/Item["+iIndex+"]/Subtotal");
}
function getDataPrev()
{
 iIndex--;
 if(iIndex<0)iIndex=objectItem.length-1;
 document.forms[0].ProductTag.value=getNode(objectDoc,"/Root/Item["+iIndex+"]/ProductTag");
 document.forms[0].Price.value=getNode(objectDoc,"/Root/Item["+iIndex+"]/PricePer");
 document.forms[0].Quantity.value=getNode(objectDoc,"/Root/Item["+iIndex+"]/Quantity");
 document.forms[0].Total.value=getNode(objectDoc,"/Root/Item["+iIndex+"]/Subtotal");
}
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>
Root.xml

<?xml version="1.0" encoding="gb2312"?>
<Root>
  <Cigarette>实时价格一览表</Cigarette>
  <Item id="1">
    <ProductTag>石林(软包)</ProductTag>
    <PricePer>3.50</PricePer>
    <Quantity>100</Quantity>
    <Subtotal>350.00</Subtotal>
    <Description>NULL</Description>
  </Item>
  <Item id="2">
    <ProductTag>桂花(白、软包)</ProductTag>
    <PricePer>2.50</PricePer>
    <Quantity>10</Quantity>
    <Subtotal>25.00</Subtotal>
    <Description>NULL</Description>
  </Item>
  <Item id="3">
    <ProductTag>一品黄山(硬包)</ProductTag>
    <PricePer>5.50</PricePer>
    <Quantity>1000</Quantity>
    <Subtotal>5500.00</Subtotal>
    <Description>NULL</Description>
  </Item>
  <Item id="4">
    <ProductTag>红山茶(硬包)</ProductTag>
    <PricePer>3.50</PricePer>
    <Quantity>1</Quantity>
    <Subtotal>3.50</Subtotal>
    <Description>NULL</Description>
  </Item>
  <Date>08/17/2004</Date>
</Root>

文章录入:admin    责任编辑:admin 
  • 上一篇文章:

  • 下一篇文章:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
    相关网站:

    文章 下载 图片
    推荐文章XML与数据库
    普通文章JS访问XML的简单例子
    普通文章Web上JS利用XMLHTTP
    普通文章将HTML表单数据存储
    普通文章[组图]浅析.NET下XM
    普通文章XML Schema帮你建模
    普通文章微软Office2003十月
    普通文章一个简单的XML Sche
    推荐文章XML与数据库
    推荐文章亲密接触xml
    推荐文章[组图]XML简明教程
    推荐文章[组图]用XSLT生成网
    普通文章把手教你写私服列表
    普通文章WEB标准
    普通文章GOOGLE sitemap官方
    普通文章用php定制404错误页
    普通文章WAP(wml)开发问答
    普通文章[组图]PS教程系列:快
    普通文章时使用apache和IIS,
    普通文章[组图]2005年的第一
    推荐文章[组图]彻底掌握IIS6
    推荐文章[组图]CSS 全攻略
    推荐文章各种脚本错误详解!
    推荐文章直接生成XML的Googl
    推荐文章九个常见的错误原因
    推荐文章Win XP家用版也能装
    推荐文章JSP入门初级教程之J
    推荐文章ASP入门及提高
    没有相关文章

    浅析.NET下XML数据访

    在.NET Framework中

    用XML创建可排序、分

    应该使用SAX还是DOM
    (只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    Copyright © 2003-2005 NetBei.com All rights reserved.