使用HTML javascript解析xml——我的畢設(二)

來源:互聯網
上載者:User

標籤:

  上回說到,我們已經得到openHab的xml格式頁面,這次說我們要開始解析得到的xml了。

  我使用的是Hbuilder,來利用已有APP基座開啟一個webview來載入HTML頁面,實現app效果。

        <script type="text/javascript">            $(document).ready(function() {                $.ajax({                    url: ‘http://192.168.0.19:8080/rest/items‘,                    type: ‘GET‘,                    dataType: ‘xml‘,                    timeout: 1000,  //設定逾時                    cache: false,   //禁用緩衝                    error: function(xml) {                        alert("載入XML文檔出錯!");                    },                    success: GetStudentComplete   //設定成功後回呼函數                });            });            //擷取XML成功後回呼函數            function GetStudentComplete(xml) {                $(xml).find("item").each(function(i) {     //尋找所有student節點並遍曆                    var name = $(this).children("name");          //獲得子節點                    var state = $(this).children("state");          //獲得子節點                    var links = $(this).children("link");                    var item_name = name.text();                 //擷取節點文本                    var item_state = state.text();  //擷取節點的屬性                    var item_link = links.text();                    var divClass = $(‘<li><a href="‘+item_link+‘">‘+item_name+‘</a></li>‘);                    $("#main").find(‘ul‘).append(divClass);                });            }        </script>

這裡的html body部分如下:

    <body>        <div id="main" class="mui-content">            <ul class="mui-table-view">            </ul>        </div>    </body>

我們提取xml檔案的name、state、link作為HTML頁面的list。今天先進行到這一步。

結果如下:

 

關於使用美化,我們後面一篇再講。

———————————原創文章,轉載請聯絡[email protected],本文在開源智能家居論壇首發———————————————

使用HTML javascript解析xml——我的畢設(二)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.