UNPKG

htm-to-json

Version:

to convert html string to json file , and also get data through tag name ,tag id and attribute value.

67 lines (54 loc) 1.35 kB
html-to-json ========= A small library providing utility methods for html string. ## Installation ```console npm install htm-to-json --save ``` ## Usage ```js var hObj = require('html-to-json'); var str = "<!DOCTYPE html><html lang='en'><head><title>html-to-json</title><meta charset='UTF-8'><div id='username'>praveen</div>"; hObj.convert_html_to_json(str,function(err,data){ if(err) throw err; console.log(data); }); ``` output:- ```console { html: [ { lang: 'en' } ], head: [], title: [ { innerHTML: 'html-to-json' } ], meta: [ { charset: 'UTF-8' }, div: [ { id: 'username', innerHTML: 'praveen' } ] } ``` ## Functions * convert_html_to_json * get_data_by_id * get_data_by_tag * get_data_by_attr_val ```js hObj.convert_html_to_json(str,function(err,jdata){ if(err) throw err; console.log(jdata); //get data by tag Id hObj.get_data_by_id(jdata,'username',function(err,data){ console.log(data); }); //get data by tag name eg:div hObj.get_data_by_tag(jdata,'div',function(err,data){ console.log(data); }); //get data by tag attribute value eg: first_name hObj.get_data_by_attr_val(jdata,'username',function(err,data){ console.log(data); }); }); ``` ## Tests ```console npm test.js ``` ## Release History * 0.1.1 Initial release