Skip to content

Instantly share code, notes, and snippets.

@kassane
Last active May 10, 2023 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kassane/54ffa677fc6250d9214e5671f70aac44 to your computer and use it in GitHub Desktop.
Save kassane/54ffa677fc6250d9214e5671f70aac44 to your computer and use it in GitHub Desktop.
Zig XML parser to NFe

Some tests

Need: https://github.com/kassane/xml (forked: updated to zig 0.11)

nfe_sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<NFe xmlns="http://www.portalfiscal.inf.br/nfe">
    <infNFe versao="4.00" Id="NFe41200880249881000118650010000278531000123456">
        <!-- Elementos suprimidos pra facilitar a visualização do exemplo-->
        <infAdic>
            <!-- Elementos suprimidos pra facilitar a visualização do exemplo-->
            <infAdFisco>
                eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYWYiOiJKdzJrQV9weFFxV0RWZjRBcTc2a2pRIiwi
                dnNiIjoxLCJtb3AiOjAsInBkdiI6Ing0WW93Sm82WHMiLCJjbnQiOjIsImF1dCI6IjdsT2JIMUdBQk9LO
                UV5c3VSbWpYYVlkMnFuS2U3cGlSQUJhWGItbGNCTTAifQ.NwfMyW_KODNNpUhNEeRkks3All7XIqNuN_B
                fMYS76ng
            </infAdFisco>
        </infAdic>
    </infNFe>
    <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"><!-- Assinatura -->
    </Signature>
</NFe>

Run Xml.zig

$> zig build run -- nfe_sample.xml
doctype: xml
attr_key: version
attr_value: "1.0"
attr_key: encoding
attr_value: "UTF-8"
tag_open: NFe
attr_key: xmlns
attr_value: "http://www.portalfiscal.inf.br/nfe"
tag_open: infNFe
attr_key: versao
attr_value: "4.00"
attr_key: Id
attr_value: "NFe41200880249881000118650010000278531000123456"
tag_open: infAdic
tag_open: infAdFisco
content: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkYWYiOiJKdzJrQV9weFFxV0RWZjRBcTc2a2pRIiwi
                dnNiIjoxLCJtb3AiOjAsInBkdiI6Ing0WW93Sm82WHMiLCJjbnQiOjIsImF1dCI6IjdsT2JIMUdBQk9LO
                UV5c3VSbWpYYVlkMnFuS2U3cGlSQUJhWGItbGNCTTAifQ.NwfMyW_KODNNpUhNEeRkks3All7XIqNuN_B
                fMYS76ng
            
tag_close: infAdFisco
tag_close: infAdic
tag_close: infNFe
tag_open: Signature
attr_key: xmlns
attr_value: "http://www.w3.org/2000/09/xmldsig#"
tag_close: Signature
tag_close: NFe
eof:

Another example

register_sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<iniciarRegistro xmlns="http://www.portalfiscal.inf.br/daf/wsdl/DAFRegistroDispositivo">
    <pedRegistro xmlns="http://www.portalfiscal.inf.br/daf" versao="1.00">
        <infRegistro Id="DAFughyrcDYBW0zaIGJG3Z6iw">
            <!-- Conteúdo do pedido -->
        </infRegistro>
        <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
            <SignedInfo>
                <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
                <Reference URI="#ughyrcDYBW0zaIGJG3Z6iw">
                    <Transforms>
                        <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
                        <Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
                    </Transforms>
                    <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                    <DigestValue>dQXTK2bjTzPLEWKGztY8wuv7f20=</DigestValue>
                </Reference>
            </SignedInfo>
            <SignatureValue>vhqZ3zpWq58OPRyYJdGsKw7JX+oEwYW2wPRpAIgobsC...</SignatureValue>
            <KeyInfo>
                <X509Data>
                    <X509Certificate>MIIDjzCCAnegAwIBAgIEF2/aITANBgkqhkiG9w...</X509Certificate>
                </X509Data>
            </KeyInfo>
        </Signature>
    </pedRegistro>
</iniciarRegistro>

Run Xml.zig

$> zig build run -- register_sample.xml
doctype: xml
attr_key: version
attr_value: "1.0"
attr_key: encoding
attr_value: "UTF-8"
tag_open: iniciarRegistro
attr_key: xmlns
attr_value: "http://www.portalfiscal.inf.br/daf/wsdl/DAFRegistroDispositivo"
tag_open: pedRegistro
attr_key: xmlns
attr_value: "http://www.portalfiscal.inf.br/daf"
attr_key: versao
attr_value: "1.00"
tag_open: infRegistro
attr_key: Id
attr_value: "DAFughyrcDYBW0zaIGJG3Z6iw"
tag_close: infRegistro
tag_open: Signature
attr_key: xmlns
attr_value: "http://www.w3.org/2000/09/xmldsig#"
tag_open: SignedInfo
tag_open: CanonicalizationMethod
attr_key: Algorithm
attr_value: "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
tag_close_empty: /
tag_open: SignatureMethod
attr_key: Algorithm
attr_value: "http://www.w3.org/2000/09/xmldsig#rsa-sha1"
tag_close_empty: /
tag_open: Reference
attr_key: URI
attr_value: "#ughyrcDYBW0zaIGJG3Z6iw"
tag_open: Transforms
tag_open: Transform
attr_key: Algorithm
attr_value: "http://www.w3.org/2000/09/xmldsig#enveloped-signature"
tag_close_empty: /
tag_open: Transform
attr_key: Algorithm
attr_value: "http://www.w3.org/TR/2001/REC-xml-c14n-20010315"
tag_close_empty: /
tag_close: Transforms
tag_open: DigestMethod
attr_key: Algorithm
attr_value: "http://www.w3.org/2000/09/xmldsig#sha1"
tag_close_empty: /
tag_open: DigestValue
content: dQXTK2bjTzPLEWKGztY8wuv7f20=
tag_close: DigestValue
tag_close: Reference
tag_close: SignedInfo
tag_open: SignatureValue
content: vhqZ3zpWq58OPRyYJdGsKw7JX+oEwYW2wPRpAIgobsC...
tag_close: SignatureValue
tag_open: KeyInfo
tag_open: X509Data
tag_open: X509Certificate
content: MIIDjzCCAnegAwIBAgIEF2/aITANBgkqhkiG9w...
tag_close: X509Certificate
tag_close: X509Data
tag_close: KeyInfo
tag_close: Signature
tag_close: pedRegistro
tag_close: iniciarRegistro
eof:

Reference: https://www.sef.sc.gov.br/arquivos_portal/servicos/159/Especificacao_de_Requisitos_do_DAF___versao_3.0.0.pdf

@kassane
Copy link
Author

kassane commented May 10, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment