• Stars
    star
    996
  • Rank 46,003 (Top 1.0 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 10 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Swift minion for simple and lightweight XML parsing

Swift 5.3 Platforms iOS | watchOS | tvOS | macOS CocoaPods Carthage Swift Package Manager License MIT

AEXML

Swift minion for simple and lightweight XML parsing

I made this for personal use, but feel free to use it or contribute. For more examples check out Sources and Tests.

Index

Intro

This is not a robust full featured XML parser, but rather simple, lightweight and easy to use utility for casual XML handling.

Features

  • Read XML data
  • Write XML string
  • Covered with unit tests
  • Covered with inline docs

Usage

Read XML

Let's say this is some XML string you picked up somewhere and made a variable data: Data from that.

<?xml version="1.0" encoding="utf-8"?>
<animals>
    <cats>
        <cat breed="Siberian" color="lightgray">Tinna</cat>
        <cat breed="Domestic" color="darkgray">Rose</cat>
        <cat breed="Domestic" color="yellow">Caesar</cat>
        <cat></cat>
    </cats>
    <dogs>
        <dog breed="Bull Terrier" color="white">Villy</dog>
        <dog breed="Bull Terrier" color="white">Spot</dog>
        <dog breed="Golden Retriever" color="yellow">Betty</dog>
        <dog breed="Miniature Schnauzer" color="black">Kika</dog>
    </dogs>
</animals>

This is how you can use AEXML for working with this data:
(for even more examples, look at the unit tests code included in project)

guard let
    let xmlPath = Bundle.main.path(forResource: "example", ofType: "xml"),
    let data = try? Data(contentsOf: URL(fileURLWithPath: xmlPath))
else { return }

do {
    let xmlDoc = try AEXMLDocument(xml: data, options: options)
        
    // prints the same XML structure as original
    print(xmlDoc.xml)
    
    // prints cats, dogs
    for child in xmlDoc.root.children {
        print(child.name)
    }
    
    // prints Optional("Tinna") (first element)
    print(xmlDoc.root["cats"]["cat"].value)
    
    // prints Tinna (first element)
    print(xmlDoc.root["cats"]["cat"].string)
    
    // prints Optional("Kika") (last element)
    print(xmlDoc.root["dogs"]["dog"].last?.value)
    
    // prints Betty (3rd element)
    print(xmlDoc.root["dogs"].children[2].string)
    
    // prints Tinna, Rose, Caesar
    if let cats = xmlDoc.root["cats"]["cat"].all {
        for cat in cats {
            if let name = cat.value {
                print(name)
            }
        }
    }
    
    // prints Villy, Spot
    for dog in xmlDoc.root["dogs"]["dog"].all! {
        if let color = dog.attributes["color"] {
            if color == "white" {
                print(dog.string)
            }
        }
    }
    
    // prints Tinna
    if let cats = xmlDoc.root["cats"]["cat"].all(withValue: "Tinna") {
        for cat in cats {
            print(cat.string)
        }
    }
    
    // prints Caesar
    if let cats = xmlDoc.root["cats"]["cat"].all(withAttributes: ["breed" : "Domestic", "color" : "yellow"]) {
        for cat in cats {
            print(cat.string)
        }
    }
    
    // prints 4
    print(xmlDoc.root["cats"]["cat"].count)
    
    // prints Siberian
    print(xmlDoc.root["cats"]["cat"].attributes["breed"]!)
    
    // prints <cat breed="Siberian" color="lightgray">Tinna</cat>
    print(xmlDoc.root["cats"]["cat"].xmlCompact)
    
    // prints Optional(AEXML.AEXMLError.elementNotFound)
    print(xmlDoc["NotExistingElement"].error)
}
catch {
    print("\(error)")
}

Write XML

Let's say this is some SOAP XML request you need to generate.
Well, you could just build ordinary string for that?

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <soap:Header>
    <m:Trans xmlns:m="http://www.w3schools.com/transaction/" soap:mustUnderstand="1">234</m:Trans>
  </soap:Header>
  <soap:Body>
    <m:GetStockPrice>
      <m:StockName>AAPL</m:StockName>
    </m:GetStockPrice>
  </soap:Body>
</soap:Envelope>

Yes, but, you can also do it in a more structured and elegant way with AEXML:

// create XML Document
let soapRequest = AEXMLDocument()
let attributes = ["xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd" : "http://www.w3.org/2001/XMLSchema"]
let envelope = soapRequest.addChild(name: "soap:Envelope", attributes: attributes)
let header = envelope.addChild(name: "soap:Header")
let body = envelope.addChild(name: "soap:Body")
header.addChild(name: "m:Trans", value: "234", attributes: ["xmlns:m" : "http://www.w3schools.com/transaction/", "soap:mustUnderstand" : "1"])
let getStockPrice = body.addChild(name: "m:GetStockPrice")
getStockPrice.addChild(name: "m:StockName", value: "AAPL")

// prints the same XML structure as original
print(soapRequest.xml)

Or perhaps like this, using result builders (see #186):

@AEXMLDocumentBuilder
private func buildSoapEnvelope(
    for action: String,
    in serviceType: String,
    with parameters: [String: String] = [:]
) -> AEXMLDocument {
    AEXMLElement("s:Envelope", attributes: [
        "xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/",
        "s:encodingStyle": "http://schemas.xmlsoap.org/soap/encoding/"
    ]) {
        AEXMLElement("s:Body") {
            AEXMLElement("s:\(action)", attributes: [
                "xmlns:u": serviceType
            ]) {
                for parameter in parameters {
                    AEXMLElement(
                        name: parameter.key,
                        value: parameter.value
                    )
                }
            }
        }
    }
}

Installation

License

AEXML is released under the MIT license. See LICENSE for details.

More Repositories

1

AEIconizer

Sketch plugin for automatic scaling of iOS app icon artwork in all needed sizes
440
star
2

AEFlowchart

Sketch plugin for fast and convenient creation of flowcharts
377
star
3

xcode-project-renamer

Swift script for renaming Xcode project
Swift
354
star
4

AERecord

Super awesome Swift minion for Core Data (iOS, macOS, tvOS)
Swift
302
star
5

AEAccordion

Simple and lightweight UITableViewController with accordion effect (expand / collapse cells)
Swift
211
star
6

AEConsole

Customizable Console UI overlay with debug log on top of your iOS App
Swift
148
star
7

AEConicalGradient

Conical (angular) gradient for iOS written in Swift
Swift
83
star
8

TouchDemo

Demo from WWDC 2014 Session 235 - Advanced Scrollviews and Touch Handling Techniques
Swift
38
star
9

AELog

Simple, lightweight and flexible debug logging framework written in Swift
Swift
32
star
10

AEViewModel

Swift minion for convenient creation of tables and collection views
Swift
18
star
11

AEImage

Adaptive image viewer for iOS (with support for zoom, gyro motion and infinite scroll)
Swift
17
star
12

swift-greenfield

greenfield swift app project
Swift
12
star
13

AEAppVersion

Simple and Lightweight App Version Tracking for iOS written in Swift
Swift
12
star
14

AECoreDataUI

Super awesome Core Data driven UI for iOS written in Swift
Swift
11
star
15

AEDotFiles

From vanilla to personalized macOS in less than 30 minutes
Shell
11
star
16

bitrise-step-xcode-project-info

Bitrise step which extracts Xcode project information to environment variables
Shell
9
star
17

AETransition

Custom transitions for iOS - simple yet powerful
Swift
4
star
18

AESound

Convenience API for playing iOS system sounds
Swift
3
star
19

SwiftLook

Swift file viewer for iOS - it's like a QuickLook for Swift files
Swift
3
star
20

AECli

Swift package for making simple command line tools
Swift
3
star
21

AENetwork

Simple and lightweight networking in Swift
Swift
2
star
22

cs193p-matchismo

CS193p Winter 2013 - Matchismo - Card matching game
Objective-C
2
star
23

AECoreDataDemo

Demo project for AERecord and AECoreDataUI
Swift
2
star
24

swift-minions

Single-file helpers written in Swift, reusable across multiple projects
Swift
2
star
25

mappable

Swift package for simple and lightweight models mapping (with JSON support out of the box)
Swift
2
star
26

ae

command line personal assistant
Swift
1
star
27

AETool

Swift package for driving "ae" - command line personal assistant
Swift
1
star