html5 try upgrading your browser,html - Need read XML and show it and save on Local storage. - Stack...

I found this article/tutorial.

It shows how to parse and save a xml file.

And how you can query it offline.

It is done by using javascript.

Article is on mantascode.com by Mantascode and describes how to parse a xml file to localstorage.

using a launch.html to parse xml file using java script.

Untitled Document

This page will parse and save books.xml into the users browser

specifically into html5 localStorage

The xml file being pushed is called books.xml

books.xml

OFFLINE.html

if (window.XMLHttpRequest)

{// code for IE7+, Firefox, Chrome, Opera, Safari

xmlhttp=new XMLHttpRequest();

}

else

{// code for IE6, IE5

xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

}

xmlhttp.open("GET","books.xml",false);

xmlhttp.send();

xmlDoc=xmlhttp.responseXML;

var xmlRowString = "";

for (var i = 0; i < xmlDoc.documentElement.childNodes.length; i++)

{

if ( xmlDoc.documentElement.childNodes[i].nodeName == 'book' )

{

for ( var k = 0 ; k < xmlDoc.documentElement.childNodes[i].childNodes.length; k++ )

{

if ( xmlDoc.documentElement.childNodes[i].childNodes[k].nodeName == 'author' )

{

xmlRowString += ""+xmlDoc.documentElement.childNodes[i].childNodes[k].textContent+"";

}

if ( xmlDoc.documentElement.childNodes[i].childNodes[k].nodeName == 'title' )

{

xmlRowString += "

"+xmlDoc.documentElement.childNodes[i].childNodes[k].textContent+"";

}

if ( xmlDoc.documentElement.childNodes[i].childNodes[k].nodeName == 'description' )

{

xmlRowString += ""+xmlDoc.documentElement.childNodes[i].childNodes[k].textContent+"";

}

}

}

if ( xmlRowString === "" )

{

}

else

{

//Here for each book we populate a local stoage row

if (typeof(localStorage) == 'undefined' )

{

alert('Your browser does not support HTML5 localStorage. Try upgrading.');

}

else

{

try

{

localStorage.setItem(i, xmlRowString);

}

catch (e)

{

alert("save failed!");

if (e == QUOTA_EXCEEDED_ERR)

{

alert('Quota exceeded!'); //data wasn't successfully saved due to quota exceed so throw an error

}

}

}

xmlRowString = "";

}

}

And a Offline.html to query the local stored xml

Search local storage

This page will allow the user to search the content saved in your local storage.

Search By Author name, results will be returned by book title.

Search By Author :

var localStorageRow = localStorage.getItem(localStorage.key(i)) ;

var author_query = getUrlVars()["txtA"];

if (typeof(author_query) == "undefined" || author_query === "" )

{

}

else

{

for ( var i = 0 ; i < localStorage.length; i++)

{

var localStorageRow = localStorage.getItem(localStorage.key(i)) ;

if (window.DOMParser)

{

parser=new DOMParser();

xmlDoc=parser.parseFromString(localStorageRow,"text/xml");

}

else // Internet Explorer

{

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

xmlDoc.async=false;

xmlDoc.loadXML(localStorageRow);

}

for ( var k = 0 ; k < xmlDoc.firstChild.childNodes.length ; k++ )

{

if ( xmlDoc.firstChild.childNodes[k].nodeName === "author" )

{

var auth_row = xmlDoc.firstChild.childNodes[k].textContent;

var authMatch = auth_row.match(new RegExp(author_query, "i"));

if ( authMatch )

{

//CANNOT USE XPATH(a lot of browsers dont support this)

//YOU HAVE TO LOOP THOUGH ELEMENTS (again) TO GET TITLE

/*

var nodesSnapshot = document.evaluate('//title', xmlDoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null );

for ( var q=0 ; q < nodesSnapshot.snapshotLength; q++ )

{

document.getElementById("results_ID").innerHTML += nodesSnapshot.snapshotItem(q).textContent+"
";

}

*/

for ( var p = 0 ; p < xmlDoc.firstChild.childNodes.length ; p ++ )

{

if ( xmlDoc.firstChild.childNodes[p].nodeName == 'title' )

{

document.getElementById("results_ID").innerHTML += xmlDoc.firstChild.childNodes[p].textContent+"
";

}

}

}

}

}

}

}

//GET URL VARS function

function getUrlVars() {

var vars = {};

var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {

vars[key] = value;

});

return vars;

}

Markup of books.xml

Gambardella, Matthew

XML Developer's Guide

Computer

44.95

2000-10-01

An in-depth look at creating applications

with XML.

Title of article:

Javascript: How to parse xml, write to html5 local storage, then read from local storage, and allow user to search content.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值