Table of Contents
  源码:
Home Backend Development XML/RSS Tutorial xml learning (3) html display xml

xml learning (3) html display xml

Feb 23, 2017 pm 02:33 PM


有的时候我们需要在html显示xml,比如我们修改了xml,点击保存,需要在页面显示xml源码,让我们知道xml已经修改了,最好的方法是把xml放到pre元素中,但是会发现没有换行,全部显示一行,肯定很难看,所以我做了一个迭代xmlDOM的函数来格式显示xml的函数,

  迭代函数思路:

1.每个xml文件时有无数个兄弟节点组成,但是终有最后截止的一个,那么循环结束的标志就是当一个节点没有兄弟节点时,循环就结束,

那么可以循环兄弟节点,于是有循环兄弟节点函数

2每个节点可能有子节点,子节点也可能有兄弟子节点,这个时候利用循环兄弟节点函数,循环节点的第一个子节点,

效果图:

主要代码:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

private void getXMLStr(XmlDocument xmlDoc)

 {

     foreach (XmlNode node in xmlDoc.ChildNodes)

     {

 

         if (node.NodeType == XmlNodeType.Element)

         {

             getNext(node,0);

         }

         else

         {

             xml = "<p>" + node.OuterXml.Replace("<","<").Replace(">",">");

         }

 

 

     }

 }

 

 

 private void getNext(XmlNode node,int i)

 {

     if (node.NextSibling == null)//如果没有兄弟节点

     {

         if (node.HasChildNodes)

         {

             //如果有子节点

             if (node.FirstChild.NodeType != XmlNodeType.Text)

             {

                 //getXmlAttribute(node)  获取节点的所有属性

                 //如果子节点的子节点不是text类型

                 xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  ><" + node.Name +"  "+ getXmlAttribute(node) + "></p>";

                 getNext(node.FirstChild, i + 1);

                 xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  ></"+ node.Name +"></p>";

             }

             else

             {

                 //如果子节点的子节点不是text类型

                 xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";

 

             }

         }

         else 

         {

             xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";

         }

     }

     else

     {

         if (node.HasChildNodes)

         {

             if (node.FirstChild.NodeType != XmlNodeType.Text)

             {

 

                 xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  ><" + node.Name+"  " + getXmlAttribute(node) + "></p>";

                 getNext(node.FirstChild, i + 1);

                 xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  ></"+ node.Name  + "></p>";

             }

             else

             {

                 xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";

             }

         }

         else

         {

             xml = xml + "<p " + "style=&#39;margin-left:" + (i*20).ToString() + "px&#39;  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";

         }

         getNext(node.NextSibling,i);

 

     }

 

 }

 

 

 private string  getXmlAttribute(XmlNode node)

 {

     string rtn=string.Empty;

     foreach (XmlAttribute attr in node.Attributes)

     {

         rtn +="  "+ attr.Name + "=" + attr.Value;

     }

     return rtn;

 }

Copy after login


源码:

showXML.aspx

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="showXML.aspx.cs" Inherits="showXML" %>

 

