shijiang1130 发表于 2014-10-23 18:28

【xmerl】erlang 读xml文件

本帖最后由 shijiang1130 于 2014-12-26 23:00 编辑

-module(tmp).

-include("xmerl.hrl").

-export([file1/1,
       file2/1,
       file3/1]).

file1(F) ->
    xmerl_scan:file(F).

file2(F) ->
    xmerl_scan:file(F, [{space,normalize}]).

file3(F) ->
    Acc = fun(#xmlText{value = " ", pos = P}, Acc, S) ->
                  {Acc, P, S};% new return format
             (X, Acc, S) ->
                  {, S}
          end,
    xmerl_scan:file(F, [{space,normalize}, {acc_fun, Acc}]).

shijiang1130 发表于 2014-10-23 18:31

http://www.erlang.org/documentation/doc-1/apps/xmerl/xmerl_examples.html
文章来自这里

arserangel 发表于 2014-10-24 11:17

xmerl.hrl ,这个应该是一个 xml 的第三方库吧。

hncscwc 发表于 2014-10-24 11:59

OTP自带的回复 3# arserangel


   

shijiang1130 发表于 2014-12-26 22:58

更多时候,我们可以用 xmerl_xpath:string("//alert-list/alert[@id]",Root) 来得到xml中想要的内容。

shijiang1130 发表于 2014-12-26 22:59

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>

<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>

</bookstore>路径表达式        结果
bookstore        选取 bookstore 元素的所有子节点。
/bookstore       
选取根元素 bookstore。
注释:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径!
bookstore/book        选取属于 bookstore 的子元素的所有 book 元素。
//book        选取所有 book 子元素,而不管它们在文档中的位置。
bookstore//book        选择属于 bookstore 元素的后代的所有 book 元素,而不管它们位于 bookstore 之下的什么位置。
//@lang        选取名为 lang 的所有属性。
页: [1]
查看完整版本: 【xmerl】erlang 读xml文件