AEXML-CU 5.0.0

AEXML-CU 5.0.0

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Dec 2019
SPMSupports SPM

Maintained by Tanner Stokes, Tanner Stokes.



AEXML-CU 5.0.0

  • By
  • Tanner Stokes

AEXML-CU

Simple and lightweight XML parser written in Swift. A fork of AEXML.

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

Index

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.0pl-pds> encoding=utf-8pl-pds>?>
<animals>
    <cats>
        <cat breed=Siberianpl-pds> color=lightgraypl-pds>>Tinna</cat>
        <cat breed=Domesticpl-pds> color=darkgraypl-pds>>Rose</cat>
        <cat breed=Domesticpl-pds> color=yellowpl-pds>>Caesar</cat>
        <cat></cat>
    </cats>
    <dogs>
        <dog breed=Bull Terrierpl-pds> color=whitepl-pds>>Villy</dog>
        <dog breed=Bull Terrierpl-pds> color=whitepl-pds>>Spot</dog>
        <dog breed=Golden Retrieverpl-pds> color=yellowpl-pds>>Betty</dog>
        <dog breed=Miniature Schnauzerpl-pds> color=blackpl-pds>>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: examplepl-pds>, ofType: xmlpl-pds>),
    let data = try? Data(contentsOf: URL(fileURLWithPath: xmlPath))
else { return }

do { let xmlDoc = try AEXMLDocument(xml: data, options: options)

<span class="pl-c"><span class="pl-c">//</span> prints the same XML structure as original</span>

print(xmlDoc.xml)

<span class="pl-c"><span class="pl-c">//</span> prints cats, dogs</span>

for child in xmlDoc.root.children { print(child.name) }

<span class="pl-c"><span class="pl-c">//</span> prints Optional("Tinna") (first element)</span>

print(xmlDoc.root[catspl-pds>][catpl-pds>].value)

<span class="pl-c"><span class="pl-c">//</span> prints Tinna (first element)</span>

print(xmlDoc.root[catspl-pds>][catpl-pds>].string)

<span class="pl-c"><span class="pl-c">//</span> prints Optional("Kika") (last element)</span>

print(xmlDoc.root[dogspl-pds>][dogpl-pds>].last?.value)

<span class="pl-c"><span class="pl-c">//</span> prints Betty (3rd element)</span>

print(xmlDoc.root[dogspl-pds>].children[2].string)

<span class="pl-c"><span class="pl-c">//</span> prints Tinna, Rose, Caesar</span>

if let cats = xmlDoc.root[catspl-pds>][catpl-pds>].all { for cat in cats { if let name = cat.value { print(name) } } }

<span class="pl-c"><span class="pl-c">//</span> prints Villy, Spot</span>

for dog in xmlDoc.root[dogspl-pds>][dogpl-pds>].all! { if let color = dog.attributes[colorpl-pds>] { if color == whitepl-pds> { print(dog.string) } } }

<span class="pl-c"><span class="pl-c">//</span> prints Tinna</span>

if let cats = xmlDoc.root[catspl-pds>][catpl-pds>].all(withValue: Tinnapl-pds>) { for cat in cats { print(cat.string) } }

<span class="pl-c"><span class="pl-c">//</span> prints Caesar</span>

if let cats = xmlDoc.root[catspl-pds>][catpl-pds>].all(withAttributes: [breedpl-pds> : Domesticpl-pds>, colorpl-pds> : yellowpl-pds>]) { for cat in cats { print(cat.string) } }

<span class="pl-c"><span class="pl-c">//</span> prints 4</span>

print(xmlDoc.root[catspl-pds>][catpl-pds>].count)

<span class="pl-c"><span class="pl-c">//</span> prints Siberian</span>

print(xmlDoc.root[catspl-pds>][catpl-pds>].attributes[breedpl-pds>]!)

<span class="pl-c"><span class="pl-c">//</span> prints &lt;cat breed="Siberian" color="lightgray"&gt;Tinna&lt;/cat&gt;</span>

print(xmlDoc.root[catspl-pds>][catpl-pds>].xmlCompact)

<span class="pl-c"><span class="pl-c">//</span> prints Optional(AEXML.AEXMLError.elementNotFound)</span>

print(xmlDoc[NotExistingElementpl-pds>].error) } catch { print(pl-pse>\(pl-s1>errorpl-pse>pl-s1>)pl-pds>) }

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.0pl-pds> encoding=utf-8pl-pds>?>
<soap:Envelope xmlns:xsd=http://www.w3.org/2001/XMLSchemapl-pds> xmlns:xsi=http://www.w3.org/2001/XMLSchema-instancepl-pds>>
  <soap:Header>
    <m:Trans xmlns:m=http://www.w3schools.com/transaction/pl-pds> soap:mustUnderstand=1pl-pds>>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:xsipl-pds> : http://www.w3.org/2001/XMLSchema-instancepl-pds>, xmlns:xsdpl-pds> : http://www.w3.org/2001/XMLSchemapl-pds>]
let envelope = soapRequest.addChild(name: soap:Envelopepl-pds>, attributes: attributes)
let header = envelope.addChild(name: soap:Headerpl-pds>)
let body = envelope.addChild(name: soap:Bodypl-pds>)
header.addChild(name: m:Transpl-pds>, value: 234pl-pds>, attributes: [xmlns:mpl-pds> : http://www.w3schools.com/transaction/pl-pds>, soap:mustUnderstandpl-pds> : 1pl-pds>])
let getStockPrice = body.addChild(name: m:GetStockPricepl-pds>)
getStockPrice.addChild(name: m:StockNamepl-pds>, value: AAPLpl-pds>)

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

Installation

License

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