XML parsing on Linux, with Swift 3.1

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.

Foundation (NS)XML parsing is a little buggy and incomplete, check out github.com/kelvin13/swiftxml for a pure swift implementation

···

On Sep 5, 2017, at 1:01 AM, Georgios Moschovitis via swift-users <swift-users@swift.org> 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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

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.parse()

···

On 5 Sep 2017, at 9:01 AM, Georgios Moschovitis <george.moschovitis@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.

Foundation (NS)XML parsing is a little buggy and incomplete, check out github.com/kelvin13/swiftxml for a pure swift implementation

Your implementation seems to be Swift4 only?

-g.

PS: Also, I am a bit put-off by some C/C++ coding conventions (e.g. snake_case)

···

On Sep 5, 2017, at 1:01 AM, Georgios Moschovitis via swift-users <swift-users@swift.org> 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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

1 Like

XMLParser is a SAX-based parser, which is optimized for memory-usage. There's also XMLDocument, which is DOM-based and is optimized for developer convenience. If the XML files you're reading are small, i.e. few megabytes, DOM-based is usually the easiest way to handle such documents. XMLDocument is also implemented in Swift-Foundation, but I found that it was lacking too much functionality (e.g. namespacing). The situation might be different now -- you could check the source for `NSUnimplemented()`.

Other options is to link libxml2 (C library), however that requires much more effort on your end -- it doesn't expose high-level API's such as XMLDocument.

--Bouke

···

On 2017-09-05 06:01:55 +0000, Georgios Moschovitis via swift-users said:

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@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

The CI builds on Swift 4, i believe it should work fine on master too but
it’s not tested there. Also I don’t think snake_case is C++ convention,
i’ve seen both. I use snake_case because camelCase looks too similar to
UpperCamelCase which is reserved for type names.

···

On Tue, Sep 5, 2017 at 3:43 AM, Georgios Moschovitis < george.moschovitis@icloud.com> wrote:

>
> Foundation (NS)XML parsing is a little buggy and incomplete, check out
github.com/kelvin13/swiftxml for a pure swift implementation

Your implementation seems to be Swift4 only?

-g.

PS: Also, I am a bit put-off by some C/C++ coding conventions (e.g.
snake_case)

>
>> On Sep 5, 2017, at 1:01 AM, Georgios Moschovitis via swift-users < > swift-users@swift.org> 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@swift.org
>> https://lists.swift.org/mailman/listinfo/swift-users

Thank you, I wasn’t aware of XMLDocument.
I managed to get XMLParser to work for my use case though…

-g.

FWIW, I also prefer snake_case, but camelCase is unfortunately the standard convention in Swift so I use that.
Also, I avoid using libraries that don’t follow standard conventions.

-g.