AEXML-CU 5.0.0

AEXML-CU 5.0.0

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最后发布日期2019年12月
SPM支持 SPM

Tanner StokesTanner Stokes 维护。



AEXML-CU 5.0.0

  • 作者:
  • Tanner Stokes

AEXML-CU

使用 Swift 编写的简单且轻量级的 XML 解析器。AEXML 的分支。

这不是一个功能全面的强大 XML 解析器,而是一个简单、轻量级且易于使用的ild XML 处理的实用工具。
索引

功能

读取 XML 数据

  • 写入 XML 字符串
  • 覆盖单元测试
  • 覆盖内联文档
  • 用法

读取 XML

假设这是您从某处获得的 XML 字符串,并从那些数据中创建了一个变量 data: Data

这就是您可以使用 AEXML 来处理这些数据的方式

<?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>

(有关更多示例,请查看项目包含的单元测试代码)
写入 XML

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>) }

假设这是您需要生成的某些 SOAP XML 请求。

嗯,您只是为这个构建一个普通的字符串?
是的,但是,您也可以用 AEXML 以一种更结构化和优雅的方式进行

<?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=https://w3schools.org.cn/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>

安装

// 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> : https://w3schools.org.cn/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)

Swift 包管理器

  • CocoaPods:

     .Package(url: https://github.com/OpenClemson/AEXML-CU.git, majorVersion: 4)
    
  • 许可证:

     pod 'AEXML-CU'

AEXML 在 MIT 许可下发布。有关详细资料,请参阅 LICENSE

CocoaPods 是以下项目的一部分