2 **Simple and lightweight XML parser written in Swift**
4 [![Language Swift 4.0](https://img.shields.io/badge/Language-Swift%204.0-orange.svg?style=flat)](https://swift.org)
5 [![Platforms iOS | watchOS | tvOS | OSX](https://img.shields.io/badge/Platforms-iOS%20%7C%20watchOS%20%7C%20tvOS%20%7C%20OS%20X-lightgray.svg?style=flat)](http://www.apple.com)
6 [![License MIT](https://img.shields.io/badge/License-MIT-lightgrey.svg?style=flat)](https://github.com/tadija/AEXML/blob/master/LICENSE)
8 [![CocoaPods Version](https://img.shields.io/cocoapods/v/AEXML.svg?style=flat)](https://cocoapods.org/pods/AEXML)
9 [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-brightgreen.svg?style=flat)](https://github.com/Carthage/Carthage)
10 [![Swift Package Manager compatible](https://img.shields.io/badge/Swift%20Package%20Manager-compatible-brightgreen.svg)](https://github.com/apple/swift-package-manager)
12 > This is not a robust full featured XML parser, but rather simple,
13 lightweight and easy to use utility for casual XML handling.
16 - [Features](#features)
18 - [Read XML](#read-xml)
19 - [Write XML](#write-xml)
20 - [Installation](#installation)
25 - **Write XML** string
26 - Covered with [unit tests](https://github.com/tadija/AEXML/blob/master/Tests/AEXMLTests.swift)
27 - Covered with inline docs
32 Let's say this is some XML string you picked up somewhere and made a variable `data: Data` from that.
35 <?xml version="1.0" encoding="utf-8"?>
38 <cat breed="Siberian" color="lightgray">Tinna</cat>
39 <cat breed="Domestic" color="darkgray">Rose</cat>
40 <cat breed="Domestic" color="yellow">Caesar</cat>
44 <dog breed="Bull Terrier" color="white">Villy</dog>
45 <dog breed="Bull Terrier" color="white">Spot</dog>
46 <dog breed="Golden Retriever" color="yellow">Betty</dog>
47 <dog breed="Miniature Schnauzer" color="black">Kika</dog>
52 This is how you can use AEXML for working with this data:
53 (for even more examples, look at the unit tests code included in project)
57 let xmlPath = Bundle.main.path(forResource: "example", ofType: "xml"),
58 let data = try? Data(contentsOf: URL(fileURLWithPath: xmlPath))
62 let xmlDoc = try AEXMLDocument(xml: data, options: options)
64 // prints the same XML structure as original
68 for child in xmlDoc.root.children {
72 // prints Optional("Tinna") (first element)
73 print(xmlDoc.root["cats"]["cat"].value)
75 // prints Tinna (first element)
76 print(xmlDoc.root["cats"]["cat"].string)
78 // prints Optional("Kika") (last element)
79 print(xmlDoc.root["dogs"]["dog"].last?.value)
81 // prints Betty (3rd element)
82 print(xmlDoc.root["dogs"].children[2].string)
84 // prints Tinna, Rose, Caesar
85 if let cats = xmlDoc.root["cats"]["cat"].all {
87 if let name = cat.value {
94 for dog in xmlDoc.root["dogs"]["dog"].all! {
95 if let color = dog.attributes["color"] {
103 if let cats = xmlDoc.root["cats"]["cat"].all(withValue: "Tinna") {
110 if let cats = xmlDoc.root["cats"]["cat"].all(withAttributes: ["breed" : "Domestic", "color" : "yellow"]) {
117 print(xmlDoc.root["cats"]["cat"].count)
120 print(xmlDoc.root["cats"]["cat"].attributes["breed"]!)
122 // prints <cat breed="Siberian" color="lightgray">Tinna</cat>
123 print(xmlDoc.root["cats"]["cat"].xmlCompact)
125 // prints Optional(AEXML.AEXMLError.elementNotFound)
126 print(xmlDoc["NotExistingElement"].error)
134 Let's say this is some SOAP XML request you need to generate.
135 Well, you could just build ordinary string for that?
138 <?xml version="1.0" encoding="utf-8"?>
139 <soap:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
141 <m:Trans xmlns:m="http://www.w3schools.com/transaction/" soap:mustUnderstand="1">234</m:Trans>
145 <m:StockName>AAPL</m:StockName>
151 Yes, but, you can also do it in a more structured and elegant way with AEXML:
154 // create XML Document
155 let soapRequest = AEXMLDocument()
156 let attributes = ["xmlns:xsi" : "http://www.w3.org/2001/XMLSchema-instance", "xmlns:xsd" : "http://www.w3.org/2001/XMLSchema"]
157 let envelope = soapRequest.addChild(name: "soap:Envelope", attributes: attributes)
158 let header = envelope.addChild(name: "soap:Header")
159 let body = envelope.addChild(name: "soap:Body")
160 header.addChild(name: "m:Trans", value: "234", attributes: ["xmlns:m" : "http://www.w3schools.com/transaction/", "soap:mustUnderstand" : "1"])
161 let getStockPrice = body.addChild(name: "m:GetStockPrice")
162 getStockPrice.addChild(name: "m:StockName", value: "AAPL")
164 // prints the same XML structure as original
165 print(soapRequest.xml)
170 - [Swift Package Manager](https://swift.org/package-manager/):
173 .Package(url: "https://github.com/tadija/AEXML.git", majorVersion: 4)
176 - [Carthage](https://github.com/Carthage/Carthage):
179 github "tadija/AEXML"
182 - [CocoaPods](http://cocoapods.org/):
189 AEXML is released under the MIT license. See [LICENSE](LICENSE) for details.