可能的xml解析 [英] Possible xml parsing

查看:120
本文介绍了可能的xml解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个新手的问​​题。



我正在尝试使用函数中的模式匹配解析一个xml消息



 < msg> < action type =xxx... /> < / MSG> 

我想要做的是(排序)

  decode_msg_in(< $<,$ m,$ s,$ g,$>,Message / binary,$& $ m,$ s,$ g,$>>>> R) - > 

解码不起作用(显然,这仅仅是我想要做的事情)



这是否可以?



有没有人有想法?或者我需要迭代整个消息作为一个列表,构建新的单词?



Regards
/ P

$ b $我可能认为你需要阅读关于位语法表达式二进制理解和关于这个xml解析器库,名为 erlsom ,下载它 here 。您将被提升到您要做的事情。

编辑
xml消息可能会以二进制形式或字符串的方式到达您的服务器:哪种方式它提供的xml解析器可以将xml数据解析为Erlang条款。使用 erlsom 库,这里是您的xml结构的一个例子。我有代码路径中的 erlsom 库。

 
C:\\ \\ Windows \System32> erl
Eshell V5.9(中止与^ G)
1> XML =< msg>< action type = \xxx\/> message< / msg>。
< msg>< action type = \xxx\/> message< / msg>
2> erlsom:simple_form(XML)。
{ok,{msg,[],[{action,[{type,xxx}],[]},message]},
[]}
3> {_,Parsed,_} = erlsom:simple_form(XML)。
{ok,{msg,[],[{action,[{type,xxx}],[]},message]},
[]}
4>解析。
{msg,[],[{action,[{type,xxx}],[]},message]}
5& {_,_,[{_,[{_,ActionType}],_},Message]} =解析。
{msg,[],[{action,[{type,xxx}],[]},message]}
6>操作类型。
xxx
7>信息。
message
8>

您可以在上面看到它归结为简单的模式匹配。只要发件人发送格式正确的 xml数据,结果将会给您一些干净的东西。如果您怀疑不正确的xml数据到达您的服务器,那么您需要将解析器包装到

尝试[GoodResult] - > [Action1]的[CALL] catch _Error:_Reason - > [Action2]结束。

请注意,如果XML正文很大,则需要使用 SAX方法来解析xml以避免大量内存脚印。 SAX示例包含在库文档中。


I've got a newbie question.

I'm trying to parse a xml message with pattern matching in functions

A sample of a message is:

    <msg> <action type="xxx"... />  </msg>

What I would like to able to do is ( sort of )

    decode_msg_in( << $<,$m,$s,$g,$>, Message/binary, $<,$/,$m,$s,$g,$> >>, R ) ->

The decode does not work (obviously, it's only a indication on what I'd like to do )

Is this even possible?

Does anyone have an idea? Or do I need to "iterate" the whole message as a list, building new "words" ?

Regards /P

解决方案

i probably think you need to read about Bit syntax expressions, Binary Comprehensions and about this xml parser library called erlsom, download it here. You will be brought up to speed in what you want to do.

EDIT


The xml message may reach your server as a binary, or as a string: Which ever way it does, the xml parser provided can parse the xml data into Erlang terms. Using the erlsom library, here is an example for your xml structure. I have my erlsom library in code path.

C:\Windows\System32>erl
Eshell V5.9  (abort with ^G)
1> XML = "<msg><action type=\"xxx\"/>message</msg>".
"<msg><action type=\"xxx\"/>message</msg>"
2> erlsom:simple_form(XML).
{ok,{"msg",[],[{"action",[{"type","xxx"}],[]},"message"]},
    []}
3> {_,Parsed,_} = erlsom:simple_form(XML).
{ok,{"msg",[],[{"action",[{"type","xxx"}],[]},"message"]},
    []}
4> Parsed.
{"msg",[],[{"action",[{"type","xxx"}],[]},"message"]}
5> {_,_,[{_,[{_,ActionType}],_},Message]} = Parsed.
{"msg",[],[{"action",[{"type","xxx"}],[]},"message"]}
6> ActionType.
"xxx"
7> Message.
"message"
8>

You can see above that it comes down to easy pattern matching. The resulting structure will give you something clean as long as the senders send properly formatted xml data. If you suspect improper xml data to hit your server, then, you need to wrap the parser in

try [CALL] of [GoodResult] -> [Action1] catch _Error:_Reason -> [Action2] end.

Note that if the XML Body is very large, you need to use SAX method to parse the xml to avoid big memory foot prints. SAX examples are included in the library documentation.

这篇关于可能的xml解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