<!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 id="Head1" runat="server">

    <title>读取xml</title>

   

    <link rel="Stylesheet" type="text/css" href="Css/Common/InputStyle.css" />

    <script type="text/javascript"  src="Scripts/jquery-1.4.1.min.js"></script>

    <script type="text/javascript">

        function showXml() {

            $.ajax({

                url: &#39;showXML.aspx?action=create&rnd&#39; + Math.random(),

                type: &#39;post&#39;,

                cache: false,

                async: false,

                success: function (result) {

                    if (result != &#39;&#39;) {

                        result = result.toString();

                        $("#pre_xml").show().html(&#39;&#39;).append(result);

                    }

                    else {

                        alert(&#39;读取数据失败!&#39;);

                    }

                }

            });

        }

 

        $(document).ready(function () {

            showXml();

        });

 

 

 

        function hideXml() {

            $("#pre_xml").hide();

        }

    </script>

 

 

    

 

</head>

<body>

    <form id="form1" runat="server">

    <p>

    <p class="main">

      <p class="button">

        <table width="100%">

          <tr>

            <td>

              <input type="button" id="btn_show" class="two-bu" onclick="showXml();" value="显示"/>

               <input type="button" id="btn_hide" class="two-bu" onclick="hideXml();" value="隐藏"/>

            </td>

          </tr>

        </table>

      </p>

      <p >

       <pre id="pre_xml"  style=" background-color:#A8B7CC; width:500px;"  >

Copy after login



showXML.aspx.cs


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.IO;

using System.Xml;

using System.Web.UI.HtmlControls;

 

public partial class showXML : System.Web.UI.Page

{

 

    public  string xml = string.Empty;

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

        }

        if (Request.QueryString["action"] != null && Request.QueryString["action"].ToString() != "")

        {

            switch (Request.QueryString["action"].ToString())

            {

                case "create":

                    Response.Clear();

                    Response.Write(showXml());

                    Response.End();

                    break;

                default:

                    break;

 

            }

        }

 

 

    }

 

 

    /// <summary>

    /// 在html显示xml

    /// </summary>

    /// <param name="fileName">文件名字

    private string showXml()

    {

        string rtn = string.Empty;

        string path = Server.MapPath("Xml\\") + "示例_创建" + ".xml"; //xml文件路径

        if (File.Exists(path))

        {

            XmlTextReader xmlRead = new XmlTextReader(path);//xml只读类

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(xmlRead);

 

            xmlRead.Close();

            getXMLStr(xmlDoc);

            rtn = xml;

        }

        return rtn;

 

    }

 

 

    private void getXMLStr(XmlDocument xmlDoc)

    {

        foreach (XmlNode node in xmlDoc.ChildNodes)

        {

 

            if (node.NodeType == XmlNodeType.Element)

            {

                getNext(node,0);

            }

            else

            {

                xml = "<p>" + node.OuterXml.Replace("<","<").Replace(">",">");

            }

 

 

        }

    }

 

 

    private void getNext(XmlNode node,int i)

    {

        if (node.NextSibling == null)//如果没有兄弟节点

        {

            if (node.HasChildNodes)

            {

                //如果有子节点

                if (node.FirstChild.NodeType != XmlNodeType.Text)

                {

                    //getXmlAttribute(node)  获取节点的所有属性

                    //如果子节点的子节点不是text类型

                    xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  ><" + node.Name +"  "+ getXmlAttribute(node) + "></p>";

                    getNext(node.FirstChild, i + 1);

                    xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  ></"+ node.Name +"></p>";

                }

                else

                {

                    //如果子节点的子节点不是text类型

                    xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";

  

                }

            }

            else 

            {

                xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";

            }

        }

        else

        {

            if (node.HasChildNodes)

            {

                if (node.FirstChild.NodeType != XmlNodeType.Text)

                {

 

                    xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  ><" + node.Name+"  " + getXmlAttribute(node) + "></p>";

                    getNext(node.FirstChild, i + 1);

                    xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  ></"+ node.Name  + "></p>";

                }

                else

                {

                    xml = xml + "<p " + "style=&#39;margin-left:" + (i * 20).ToString() + "px&#39;  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";

                }

            }

            else

            {

                xml = xml + "<p " + "style=&#39;margin-left:" + (i*20).ToString() + "px&#39;  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";

            }

            getNext(node.NextSibling,i);

 

        }

 

    }

 

 

    private string  getXmlAttribute(XmlNode node)

    {

        string rtn=string.Empty;

        foreach (XmlAttribute attr in node.Attributes)

        {

            rtn +="  "+ attr.Name + "=" + attr.Value;

        }

        return rtn;

    }

 

}

Copy after login



示例_创建.xml源码

注意:xml路径与后天获取的xml的路径要一致,我的路径是程序根目录xml文件夹下

示例_创建.xml源码


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

<?xml version="1.0" encoding="utf-8" ?>

<catalog  name="测试" >

    <cd >

        <title>11</title>

        <artist>12</artist>

        <country>13</country>

        <company>14</company>

        <price>15</price>

        <year>16</year>

    </cd>

    <cd>

        <title>21</title>

        <artist>22</artist>

        <country>23</country>

        <company>24</company>

        <price>25</price>

        <year>26</year>

    </cd>

   

  </catalog>

Copy after login



 以上就是xml学习(3) html显示xml的内容,更多相关内容请关注PHP中文网(www.php.cn)! 

  

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles