使用XML存储的用户个性化配置信息

前端之家收集整理的这篇文章主要介绍了使用XML存储的用户个性化配置信息前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近遇到了一个场景:

1、一个客户端软件,需要对其中用户的个性化偏好进行记录,但是同一个计算机可能会有多人使用这个软件,因此软件需要记录每个人的偏好

2、如果用户第一次登录,则需要使用默认的偏好

因此我设计了下面这个XML文件(PersonalizedConfig.xml),用来保存偏好:

  1. <?xml version="1.0" encoding="gb2312"?>
  2. <?xml-stylesheet type='text/xsl' href='PersonalizedConfig.xslt'?>
  3. <!DOCTYPE UserConfig PUBLIC "-//Tsybius//UserConfig//EN" "PersonalizedConfig.dtd">
  4. <UserConfig Title="用户个性化配置参数表">
  5. <User MemCode="10000001" OperId="10006">
  6. <Config Key="Config1" Value="testConfigValue1" />
  7. <Config Key="Config2" Value="testConfigValue2" />
  8. <Config Key="Config3" Value="testConfigValue3" />
  9. <Config Key="Config4" Value="testConfigValue4" />
  10. <Config Key="Config5" Value="testConfigValue5" />
  11. </User>
  12. <User MemCode="10000001" OperId="10008">
  13. <Config Key="Config1" Value="testConfigValueA" />
  14. <Config Key="Config2" Value="testConfigValueB" />
  15. <Config Key="Config3" Value="testConfigValueC" />
  16. <Config Key="Config4" Value="testConfigValueD" />
  17. <Config Key="Config5" Value="testConfigValueE" />
  18. </User>
  19. </UserConfig>

其中每个User元素是一个用户,MemCode和OperId属性用作该用户的唯一标识(即说明该用户是哪个机构下的哪个操作员)。每个用户下面,都有多个Config元素,其中包含了Key和Value两个属性,每一个Config元素说明该用户一个配置项的设置情况。

相关文档类型定义文件(PersonalizedConfig.dtd)代码如下:

  1. <!--用户个性化配置文件DTD校验-->
  2. <!ELEMENT UserConfig (User)*>
  3. <!ATTLIST UserConfig Title CDATA #IMPLIED>
  4. <!ELEMENT User (Config)*>
  5. <!ATTLIST User MemCode CDATA #IMPLIED>
  6. <!ATTLIST User OperId CDATA #IMPLIED>
  7. <!ELEMENT Config EMPTY>
  8. <!ATTLIST Config Key CDATA #IMPLIED>
  9. <!ATTLIST Config Value CDATA #IMPLIED>

相关样式文件(PersonalizedConfig.xslt)代码如下:

  1. <?xml version="1.0" encoding="gb2312"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <!--用户个性化配置文件XSLT样式表-->
  4. <xsl:output method="html"/>
  5. <xsl:template match="/">
  6. <html>
  7. <head>
  8. <Meta content="zh-cn" http-equiv="Content-Language"/>
  9. <Meta content="text/html; charset=utf-16" http-equiv="Content-Type"/>
  10. <link type="text/css" rel="stylesheet" href="PersonalizedConfig.css"/>
  11. <title><xsl:value-of select="UserConfig/@Title" /></title>
  12. <script type="text/javascript" language="javascript">
  13. <xsl:text disable-output-escaping="yes">
  14. <![CDATA[
  15. //自动刷新
  16. function myrefresh()
  17. {
  18. window.location.reload();
  19. }
  20. setTimeout('myrefresh()',3000); //在这里指定刷新间隔,单位毫秒
  21. ]]>
  22. </xsl:text>
  23. </script>
  24. </head>
  25. <body>
  26. <div id="content">
  27. <br />
  28. <strong><font color="red"><xsl:value-of select="UserConfig/@Title" /></font></strong><br />
  29. <hr />
  30. <table>
  31. <tr>
  32. <th>用户编号</th>
  33. <th>操作员代码</th>
  34. <th>配置项1</th>
  35. <th>配置项2</th>
  36. <th>配置项3</th>
  37. <th>配置项4</th>
  38. <th>配置项5</th>
  39. </tr>
  40. <xsl:for-each select="UserConfig/User">
  41. <tr>
  42. <td class="textCentered"><xsl:value-of select="@MemCode" /></td>
  43. <td class="textCentered"><xsl:value-of select="@OperId" /></td>
  44. <xsl:for-each select="Config">
  45. <td class="textCentered"><xsl:value-of select="@Value"/></td>
  46. </xsl:for-each>
  47. </tr>
  48. </xsl:for-each>
  49. </table>
  50. </div>
  51. </body>
  52. </html>
  53. </xsl:template>
  54.  
  55. </xsl:stylesheet>

注意在这个XSLT文件中,包含了自动刷新相关的代码(3秒刷一次),这样在用浏览器打开XML并的时候,就可以看到XML文档的最新数据变更情况了。

XSLT文件使用到的CSS样式表文件(PersonalizedConfig.css)代码如下:

  1. /* Body style,for the entire document */
  2. body
  3. {
  4. background: #F3F3F4;
  5. color: #1E1E1F;
  6. font-family: "Segoe UI",Tahoma,Geneva,Verdana,sans-serif;
  7. padding: 0;
  8. margin: 0;
  9. }
  10. /* Header1 style,used for the main title */
  11. h1
  12. {
  13. padding: 10px 0px 10px 10px;
  14. font-size: 21pt;
  15. background-color: #E2E2E2;
  16. border-bottom: 1px #C1C1C2 solid;
  17. color: #201F20;
  18. margin: 0;
  19. font-weight: normal;
  20. }
  21. /* Table styles */
  22. table
  23. {
  24. border-spacing: 0 0;
  25. border-collapse: collapse;
  26. font-size: 10pt;
  27. }
  28. table th
  29. {
  30. background: #3399FF;
  31. text-align: center;
  32. text-decoration: none;
  33. font-weight: normal;
  34. padding: 3px 6px 3px 6px;
  35. width:200px;
  36. }
  37. table td
  38. {
  39. vertical-align: top;
  40. text-align: center;
  41. padding: 3px 6px 3px 6px;
  42. margin: 0px;
  43. border: 1px solid #3399FF;
  44. /*background: #66FF33;*/
  45. }
  46. .textCentered
  47. {
  48. text-align: center;
  49. }
  50. #content
  51. {
  52. padding: 0px 12px 40px 40px;
  53. }

在IE8.0中打开上面的XML文档,展示效果如下:

END

猜你在找的XML相关文章