Skip to content

Instantly share code, notes, and snippets.

@fitomad fitomad/swiftxml_delegate.swift Secret
Created Mar 26, 2020

Embed
What would you like to do?
public class BookParser: NSObject
{
...
/// Parser de XML
private var parser: XMLParser
/// Tag en la que nos encontramos en un momento dado
private var actualElement: String
/// El parser que está *activo* en este momento
private var actualParser: XMLParserDelegate?
/**
Parseamos el XML que viene contenido en el `Data`
*/
public init(data: Data)
{
...
self.parser = XMLParser(data: data)
super.init()
// Delegado
self.parser.delegate = self
// Empezamos a parsear
parser.parse()
}
}
// MARK: - XMLParserDelegate protocol implementation -
extension BookParser: XMLParserDelegate
{
/// Etiqueta de **apertura** de un elemento XML.
/// Aquí podremos recuperar los atributos de la etiqueta.
public func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:])
{
...
}
/// Etiqueta de **cierre** de un elemento XML
public func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
...
}
/// Contenido de una etiqueta
public func parser(_ parser: XMLParser, foundCharacters string: String)
{
...
}
/// Etiquetas CDATA
public func parser(_ parser: XMLParser, foundCDATA CDATABlock: Data)
{
...
}
/// Recibimos los errores durante el parseo
public func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error)
{
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.