溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

html顯示xml文件數(shù)據(jù)

發(fā)布時(shí)間:2020-04-22 13:48:12 來源:億速云 閱讀:389 作者:小新 欄目:編程語(yǔ)言

本篇文章和大家了解一下html顯示xml文件數(shù)據(jù)。有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)大家有所幫助。

有的時(shí)候我們需要在html顯示xml,比如我們修改了xml,點(diǎn)擊保存,需要在頁(yè)面顯示xml源碼,讓我們知道xml已經(jīng)修改了,最好的方法是把xml放到pre元素中,但是會(huì)發(fā)現(xiàn)沒有換行,全部顯示一行,肯定很難看,所以我做了一個(gè)迭代xmlDOM的函數(shù)來格式顯示xml的函數(shù),

迭代函數(shù)思路:

1.每個(gè)xml文件時(shí)有無數(shù)個(gè)兄弟節(jié)點(diǎn)組成,但是終有最后截止的一個(gè),那么循環(huán)結(jié)束的標(biāo)志就是當(dāng)一個(gè)節(jié)點(diǎn)沒有兄弟節(jié)點(diǎn)時(shí),循環(huán)就結(jié)束,

那么可以循環(huán)兄弟節(jié)點(diǎn),于是有循環(huán)兄弟節(jié)點(diǎn)函數(shù)

2每個(gè)節(jié)點(diǎn)可能有子節(jié)點(diǎn),子節(jié)點(diǎn)也可能有兄弟子節(jié)點(diǎn),這個(gè)時(shí)候利用循環(huán)兄弟節(jié)點(diǎn)函數(shù),循環(huán)節(jié)點(diǎn)的第一個(gè)子節(jié)點(diǎn),

效果圖:

html顯示xml文件數(shù)據(jù)

主要代碼:

   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)//如果沒有兄弟節(jié)點(diǎn)
        {
            if (node.HasChildNodes)
            {
                //如果有子節(jié)點(diǎn)
                if (node.FirstChild.NodeType != XmlNodeType.Text)
                {
                    //getXmlAttribute(node)  獲取節(jié)點(diǎn)的所有屬性
                    //如果子節(jié)點(diǎn)的子節(jié)點(diǎn)不是text類型
                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  ><" + node.Name +"  "+ getXmlAttribute(node) + "></p>";
                    getNext(node.FirstChild, i + 1);
                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  ></"+ node.Name +"></p>";
                }
                else
                {
                    //如果子節(jié)點(diǎn)的子節(jié)點(diǎn)不是text類型
                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";
 
                }
            }
            else  
            {
                xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";
            }
        }
        else
        {
            if (node.HasChildNodes)
            {
                if (node.FirstChild.NodeType != XmlNodeType.Text)
                {

                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  ><" + node.Name+"  " + getXmlAttribute(node) + "></p>";
                    getNext(node.FirstChild, i + 1);
                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  ></"+ node.Name  + "></p>";
                }
                else
                {
                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";
                }
            }
            else
            {
                xml = xml + "<p " + "style='margin-left:" + (i*20).ToString() + "px'  >" + 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;
    }


 源碼:

showXML.aspx

<%@ 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: 'showXML.aspx?action=create&rnd' + Math.random(),
                type: 'post',
                cache: false,
                async: false,
                success: function (result) {
                    if (result != '') {
                        result = result.toString();
                        $("#pre_xml").show().html('').append(result);
                    }
                    else {
                        alert('讀取數(shù)據(jù)失敗!');
                    }
                }
            });
        }

        $(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;"  ></pre>
      </p>
      

     </p>
    
    </p>
    
    </form>
</body>
</html>



showXML.aspx.cs


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">文件名字</param>
    private string showXml()
    {
        string rtn = string.Empty;
        string path = Server.MapPath("Xml\\") + "示例_創(chuàng)建" + ".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)//如果沒有兄弟節(jié)點(diǎn)
        {
            if (node.HasChildNodes)
            {
                //如果有子節(jié)點(diǎn)
                if (node.FirstChild.NodeType != XmlNodeType.Text)
                {
                    //getXmlAttribute(node)  獲取節(jié)點(diǎn)的所有屬性
                    //如果子節(jié)點(diǎn)的子節(jié)點(diǎn)不是text類型
                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  ><" + node.Name +"  "+ getXmlAttribute(node) + "></p>";
                    getNext(node.FirstChild, i + 1);
                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  ></"+ node.Name +"></p>";
                }
                else
                {
                    //如果子節(jié)點(diǎn)的子節(jié)點(diǎn)不是text類型
                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";
 
                }
            }
            else  
            {
                xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";
            }
        }
        else
        {
            if (node.HasChildNodes)
            {
                if (node.FirstChild.NodeType != XmlNodeType.Text)
                {

                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  ><" + node.Name+"  " + getXmlAttribute(node) + "></p>";
                    getNext(node.FirstChild, i + 1);
                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  ></"+ node.Name  + "></p>";
                }
                else
                {
                    xml = xml + "<p " + "style='margin-left:" + (i * 20).ToString() + "px'  >" + node.OuterXml.Replace("<", "<").Replace(">", ">") + "</p>";
                }
            }
            else
            {
                xml = xml + "<p " + "style='margin-left:" + (i*20).ToString() + "px'  >" + 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;
    }

}



示例_創(chuàng)建.xml源碼

注意:xml路徑與后天獲取的xml的路徑要一致,我的路徑是程序根目錄xml文件夾下

示例_創(chuàng)建.xml源碼


<?xml version="1.0" encoding="utf-8" ?>
<catalog  name="測(cè)試" >
    <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>

以上就是html顯示xml文件數(shù)據(jù)的詳細(xì)內(nèi)容了,看完之后是否有所收獲呢?如果想了解更多相關(guān)內(nèi)容,歡迎來億速云行業(yè)資訊!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI