- 浏览: 306387 次
文章分类
最新评论
-
shoru:
最新的netbeans scala插件无法安装到6.9中呀,求 ...
Scala-对Java的修正和超越-Presentation -
alanwu:
night_stalker 写道讲一次多少钱啊,能透露下么…… ...
Scala-对Java的修正和超越-Presentation -
wzjin:
暂时没有考虑学习,等有一定市场再看!
Scala-对Java的修正和超越-Presentation -
dcaoyuan:
jasspier 写道对scala有点兴趣,有没有入门的推荐教 ...
Scala-对Java的修正和超越-Presentation -
jasspier:
草原兄,听了你的课,对scala有点兴趣,有没有入门的推荐教程 ...
Scala-对Java的修正和超越-Presentation
Parse JSON to xmerl Compitable XML Tree via A Simple XML State Machine
- 博客分类:
- Erlang
Updated Aug 5: rewrote json_parser.erl base on tonyg's RFC4627 implementation, fixed some bugs.
In my previous blog: A Simple XML State Machine Accepting SAX Events to Build xmerl Compitable XML Tree: icalendar demo, I wrote a simple state machine to parse icalendar to xmerl compitable XML tree. This time, I'll use this state machine to parse a JSON expression to xmerl compitable XML tree, the work is fairly simple:
-module(json_parser). -define(stateMachine, fun xml_sm:state/2). -export([parse/1]). -export([test/0]). parse(Bin) when is_binary(Bin) -> parse(binary_to_list(Bin)); parse(Text) -> States1 = ?stateMachine({startDocument}, undefined), States2 = parse_value(skip_ws(Text), States1), States3 = ?stateMachine({endDocument}, States2). parse_value([], States) -> States; parse_value([H|T], States) when H == $"; H == $' -> {Rest, Value} = parse_string(T, [], H), States1 = ?stateMachine({characters, Value}, States), {Rest, States1}; parse_value([${|T], States) -> States1 = ?stateMachine({startElement, [], obj, [], []}, States), parse_object(skip_ws(T), States1); parse_value([$[|T], States) -> parse_array(skip_ws(T), States); parse_value(Chars, States) -> {Rest, Value} = parse_number(skip_ws(Chars), []), States1 = ?stateMachine({characters, Value}, States), {Rest, States1}. parse_object([$}|T], States) -> States1 = ?stateMachine({endElement, [], obj, []}, States), case skip_ws(T) of [] -> States1; %% final result Chars -> {Chars, States1} end; parse_object([$,|T], States) -> parse_object(skip_ws(T), States); parse_object([H|T], States) when H == $"; H == $' -> {Rest, Name} = parse_string(skip_ws(T), [], H), States1 = ?stateMachine({startElement, [], list_to_atom(Name), [], []}, States), [$:|Rest1] = skip_ws(Rest), {Rest2, States2} = parse_value(skip_ws(Rest1), States1), States3 = ?stateMachine({endElement, [], undefined, []}, States2), parse_object(skip_ws(Rest2), States3). parse_array([$]|T], States) -> {T, States}; parse_array([$,|T], States) -> parse_array(skip_ws(T), States); parse_array(Chars, States) -> {Rest, States1} = parse_value(Chars, States), parse_array(skip_ws(Rest), States1). parse_string([H|T], Acc, Quote) when H == Quote -> {T, lists:reverse(Acc)}; parse_string([H|T], Acc, Quote) -> parse_string(T, [H|Acc], Quote). parse_number([H|T], Acc) when H == $,; H == $}; H == $] -> {[H|T], lists:reverse(Acc)}; parse_number([H|T], Acc) -> parse_number(T, [H|Acc]). skip_ws([H|T]) when H =< 32 -> skip_ws(T); skip_ws(Chars) -> Chars. test() -> Text = " {'businesses': [{'address1': '650 Mission Street', 'address2': '', 'avg_rating': 4.5, 'categories': [{'category_filter': 'localflavor', 'name': 'Local Flavor', 'search_url': 'http://lightpole.net/search'}], 'city': 'San Francisco', 'distance': 0.085253790020942688, 'id': '4kMBvIEWPxWkWKFN__8SxQ', 'latitude': 37.787185668945298, 'longitude': -122.40093994140599}, {'address1': '25 Maiden Lane', 'address2': '', 'avg_rating': 5.0, 'categories': [{'category_filter': 'localflavor', 'name': 'Local Flavor', 'search_url': 'http://lightpole.net/search'}], 'city': 'San Francisco', 'distance': 0.23186808824539185, 'id': 'O1zPF_b7RyEY_NNsizX7Yw', 'latitude': 37.788387, 'longitude': -122.40401}]} ", {ok, Xml} = parse(Text), %io:fwrite(user, "Xml Tree: ~p~n", [Xml]), XmlText = lists:flatten(xmerl:export_simple([Xml], xmerl_xml)), io:fwrite(user, "Parsed: ~n~p~n", [XmlText]), Latitude1 = xmerl_xpath:string("/obj/businesses/obj[1]/latitude/text()", Xml), io:format(user, "Latitude1: ~p~n", [Latitude1]).
The result will be something like:
<?xml version=\"1.0\"?> <obj> <businesses> <obj> <address1>650 Mission Street</address1> <address2></address2> <avg_rating>4.5</avg_rating> <categories> <obj> <category_filter>localflavor</category_filter> <name>Local Flavor</name> <search_url>http://lightpole.net/search</search_url> </obj> </categories> <city>San Francisco</city> <distance>0.085253790020942688</distance> <id>4kMBvIEWPxWkWKFN__8SxQ</id> <latitude>37.787185668945298</latitude> <longitude>-122.40093994140599</longitude> </obj> <obj> <address1>25 Maiden Lane</address1> <address2></address2> <avg_rating>5.0</avg_rating> <categories> <obj> <category_filter>localflavor</category_filter> <name>Local Flavor</name> <search_url>http://lightpole.net/search</search_url> </obj> </categories> <city>San Francisco</city> <distance>0.23186808824539185</distance> <id>O1zPF_b7RyEY_NNsizX7Yw</id> <latitude>37.788387</latitude> <longitude>-122.40401</longitude> </obj> </businesses> </obj>
Now you fecth element by:
> [Latitude1] = xmerl_xpath:string("/obj/businesses/obj[1]/latitude/text()", Xml), > Latitude1#xmlText.value. "37.787185668945298"
Next time, I'll write a simple Erlang Data state machine, which will parse icalendar and json to simple Erlang Lists + Tuples.
The code of xml_sm.erl can be found in my previous blog.
发表评论
-
Wide Finder - Erlang实现小结
2007-11-12 13:50 2653Tim的WideFinder习题让多核和并行编程实践在一个简单 ... -
From Rails to Erlyweb - Part II Manage Project - Reloaded
2007-08-23 20:45 1355The migrating from Rails to Erl ... -
From Rails to Erlyweb - Part II Manage Project - Reloaded
2007-08-23 20:45 1198The migrating from Rails to Erl ... -
ErlyBird 0.12.0 released - Erlang IDE based on NetBeans
2007-08-08 18:34 886I'm pleased to announce ErlyBir ... -
A Simple POET State Machine Accepting SAX Events to Build Plain Old Erlang Term
2007-08-14 19:37 645Per previous blogs: A Simple ... -
recbird - An Erlang Dynamic Record Inferring Parse Transform
2007-08-19 07:57 807You should have read Yariv's re ... -
A Simple POET State Machine Accepting SAX Events to Build Plain Old Erlang Term
2007-08-14 19:37 1034Per previous blogs: A Simple ... -
recbird - An Erlang Dynamic Record Inferring Parse Transform
2007-08-19 07:57 1164You should have read Yariv's re ... -
recbird - An Erlang Dynamic Record Inferring Erlang Parse Transform
2007-08-19 07:57 1215You should have read Yariv's re ... -
A Simple POET State Machine Accepting SAX Events to Build Plain Old Erlang Term
2007-08-14 19:37 1090Per previous blogs: A Simple ... -
ErlyBird 0.12.0 released - Erlang IDE based on NetBeans
2007-08-08 18:34 821I'm pleased to announce ErlyBir ... -
ErlyBird 0.12.0 released - Erlang IDE based on NetBeans
2007-08-08 18:34 1117I'm pleased to announce ErlyBir ... -
Parse JSON to xmerl Compitable XML Tree via A Simple XML State Machine
2007-08-04 09:55 951Updated Aug 5: rewrote json_par ... -
A Simple XML State Machine Accepting SAX Events to Build xmerl Compitable XML Tree: icalendar demo
2007-07-30 00:06 1068xmerl is a full XML functionali ... -
Parse JSON to xmerl Compitable XML Tree via A Simple XML State Machine
2007-08-04 09:55 1481In my previous blog: A Simple ... -
A Simple XML State Machine Accepting SAX Events to Build xmerl Compitable XML Tree: icalendar demo
2007-07-30 00:06 1369xmerl is a full XML functionali ... -
A Simple XML State Machine Accepting SAX Events to Build xmerl Compitable XML Tree: icalendar demo
2007-07-30 00:06 1437xmerl is a full XML functionali ...
相关推荐
Is it possible to parse JSON in TSQL? I dont mean to create a JSON string, i mean to parse a json string passed in as a parameter.数据库parseJSON 转表
在上述代码中,`$.parseXML()`函数用于将XML字符串解析为DOM对象,然后`$.xml2json()`函数将DOM对象转换为JSON对象。转换后的JSON对象可以直接在JavaScript环境中使用,如遍历、操作或发送到服务器。 值得注意的是...
XML(Extensible Markup Language)和JSON(JavaScript Object Notation)是两种常见的数据交换格式,广泛应用于Web服务和数据传输。XML是一种结构化语言,用于描述数据,而JSON更简洁,通常用于JavaScript应用程序...
标题中的"(C#)json to xml 解析转换源代码"指出我们将探讨一个C#实现的JSON到XML的转换工具。这种转换通常涉及两个主要步骤:首先解析JSON字符串,然后将解析得到的数据结构转换成XML表示。 描述中提到的"用C#写的...
大多数网上下载的SQL parseJson函数都存在Bug,我也是下载应用到公司系统开发后才发现,无奈系统已经正式使用,硬着头皮改Bug,至少改了两处Bug,修改不易,希望大家体谅一下.
toJSONString method and a parseJSON method to Object.prototype. Use of this file is not recommended. json_parse.js: This file contains an alternative JSON parse function that uses recursive descent ...
Jboss启动报Failed to parse WEB-INF/web.xml; - nested throwable错误解决方案 在Jboss应用服务器中,启动报错Failed to parse WEB-INF/web.xml; - nested throwable是一种常见的错误,本文将对此错误进行深入分析...
JSON(JavaScript Object Notation)和XML(eXtensible Markup Language)是两种广泛使用的数据交换格式,它们在web服务和应用程序之间传输数据时起到关键作用。本文将深入讲解如何在Java环境中将Java对象与JSON报文...
总结来说,"jquery-xmlToJson.js"是一个用于XML到JSON转换的jQuery插件,它能够帮助开发者轻松地将XML数据转化为更适应JavaScript环境的JSON格式,提高数据处理的效率,并且兼容多种jQuery版本。在进行Web开发时,...
在.NET开发环境中,数据交换和序列化经常是关键任务,其中JSON和XML是最常见的两种格式。本文将深入探讨如何在C#中使用Newtonsoft.Json.dll库进行JSON与XML之间的转换,特别关注对.NET Framework 2.0及以上版本的...
在描述中提到的"Table2JSONTree_C#根据TABLE产生JSON树_jsontree2table_"是一个关于将数据库中的表格数据转换为JavaScript Object Notation (JSON)树形结构的过程。这种转换在前端开发中非常常见,特别是在需要在...
《纯C语言XML解析——xmlparse.c&xmlparse.h》 XML(eXtensible Markup Language)是一种广泛应用的标记语言,常用于数据交换、配置文件和文档存储等场景。在IT行业中,尤其是在需要跨平台或者对性能有较高要求的...
JSON(JavaScript Object Notation)和XML(eXtensible Markup Language)是两种广泛使用的数据交换格式,它们在Web服务和应用程序之间传输数据时起到关键作用。JSON以其简洁、易读和高效而受到开发者喜爱,而XML则...
在.NET编程环境中,C#是一种常用的编程语言,而JSON(JavaScript Object Notation)和XML(eXtensible Markup Language)是两种广泛使用的数据交换格式。本文将深入探讨如何在C#中实现JSON与XML的相互转换,以满足...
Perl中的XML::Simple模块是Perl社区广泛使用的XML解析器,尤其适合处理小型或结构简单的XML文档。这个模块的名称虽然包含“Simple”,但它实际上提供了一种简洁的接口,用于将XML数据转换为Perl数据结构,反之亦然。...
在Python中,有`xml.etree.ElementTree`库处理XML,`json`模块处理JSON。 总结来说,XML和JSON是数据表示的两种重要方式,它们各有特点,适应不同的应用场景。TestXML文件中的内容可能是展示如何在代码中创建、解析...
JSON(JavaScript Object Notation)和XML(eXtensible Markup Language)是两种广泛用于数据交换的格式,尤其在Web服务和移动应用开发中。本文将深入探讨这两种格式在安卓平台上的使用及其优缺点。 首先,让我们...
在iOS开发中,XML(Extensible Markup Language)和JSON(JavaScript Object Notation)是两种常见的数据交换格式。XML因其结构严谨、易于解析而被广泛应用于数据存储和传输,而JSON则以其简洁、易于读写的特点在...
sql server 2014 JSON解析到表函数 CREATE FUNCTION [dbo].[parseJSON]( @JSON NVARCHAR(MAX)) RETURNS @hierarchy TABLE ( element_id INT IDENTITY(1, 1) NOT NULL, ...
在Android开发中,XML(可扩展标记语言)和JSON(JavaScript Object Notation)都是常见的数据交换格式。XML因其结构化特性适用于复杂的文档存储,而JSON则以其轻量级、易于阅读和编写的特点广泛用于Web服务的数据...