如何解析erlang中的xml?

时间:2017-01-17 13:28:23

标签: parsing xml-parsing erlang

我在元组列表中有xml提取的字符串:

MessageResponse = [{"code",0},{"description","description"},{"respuestaServicioSoap",{{"executeWebServiceSolutionResult",{{"CEDULARUCSpecified", false},{"AUTORIZACION", "00000012431781"},{"AUTORIZACIONSpecified",true},{"RESULTADO","000"},{"CODIGO_RESULTADOSpecified",true},{"COD_PAGO","00000012431781"},{"COD_PAGOSpecified",true},{"COMISION",{{"string","0"}}},{"COMISIONSpecified", true},{"DIRECCIONSpecified", false},{"FECHA_COMPENSACIONSpecified", false},{"FECHA_TRANSACCION","20170116"},{"FECHA_TRANSACCIONSpecified",true},{"FECHORA_SW","20170116123951"},{"FECHORA_SWSpecified",true},{"HORA_TRANSACCION","123951"},{"HORA_TRANSACCIONSpecified",true},{"MENSAJE","TRANSACCION OK"},{"MENSAJESpecified",true},{"NOMBRESpecified",false},{"PRODUCTO","0010761005"},{"PRODUCTOSpecified",true},{"SECUENCIA_ADQ","2833"},{"SECUENCIA_ADQSpecified",true},{"SECUENCIA_SW","576167"},{"SECUENCIA_SWSpecified",true},{"TERMINAL","0696069603000001"},{"TERMINALSpecified",true},{"TYPE_TRNSpecified",false},{"VALOR_TOTAL", { {  "string",  "0" }}},{"VALOR_TOTALSpecified",true},{"XML_ADDSpecified",false},{"XML_DATASpecified",false},{"XML_FACT","<XML_FACT>\r\n  <DATOS_FACT>\r\n    <LINEA_1>REPRESENTACIONES ORMAN S.A.</LINEA_1>\r\n    <LINEA_2>RUC: 0987654321</LINEA_2>\r\n    <LINEA_3 />\r\n    <LINEA_4 />\r\n    <LINEA_5>FACTURA: 001-627-0000048745</LINEA_5>\r\n    <LINEA_6>CLAVE: </LINEA_6>\r\n    <LINEA_7>COMISION POR SERVICIO</LINEA_7>\r\n    <LINEA_8>RECAUDACION EEAAPP - CUENTA: 11223344</LINEA_8>\r\n    <LINEA_12>FACTURA: 001-627-0000048745 - CONSULTE SU DOCUMENTO EN WWW.LITO.COM/DOCUMENTOSELECTRONICOS</LINEA_12>\r\n    <MSGCOMP />\r\n    <MSGFACT />\r\n  </DATOS_FACT>\r\n</XML_FACT>"},{"XML_FACTSpecified",true},{"XML_REPLY_CONSULTASpecified",false},{"XML_REPLY_PAGOSSpecified",false}}},{"executeWebServiceSolutionResultSpecified", true}}},{"result", "ok"}]

并且需要获取LINEA_5标签中的文字,任何想法怎么做?

2 个答案:

答案 0 :(得分:1)

OTP库xmerl提供了操作XML文件或字符串的所有功能。它提供了一组有助于处理不同元素的记录。

文档is available here

记录在erlXX / lib / xmerl-YYY / include / xmerl.hrl中定义:

  • #xmlText{}
  • #xmlElement{}
  • #xmlPI{}
  • #xmlComment{}
  • #xmlDecl{}

<强> [编辑]

您在示例中提供的xml数据已经被修改,因此我从我自己的示例中获取示例。考虑一个包含内容的xml文件:

<?xml version="1.0"  encoding="UTF-8"?> <package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="uuid_id">
    <metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:opf="http://www.idpf.org/2007/opf" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:calibre="http://calibre.kovidgoyal.net/2009/metadata" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <dc:creator opf:role="aut" opf:file-as="Ahern, Cecelia">Cecelia Ahern</dc:creator>
    <dc:publisher>J'ai Lu</dc:publisher>
    <meta name="calibre:title_sort" content="Si tu me voyais maintenant"/>
    <dc:description>description blah blah</dc:description>
    <meta name="calibre:timestamp" content="2012-03-18T18:04:20+00:00"/>
    <dc:title>Si tu me voyais maintenant</dc:title>
    <meta name="cover" content="cover"/>
    <dc:date>2012-03-18T18:04:23+00:00</dc:date>
    <dc:contributor opf:role="bkp">calibre (0.8.42) [http://calibre-ebook.com]</dc:contributor>
    <dc:identifier opf:scheme="ISBN">9782290006504</dc:identifier>
    <dc:identifier id="uuid_id" opf:scheme="uuid">7d062b17-258e-4268-9d46-a753c063c969</dc:identifier>
    <dc:subject>Chick-lit</dc:subject>
    <meta name="calibre:user_categories" content="{}"/>
    <meta name="calibre:author_link_map" content="{&quot;Cecelia Ahern&quot;: &quot;&quot;}"/>
    <dc:language>fr</dc:language>
    </metadata>
    <manifest>
        <item href="cover.jpeg" id="cover" media-type="image/jpeg"/>
    </manifest>
    <spine toc="ncx">
        <itemref idref="titlepage"/>
    </spine>
    <guide>
        <reference href="titlepage.xhtml" type="cover" title="Cover"/>
    </guide> </package>

它是从epub书中提取的,并存储在文件&#34; content.opf&#34;中。如果我想获得作者姓名(第4行),我可以这样做:

1> rr("C:\\My programs\\erl8.2\\lib\\xmerl-1.3.12\\include\\xmerl.hrl").  
2> {Xml,_} = xmerl_scan:file("../doc/content.opf"),                 
2> Content = Xml#xmlElement.content,                                
2> [MetaRec] = [X || X <- Content, X#xmlElement.name == metadata],  
2> Meta = MetaRec#xmlElement.content,                               
2> [CreatRec] = [X || X <- Meta, X#xmlElement.name == 'dc:creator'],   
2> Creat = CreatRec#xmlElement.content,                             
2> [CreatText] = [X || X <- Creat, is_record(X,xmlText)],           
2> Aut = CreatText#xmlText.value.                                   
"Cecelia Ahern"

答案 1 :(得分:1)

使用此代码:

{Xml, _Rest} = xmerl_scan:string(XmlFactura).
[#xmlText{value=Linea5}] = xmerl_xpath:string("//LINEA_5/text()", Xml).