Skip to content

tayloraswift/swiftxml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftXML

Platforms Build Issues Language License KK25

Lightweight XML parsing in pure Swift 4. No Foundation. No dependencies.

SwiftXML is an event-driven XML parser intended to replace the Foundation (NS)XMLParser class. SwiftXML works on Linux. SwiftXML doesn’t wrap anything. It parses XML directly, character by character. And the API is simple and easy to use:

protocol XMLParser
{
    mutating
    func handle_data(data:[Unicode.Scalar])
    mutating
    func handle_tag_start(name:String, attributes:[String: String])
    mutating
    func handle_tag_empty(name:String, attributes:[String: String])
    mutating
    func handle_tag_end(name:String)
    mutating
    func handle_processing_instruction(target:String, data:[Unicode.Scalar])
    mutating
    func handle_error(_ message:String, line:Int, column:Int)
}

extension XMLParser
{
    mutating
    func parse(_ str:String)

    mutating
    func parse(path:String)
}

Import the package with

import XML

Supported XML features:

  • parse start, empty, and end tags, and XML data
  • unescape XML default entity references (<, >, ', ", &) in text data
  • unescape XML default entity references (<, >, ', ", &) in attribute data
  • unescape XML character references in text data
  • unescape XML character references in attribute data
  • parse XML processing instructions
  • parse XML comments
  • expose XML comments
  • parse XML CDATA sections
  • parse XML entity declarations
  • parse XML element type declarations
  • parse XML attribute list declarations
  • parse and validate XML namespaces
  • expose syntax error line and column numbers

SwiftXML will tokenize your XML string into tags and data. It does not build any tree structures.

See the tests.swift file for a usage example.