標籤:
上回說到,我們已經得到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——我的畢設(二)