AEXML
简单的轻量级 XML 解析的 Swift 从属者
索引
简介
这不仅仅是一个功能强大的全面 XML 解析器,而是简化、轻量级且易于使用的实用工具,用于轻量级 XML 处理。
功能
- 读取 XML 数据
- 写入 XML 字符串
- 各种 单元测试 覆盖
- 内联文档覆盖
使用方法
读取XML
假设这是你从某处获取的XML字符串,并将其存储在变量 data: Data
中。
<?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>
这是如何使用 AEXML 来处理这些数据的示例
(对于更多的示例,请参考项目中包含的单元测试代码)
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)")
}
写入XML
假设这是你需要生成的某个SOAP XML请求。
当然,你可以直接构建普通的字符串,为什么不可以呢?
<?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="https://w3schools.org.cn/transaction/" soap:mustUnderstand="1">234</m:Trans>
</soap:Header>
<soap:Body>
<m:GetStockPrice>
<m:StockName>AAPL</m:StockName>
</m:GetStockPrice>
</soap:Body>
</soap:Envelope>
是的,但你也可以用 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" : "https://w3schools.org.cn/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)
安装
-
.package(url: "https://github.com/tadija/AEXML.git", from: "4.6.0")
-
github "tadija/AEXML"
-
pod 'AEXML'
许可证
AEXML 在 MIT 许可证下发布。有关详细信息,请参阅 LICENSE。