|
Hi @twostraws, could you please help me parse the XML constant string? I need to find every attribute of the tag Link and put it in my data model. I've used your XMLParser class as coded below, but it can not even find the root element, which is DownloadMultiplo.
It prints out N/A |
|
It's because your XML is not valid. Specifically, the URLs in your
The parser chokes on You can see this if you add the following delegate method to the
Prints:
I'm honestly not sure how to solve this with the given XML. I tried replacing the raw Do you control this XML? Ampersands are allowed within CDATA sections, so something like this works:
I reformatted your XML and altered the root-level code to show some results and it looked good:
Printed:
|
|
Actually I do not control the XML format, it comes from a CVM (the Brazilian version of the SEC) API. It is strange because the Python XML parser works just fine with the original format. I'll try to parse it with Python, convert the data to a JSON format and then access it with Swift with my own API. Thanks a lot for your help! |
|
Python's parser must be more lenient than Swift's. I tried several online XML syntax checkers and they all choked at the same spot for the same reason. |
|
Hi @roosterboy, as a follow up in this topic. I can now parse correctlly the XML just adding the modifier - .replacingOccurrences(of: "&prot", with: "&prot") - to my string. The problem is that it returns a dictionary and my Documento Model is:
So I had to encode the dictionary to a JSON format and then decode it back to my data type.
It works well, printing the correct Documento type: Documento(id: 5E1582ED-C71C-4EEC-A8A9-05C50E23A247, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926177", documento: "IPE", ccvm: "22144", dataRef: "15/12/2021 23:59:59", frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Cancelado") Documento(id: CD008921-C9A7-4822-B132-83ED7EF4ADFA, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926363", documento: "IPE", ccvm: "50091", dataRef: "16/12/2021 23:59:59", frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado") Documento(id: 4C0796DE-4BEC-43D5-90B5-C3D2D77B93C2, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926364", documento: "IPE", ccvm: "53902", dataRef: "16/12/2021 23:59:59", frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado") Documento(id: 042FE16F-ADAF-44F9-B716-348FBC040B09, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926365", documento: "IPE", ccvm: "56154", dataRef: "16/12/2021 23:59:59", frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado") Documento(id: 6082A043-87FF-4180-8D40-CD4DC45ACC9F, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926366", documento: "IPE", ccvm: "55573", dataRef: "16/12/2021 23:59:59", frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado") Documento(id: FC850B0F-236B-418C-82D8-0A565051E805, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926367", documento: "IPE", ccvm: "55611", dataRef: "16/12/2021 23:59:59", frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado") Documento(id: 85872D19-CA2C-45C9-8A80-E19C23CA582A, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926368", documento: "IPE", ccvm: "54003", dataRef: "16/12/2021 23:59:59", frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado") Documento(id: C590FF8A-A81A-4416-A53B-AE333AF37899, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926369", documento: "IPE", ccvm: "53139", dataRef: "16/12/2021 23:59:59", frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado") Documento(id: C8971E3D-6DB8-4C96-B04B-91E4D6E2B5AD, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926370", documento: "IPE", ccvm: "54810", dataRef: "16/12/2021 23:59:59", frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado") Documento(id: EF5C4E5A-C4E9-4A79-B71A-485B889A86E0, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926371", documento: "IPE", ccvm: "53155", dataRef: "16/12/2021 23:59:59", frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado") The new problem is: when I try to return the result of the fetch method I don't know exactly what is going wrong:
I receive this fail message: Missing return in a function expected to return '[Documento]' Thanks for the help! |
|
You have a You need to put something like |
|
@roosterboy, I have solved most of my problems parsing the XML document. But there is a new one related to SwiftUI. My document model is:
The xml document is: (as an example) <DownloadMultiplo DataSolicitada="22/12/2021 00:00" TipoDocumento="IPE" DataConsulta="26/12/2021 22:44"> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=925719" Documento="IPE" ccvm="20516" DataRef="13/12/2021 23:59:59" FrmDtRef="dd/mm/aaaa" Categoria="Documentos de Oferta de Distribuição Pública" Tipo="Prospecto de Distribuição Pública" Especie="Prospecto Preliminar" Situacao="Cancelado"> <Assuntos Quantidade="1"> <Assunto> Prospecto preliminar da oferta pública de debêntures incentivadas </Assunto> </Assuntos> </Link> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=925720" Documento="IPE" ccvm="20516" DataRef="13/12/2021 23:59:59" FrmDtRef="dd/mm/aaaa" Categoria="Documentos de Oferta de Distribuição Pública" Tipo="Aviso ao Mercado" Especie=" " Situacao="Cancelado"> <Assuntos Quantidade="1"> <Assunto> Prospecto preliminar da oferta pública de debêntures incentivadas </Assunto> </Assuntos> </Link> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926544" Documento="IPE" ccvm="26492" DataRef="31/12/2021 23:59:59" FrmDtRef="mm/aaaa" Categoria="Valores Mobiliários negociados e detidos (art. 11 da Instr. CVM nº 358)" Tipo="Posição Consolidada" Especie=" " Situacao="Cancelado"> <Assuntos Quantidade="0"/> </Link> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926834" Documento="IPE" ccvm="26492" DataRef="31/12/2021 23:59:59" FrmDtRef="mm/aaaa" Categoria="Valores Mobiliários negociados e detidos (art. 11 da Instr. CVM nº 358)" Tipo="Posição Consolidada" Especie=" " Situacao="Cancelado"> <Assuntos Quantidade="0"/> </Link> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=927115" Documento="IPE" ccvm="20010" DataRef="04/12/2021 08:00:59" FrmDtRef="dd/mm/aaaa hh:mm" Categoria="Reunião da Administração" Tipo="Conselho de Administração" Especie="Ata" Situacao="Cancelado"> <Assuntos Quantidade="1"> <Assunto>Aprovação da Aquisição da Echoenergia</Assunto> </Assuntos> </Link> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928060" Documento="IPE" ccvm="53503" DataRef="22/12/2021 23:59:59" FrmDtRef="dd/mm/aaaa" Categoria="Comunicado ao Mercado" Tipo="Outros Comunicados Não Considerados Fatos Relevantes" Especie=" " Situacao="Liberado"> <Assuntos Quantidade="1"> <Assunto>4</Assunto> </Assuntos> </Link> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928061" Documento="IPE" ccvm="51403" DataRef="22/12/2021 23:59:59" FrmDtRef="dd/mm/aaaa" Categoria="Comunicado ao Mercado" Tipo="Outros Comunicados Não Considerados Fatos Relevantes" Especie=" " Situacao="Liberado"> <Assuntos Quantidade="1"> <Assunto>4</Assunto> </Assuntos> </Link> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928062" Documento="IPE" ccvm="14311" DataRef="16/12/2021 16:30:59" FrmDtRef="dd/mm/aaaa hh:mm" Categoria="Reunião da Administração" Tipo="Conselho de Administração" Especie="Ata" Situacao="Liberado"> <Assuntos Quantidade="1"> <Assunto> Aprovada a participação da Copel no Leilão de Transmissão ANEEL 002/2021 </Assunto> </Assuntos> </Link> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928063" Documento="IPE" ccvm="25640" DataRef="22/12/2021 23:59:59" FrmDtRef="dd/mm/aaaa" Categoria="Fato Relevante" Tipo=" " Especie=" " Situacao="Liberado"> <Assuntos Quantidade="1"> <Assunto>Acordo de Investimento e Suprimento de Energia</Assunto> </Assuntos> </Link> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928064" Documento="IPE" ccvm="11592" DataRef="22/12/2021 23:59:59" FrmDtRef="dd/mm/aaaa" Categoria="Fato Relevante" Tipo=" " Especie=" " Situacao="Liberado"> <Assuntos Quantidade="1"> <Assunto>Acordo de Investimentos Unipar e AES</Assunto> </Assuntos> </Link> <Link url="http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928065" Documento="IPE" ccvm="24740" DataRef="21/12/2021 09:30:59" FrmDtRef="dd/mm/aaaa hh:mm" Categoria="Reunião da Administração" Tipo="Conselho de Administração" Especie="Sumário das Decisões" Situacao="Liberado"> <Assuntos Quantidade="3"> <Assunto> Aprovada a antecipação de parcela de Juros sobre o Capital Próprio - JCP, em substituição aos dividendos do exercício de 2021, ao acionista com posição em 31.12.2021, de acordo com a Lei Federal nº 9. </Assunto> <Assunto> Aprovada a revisão do Plano de Negócios da Copel Geração e Transmissão S.A. para o período 2022-2026 </Assunto> <Assunto> recebido reporte das atividades do Subcomitê de Sustentabilidade da GeT e determinou que reportes do subcomitê sejam periodicamente trazidos ao Colegiado </Assunto> </Assuntos> </Link> </DownloadMultiplo> My view model is:
The decoded data is: [EvolveApp.Documento(id: 874F4915-C3BA-4507-B49E-87E2CD34E266, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=925719", documento: "IPE", ccvm: "20516", dataRef: 2021-12-14 02:59:59 +0000, frmDtRef: "dd/mm/aaaa", categoria: "Documentos de Oferta de Distribuição Pública", tipo: "Prospecto de Distribuição Pública", especie: "Prospecto Preliminar", situacao: "Cancelado", assuntos: EvolveApp.Documento.Assuntos(assunto: Optional(["Prospecto preliminar da oferta pública de debêntures incentivadas"]))), EvolveApp.Documento(id: 8AFB67B7-C922-4018-BE3F-78DC687C7ECF, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=925720", documento: "IPE", ccvm: "20516", dataRef: 2021-12-14 02:59:59 +0000, frmDtRef: "dd/mm/aaaa", categoria: "Documentos de Oferta de Distribuição Pública", tipo: "Aviso ao Mercado", especie: " ", situacao: "Cancelado", assuntos: EvolveApp.Documento.Assuntos(assunto: Optional(["Prospecto preliminar da oferta pública de debêntures incentivadas"]))), EvolveApp.Documento(id: 604D5CCB-D5F4-4CB4-B2A8-AB6B176A4DAA, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926544", documento: "IPE", ccvm: "26492", dataRef: 2022-01-01 02:59:59 +0000, frmDtRef: "mm/aaaa", categoria: "Valores Mobiliários negociados e detidos (art. 11 da Instr. CVM nº 358)", tipo: "Posição Consolidada", especie: " ", situacao: "Cancelado", assuntos: EvolveApp.Documento.Assuntos(assunto: nil)), EvolveApp.Documento(id: C717723F-749C-4A20-A3C0-A433F45CDA10, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=926834", documento: "IPE", ccvm: "26492", dataRef: 2022-01-01 02:59:59 +0000, frmDtRef: "mm/aaaa", categoria: "Valores Mobiliários negociados e detidos (art. 11 da Instr. CVM nº 358)", tipo: "Posição Consolidada", especie: " ", situacao: "Cancelado", assuntos: EvolveApp.Documento.Assuntos(assunto: nil)), EvolveApp.Documento(id: 12687ADA-474A-442B-B2D9-53FD6504A90C, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=927115", documento: "IPE", ccvm: "20010", dataRef: 2021-12-04 11:00:59 +0000, frmDtRef: "dd/mm/aaaa hh:mm", categoria: "Reunião da Administração", tipo: "Conselho de Administração", especie: "Ata", situacao: "Cancelado", assuntos: EvolveApp.Documento.Assuntos(assunto: Optional(["Aprovação da Aquisição da Echoenergia"]))), EvolveApp.Documento(id: 5C1189E7-B55A-4655-B1FA-FCC6AF8B3A16, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928060", documento: "IPE", ccvm: "53503", dataRef: 2021-12-23 02:59:59 +0000, frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado", assuntos: EvolveApp.Documento.Assuntos(assunto: Optional(["4"]))), EvolveApp.Documento(id: ECA702C1-2E5B-40A4-8836-AEAFE65EE5E5, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928061", documento: "IPE", ccvm: "51403", dataRef: 2021-12-23 02:59:59 +0000, frmDtRef: "dd/mm/aaaa", categoria: "Comunicado ao Mercado", tipo: "Outros Comunicados Não Considerados Fatos Relevantes", especie: " ", situacao: "Liberado", assuntos: EvolveApp.Documento.Assuntos(assunto: Optional(["4"]))), EvolveApp.Documento(id: C6C362C6-77FF-4263-BC81-928F36E431B8, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928062", documento: "IPE", ccvm: "14311", dataRef: 2021-12-16 19:30:59 +0000, frmDtRef: "dd/mm/aaaa hh:mm", categoria: "Reunião da Administração", tipo: "Conselho de Administração", especie: "Ata", situacao: "Liberado", assuntos: EvolveApp.Documento.Assuntos(assunto: Optional(["Aprovada a participação da Copel no Leilão de Transmissão ANEEL 002/2021"]))), EvolveApp.Documento(id: 58FBCEF2-E67F-417E-8197-CCF972AD4340, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928063", documento: "IPE", ccvm: "25640", dataRef: 2021-12-23 02:59:59 +0000, frmDtRef: "dd/mm/aaaa", categoria: "Fato Relevante", tipo: " ", especie: " ", situacao: "Liberado", assuntos: EvolveApp.Documento.Assuntos(assunto: Optional(["Acordo de Investimento e Suprimento de Energia"]))), EvolveApp.Documento(id: 5199FEC7-D55C-40C0-9B13-3BFEB94C7F48, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928064", documento: "IPE", ccvm: "11592", dataRef: 2021-12-23 02:59:59 +0000, frmDtRef: "dd/mm/aaaa", categoria: "Fato Relevante", tipo: " ", especie: " ", situacao: "Liberado", assuntos: EvolveApp.Documento.Assuntos(assunto: Optional(["Acordo de Investimentos Unipar e AES"]))), EvolveApp.Documento(id: 1905FF9A-9D91-430E-AEC9-3F3FC8035567, url: "http://siteempresas.bovespa.com.br/DWL/FormDetalheDownload.asp?site=C&prot=928065", documento: "IPE", ccvm: "24740", dataRef: 2021-12-21 12:30:59 +0000, frmDtRef: "dd/mm/aaaa hh:mm", categoria: "Reunião da Administração", tipo: "Conselho de Administração", especie: "Sumário das Decisões", situacao: "Liberado", assuntos: EvolveApp.Documento.Assuntos(assunto: Optional(["Aprovada a antecipação de parcela de Juros sobre o Capital Próprio - JCP, em substituição aos dividendos do exercício de 2021, ao acionista com posição em 31.12.2021, de acordo com a Lei Federal nº 9.", "Aprovada a revisão do Plano de Negócios da Copel Geração e Transmissão S.A. para o período 2022-2026", "recebido reporte das atividades do Subcomitê de Sustentabilidade da GeT e determinou que reportes do subcomitê sejam periodicamente trazidos ao Colegiado"]))) and my view is:
The problem is: I can not display the text coming from the Assuntos (which means Subjects) from the documents. When i try: Text(item.assuntos), Xcode tells me that this property is defined on Documento, and may not be available in this context. I'd like to loop over all the assuntos (subjects) and present them below item.categoria. Do you have any idea where I am making a mistake? Thanks a lot! |
|
If you hover your mouse over item.assuntos and hold down the option key and click, it will tell you what TYPE item.assuntos is. It looks like item.assuntos is a struct of type Assuntos. This struct holds an optional array of strings defined as:
I think you are failing on the line:
Maybe try:
But this is just debugging. You'll need to provide extra logic if there are several items in the assunto array. |
|
Also, just a bit of advice regarding your You currently have it in a computed property, which means that every time you call it a new
then the |
|
|
|
@roosterboy, what is the best way in Swift (and iOS development) to join two or more datasets? I mean, in my XML dataset above , I have this ccvm field, which is basically an ID of a publicly traded company listed in the brazilian stock exchange. I have another dataset that have a primary key that corresponds to the ccvm field, with all the information about the company, like names, sectors, ticker symbols, etc. So basically I have a one-to-many relationship, with one company reporting many documents. I'm not sure if a use Core Data to model this relationship, store these datasets on it, or if there is another solution for this task. Tks! |
SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.