XML解析

一、xsl+xml实现分页
1. 代码:

message.xml
 1 <?xml version="1.0" encoding="utf-8" ?> 
 2 <?xml-stylesheet type="text/xsl" href="MessageXML.xsl" ?>
 3 <MessageS>
 4 <Message>
 5     <UserIdSend>000</UserIdSend>
 6     <UserNameSend>张三</UserNameSend>
 7     <UserTypeSend>0</UserTypeSend>
 8     <UserId>45600</UserId>
 9     <UserName>李四</UserName>
10     <UserType>6</UserType>
11     <MessageContent>hello</MessageContent>
12     <MessageTime>2012-9-20 12:50:16</MessageTime>
13 </Message>
14 <Message>
15     <UserIdSend>000</UserIdSend>
16     <UserNameSend>李四</UserNameSend>
17     <UserTypeSend>6</UserTypeSend>
18     <UserId>45600</UserId>
19     <UserName>张三</UserName>
20     <UserType>0</UserType>
21     <MessageContent>hi...</MessageContent>
22     <MessageTime>2012-9-20 13:02:00</MessageTime>
23 </Message>
24 <Message>
25     <UserIdSend>000</UserIdSend>
26     <UserNameSend>张三</UserNameSend>
27     <UserTypeSend>0</UserTypeSend>
28     <UserId>45600</UserId>
29     <UserName>李四</UserName>
30     <UserType>6</UserType>
31     <MessageContent>你在哪里</MessageContent>
32     <MessageTime>2012-9-20 13:10:00</MessageTime>
33 </Message>
34 <Message>
35     <UserIdSend>000</UserIdSend>
36     <UserNameSend>李四</UserNameSend>
37     <UserTypeSend>6</UserTypeSend>
38     <UserId>45600</UserId>
39     <UserName>张三</UserName>
40     <UserType>0</UserType>
41     <MessageContent>扬州</MessageContent>
42     <MessageTime>2012-9-20 13:20:00</MessageTime>
43 </Message> 
44 </MessageS>
MessageXML.xsl
  1 <?xml version="1.0" encoding="utf-8" ?> 
  2 <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
  3 <xsl:template match="/">
  4 <html>
  5 <head> 
  6 <style>body,MessageS,Message,UserNameSend,MessageTime,
  7           MessageContent{ font: 12px "宋体", "Arial", "Times New Roman"; } table { font-size: 12px; border: 0px double; border-color: #99CC99 #99CC99 #CCCCCC #CCCCCC; cellpadding:3;cellspacing:3; bgcolor:#eeeeee; text-decoration: blink} span { font-size: 12px; color: red; } .keybutton { cursor:hand; font-size: 12px; color: #003300; background: #ffffff; border: 0px solid;}</style> 
  8 <script>
  9 function taxis(x) //……………………排序
 10 { 
 11 var temp = 0;
 12 stylesheet=document.XSLDocument; 
 13 source=document.XMLDocument; 
 14 temp=parseInt(document.forms[0].text5.value);
 15 temp++;
 16 if(temp%2==0){
 17 x="-"+x;
 18 }else{
 19 x=x;
 20 } 
 21 document.forms[0].text5.value = temp;
 22 sortField=document.XSLDocument.selectSingleNode("//@order-by");
 23 sortField.value=x; 
 24 Layer1.innerHTML=source.documentElement.transformNode(stylesheet); 
 25 }
 26 </script><script>
 27 <xsl:comment>
 28 //…………………………分页 
 29 <![CDATA[ 
 30 var OnePageNum=10; //每页显示五条
 31 var PageNum=1; //当前页码
 32 var XMLPageNum=1; //xml文件总的页码
 33 function pages(Num) 
 34 { 
 35 stylesheet=document.XSLDocument; //获取值xml的所有值?
 36 source=document.XMLDocument; //获取值xml的所有值?
 37 nodes=source.documentElement.childNodes;//获取所有子结点集?
 38 len=nodes.length; //计算结点的个数,并把值赋给len
 39 for(i=1;i<=(len/OnePageNum);i++); //计算出共有几页
 40 if(len%OnePageNum!=0){
 41    XMLPageNum=i; //计算出总的页码并把值传给 XMLPageNum
 42 }else{
 43    XMLPageNum=i-1;
 44 }
 45 var firstNum=0; //每页显示的第一条,给初值为0
 46 var lastNume=0; //每页显示的最后一条,给初值为0
 47 if (Num=="first") {PageNum=1;} //如果传入的是第一页,则执行这个语句,赋给当前页码的值为1
 48 if (Num=="previous") {if (PageNum>1) PageNum -=1;} //判断
 49 if (Num=="next") {
 50    if (PageNum<XMLPageNum) {
 51     PageNum +=1;
 52    }
 53 }   //判断
 54 if (Num=="last") {PageNum =XMLPageNum;}   //判断
 55 if (Num!="last"&&Num!="next"&&Num!="previous"&&Num!="first"){
 56    var temp=parseInt(document.forms[0].text4.value);
 57    if(temp<=XMLPageNum&&temp>=1&&temp==document.forms[0].text4.value){
 58     PageNum=temp;
 59    }else{
 60     alert("你查询的页面大于总页码!");
 61     document.forms[0].text4.value="";
 62     document.forms[0].text4.focus();
 63    
 64    }
 65 }sortField=document.XSLDocument.selectSingleNode("//@expr");
 66 firstNum=OnePageNum*(PageNum-1)+1; //显示的第一条的位置
 67 lastNum=OnePageNum*(PageNum-1)+OnePageNum; //显示的最后一条的位置
 68 text="childnumber(this)>="+firstNum+" & childnumber(this)<="+lastNum;// 
 69 sortField.value=text; //
 70 Layer1.innerHTML=source.documentElement.transformNode(stylesheet); 
 71 document.forms[0].text1.value=XMLPageNum;
 72 document.forms[0].text2.value=len;
 73 document.forms[0].text3.value=PageNum;
 74 document.forms[0].text4.value="";
 75 } 
 76 ]]> 
 77 </xsl:comment>
 78 </script>
 79 </head>
 80 <body onLoad="pages('first');">
 81 <form>
 82 <input type="hidden" name="text5" value="2010-2-1 16:57:00" /> 
 83 <table align="center" width="600">
 84 <tr><td><button id="cmdfirstPage" class="keybutton" οnclick="pages('first');">首页</button> 
 85          <button id="cmdpreviousPage" class="keybutton" οnclick="pages('previous');">上一页</button> 
 86          <button id="cmdnextPage" class="keybutton" οnclick="pages('next');">下一页</button> 
 87          <button id="cmdlastPage" class="keybutton" οnclick="pages('last');">尾页</button>   
 88          <input type="text" id="text3" readOnly="true" size="1" maxLength="1" />/
 89          <input type="text" id="text1" readOnly="true" size="1" maxLength="1" /> 90      总计<input type="text" id="text2" readOnly="true" size="1" maxLength="1" />条记录 
 91    跳转页<input type="text" id="text4" size="3" maxLength="3" /> 
 92          <button id="" class="keybutton" οnclick="pages('2');">提交</button>   
 93      </td></tr>
 94 </table> 
 95 </form>
 96 <div id="Layer1" name="Layer1">
 97 <xsl:apply-templates select="MessageS" /> 
 98 </div>
 99 </body>
100 </html>
101 </xsl:template>
102 <xsl:template match="MessageS">
103 <table width="700" border="1" align="center" cellpadding="1" cellspacing="1" bordercolordark="#ffffff" bordercolorlight="#ADAAAD">
104 <tr bgcolor="#FFCC99" align="center">
105 <td style="cursor:s-resize" width="8%"   onClick="taxis('UserNameSend')">发信人</td> 
106 <td style="cursor:s-resize" width="18%" onClick="taxis('MessageTime')">时间</td> 
107 <td style="cursor:s-resize" width="74%" >信息内容</td> 
108 </tr>
109 <xsl:apply-templates select="Message" order-by="MessageTime" /> 
110 </table>
111 </xsl:template>
112 <xsl:template match="Message">
113 <xsl:if expr="childnumber(this)>=1">
114 <tr align="center">
115 <xsl:apply-templates select="UserNameSend" /> 
116 <xsl:apply-templates select="MessageTime" /> 
117 <xsl:apply-templates select="MessageContent" />   
118 </tr>
119 </xsl:if>
120 </xsl:template>
121 <xsl:template match="UserNameSend">
122 <td bgcolor="#eeeeee">
123 <xsl:value-of /> 
124 </td>
125 </xsl:template>
126 <xsl:template match="MessageTime">
127 <td>
128 <xsl:value-of /> 
129 </td>
130 </xsl:template>
131 <xsl:template match="MessageContent">
132 <td align="left" >
133 <xsl:value-of /> 
134 </td>
135 </xsl:template>
136 </xsl:stylesheet>

2. 讲解:

  • message.xml 是数据文件;
  • MessageXML.xsl 是格式文件。  

3. XML例子文件的使用方法 

  • 将两个文件按照文件名分别保存;
  • 用浏览器浏览XML文件即可。

4. 效果

二、创建xml文件

代码:

实例
 1   /// <summary>
 2         /// 创建XML文件
 3         /// </summary>    
 4         /// <param name="strFileName">XML文件名(groupName_NOTICES.xml/groupName_TASKLIST.xml)</param>
 5         /// <param name="strFileDirectory">XML路径:若为空程序默认路径</param>
 6         /// <param name="intDistinct">0:公告,1:任务列表</param>
 7         /// <returns>创建成功返回1 ,否则0</returns>
 8         public int CreateXMLFile(string strFileName, string strFileDirectory, int intDistinct)
 9         {
10             int retValue = 0;
11 
12             if (strFileDirectory != null && strFileDirectory.Length > 0)
13             {
14                 CheckDirectoryExist(strFileDirectory);
15             }
16             else
17             {
18                 CheckDirectoryExist(strDefaultPath);
19                 strFileDirectory = strDefaultPath;
20             }
21 
22             if (!File.Exists(strFileDirectory + "\\" + strFileName))
23             {
24                 try
25                 {
26                     XmlDocument xmlDocument = new XmlDocument();
27                     //加入XML的段落版本声明
28                     XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", "yes");
29                     xmlDocument.AppendChild(xmlDeclaration);
30 
31                     //创建处理指令
32                     string strProcessing = string.Empty;
33                     XmlProcessingInstruction xmlProcessingInstruction = null;
34 
35                     //创建根元素
36                     XmlElement xmlElementRoot = null;
37 
38                     if (intDistinct == 0)
39                     {
40                         strProcessing = "type='text/xsl' href='Document_Notice.xsl'";
41                         xmlProcessingInstruction = xmlDocument.CreateProcessingInstruction("xml-stylesheet", strProcessing);
42 
43                         xmlElementRoot = xmlDocument.CreateElement("NOTICES");
44                     }
45                     else
46                     {
47                         strProcessing = "type='text/xsl' href='Document_TaskList.xsl'";
48                         xmlProcessingInstruction = xmlDocument.CreateProcessingInstruction("xml-stylesheet", strProcessing);
49 
50                         xmlElementRoot = xmlDocument.CreateElement("TASKLIST");
51                     }
52 
53                     xmlDocument.AppendChild(xmlProcessingInstruction);
54                     xmlDocument.AppendChild(xmlElementRoot);
55                     xmlDocument.Save(strFileDirectory + "\\" + strFileName);
56 
57                     retValue = 1;
58                 }
59                 catch (XmlException xmlEx)
60                 {
61                     LogErrorWrite log = new LogErrorWrite();
62                     log.logWrite("CreateXMLFile :" + xmlEx.ToString(), 1);
63                 }
64             }
65 
66             return retValue;
67         }

测试效果:

转载于:https://www.cnblogs.com/YZDONET/archive/2012/09/20/2695087.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值