Showing posts with label erlang. Show all posts
Showing posts with label erlang. Show all posts

Friday, 22 May 2015

HTML parser in Erlang


html parser using lexx and yec parsing tool in erlang

required files

1. html_parser_manager.erl
2. htmlParser.erl
3. htmlTokenGenerator.erl
4. html_validator.erl
5. file_reader.erl

use the html parser by

html_parser_manager:parse_file("HtmlFileNameLocation"). 

Thursday, 21 May 2015

XML Parser in Erlang

XML parser using lexx and yecc

need to download following erlang files

1. xmlParser.erl
2. xmlTokensGen.erl


run by

%%%=====================================================
get_xml(Url) ->
  case getWebContent(Url) of

    {ok,{{_,200,"OK"},_Header,Body}} ->  parseString(Body);

    Any  -> io:format(" Un Handle Response ~p",[Any]),error

  end.

%%%======================================================
%%% Internal functions
%%%======================================================

getWebContent(Url) ->
  inets:start(),ssl:start(),
  httpc:request(get, {Url, [{"connection", "close"}]},[],[]).


parseString(String) ->
  {ok,Tokens,_} = xmlTokensGen:string(String),
  xmlParser:parse(Tokens).

Json creator in Erlang


creating json form data from erlang terms

input : list of tuple form of erlang term
output : string form of json 

erlang file: jsonGenerator.erl

how to use

createJson() -> 
Sample ={geonames, [
        { fcodeName, "capital of a political entity"},
        {toponymName, "Mexico City"},
        {countrycode, "MX"},
        {fcl, "P"},
        {fclName, "city, village,..."},
        {name, "Mexiko-Stadt"},
        {wikipedia, "en.wikipedia.org/wiki/Mexico_City"},
        {lng, -99.12766456604},
        {fcode, "PPLC"},
        {geonameId, 3530597},
        {lat, 19.428472427036},
        {population, 12294193}
]},
jsonGenerator:create_json(Sample).


JSON Parser in Erlang

Json parser created by erlang parsing tool lexx and yecc.

You have to download following erlang files

1.jsonParser.erl

2.jsonTokenGen.erl


simple way to use this parser like this

parse() ->
JSON = "
{
    \"geonames\": [
        {
            \"fcodeName\": \"capital of a political entity\",
            \"toponymName\": \"Mexico City\",
            \"countrycode\": \"MX\",
            \"fcl\": \"P\",
            \"fclName\": \"city, village,...\",
            \"name\": \"Mexiko-Stadt\",
            \"wikipedia\": \"en.wikipedia.org/wiki/Mexico_City\",
            \"lng\": -99.12766456604,
            \"fcode\": \"PPLC\",
            \"geonameId\": 3530597,
            \"lat\": 19.428472427036,
            \"population\": 12294193
        },
        {
            \"fcodeName\": \"capital of a political entity\",
            \"toponymName\": \"Beijing\",
            \"countrycode\": \"CN\",
            \"fcl\": \"P\",
            \"fclName\": \"city, village,...\",
            \"name\": \"Peking\",
            \"wikipedia\": \"en.wikipedia.org/wiki/Beijing\",
            \"lng\": 116.397228240967,
            \"fcode\": \"PPLC\",
            \"geonameId\": 1816670,
            \"lat\": 39.9074977414405,
            \"population\": 11716620
        }
    ]
}",
 {ok,Tokens,_} = jsonTokensGen:string(JSON),
  jsonParser:parse(Tokens).