[swift-users] XML parsing on Linux, with Swift 3.1

Saagar Jha saagar at saagarjha.com
Tue Sep 5 13:21:30 CDT 2017


Saagar Jha

> On Sep 5, 2017, at 01:19, Georgios Moschovitis via swift-users <swift-users at swift.org> wrote:
> 
> Indeed it works, but I don’t get it.
> What’s the difference?
> 
> -g.
> 
> PS: Btw, my original code was giving `seg-fault: 11` even on macOS.
> 
>> On 5 Sep 2017, at 10:53 AM, CK TUNG via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>> This revised code, as below, works without segmentation fault
>> 
>> import Foundation
>> 
>> class ParserDelegate: NSObject, XMLParserDelegate {
>>     
>>     func startParsing(_ xml:String) {
>>         let data = xml.data(using: .utf8)!
>>         let xmlParser = XMLParser(data: data)
>>         xmlParser.delegate = self
>>         xmlParser.parse()
>>     }
>>     
>>     func parserDidStartDocument(_ parser: XMLParser) {
>>         print("Starting document")
>>     }
>>     
>>     func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
>>         print("*** \(elementName)")
>>     }
>> }
>> 
>> let xml = "<root><title name=\"test\">George</title></root>"
>> let test = ParserDelegate()
>> test.startParsing(xml)
>> 
>> On Sep 05, 2017, at 02:24 PM, Georgios Moschovitis via swift-users <swift-users at swift.org <mailto:swift-users at swift.org>> wrote:
>> 
>>> As an example, this SegFaults:
>>> 
>>> import Foundation
>>> 
>>> class ParserDelegate: NSObject, XMLParserDelegate {
>>> func parserDidStartDocument(_ parser: XMLParser) {
>>> print("Starting document")
>>> }
>>> 
>>> func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) {
>>> print("*** \(elementName)")
>>> }
>>> }
>>> 
>>> let xml = "<root><title name=\"test\">George</title></root>"
>>> let data = xml.data(using: .utf8)!
>>> let xmlParser = XMLParser(data: data)
>>> xmlParser.delegate = ParserDelegate()

XMLParser’s delegate is unowned, so it’s being deallocated when you exit the current scope. Hold on to it with a strong reference:

let delegate = ParserDelegate()
xmlParser.delegate = delegate

>>> xmlParser.parse()
>>> 
>>>> On 5 Sep 2017, at 9:01 AM, Georgios Moschovitis <george.moschovitis at icloud.com <mailto:george.moschovitis at icloud.com>> wrote:
>>>> 
>>>> Hi,
>>>> 
>>>> I would like to parse an RSS feed using Swift 3.1 on Linux.
>>>> I tried to use Foundations’s XML but I only managed to get segmentation faults.
>>>> Is this supposed to work on Linux? I have only seen examples on iOS.
>>>> 
>>>> Apart from that a quick search didn’t reveal any useful XML parsing library compatible with Linux.
>>>> 
>>>> Any suggestions?
>>>> 
>>>> -g.
>>> 
>>> _______________________________________________
>>> swift-users mailing list
>>> swift-users at swift.org <mailto:swift-users at swift.org>
>>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
>> _______________________________________________
>> swift-users mailing list
>> swift-users at swift.org <mailto:swift-users at swift.org>
>> https://lists.swift.org/mailman/listinfo/swift-users <https://lists.swift.org/mailman/listinfo/swift-users>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170905/f73c50df/attachment.html>


More information about the swift-users mailing list