platform :ios, '13.0'
use_frameworks!
target 'DemoApp' do
source 'https://github.com/CocoaPods/Specs.git'
pod 'AEXML'
end
<?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>
import UIKit
import AEXML
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
yeTyeXML()
}
func yeTyeXML(){
guard let xmlPath = Bundle.main.path(forResource: "XMLExample", ofType: "xml"),
let data = try? Data(contentsOf: URL(fileURLWithPath: xmlPath)) else { return }
do
{
let xmlDoc = try AEXMLDocument(xml: data, options: AEXMLOptions())
print("---XML格式的完整原数据:\(xmlDoc.xml)--")
print(xmlDoc.root["cats"]["cat"].count)
print(xmlDoc.root["cats"]["cat"].attributes["breed"]!)
print(xmlDoc.root["cats"]["cat"].xmlCompact)
for child in xmlDoc.root.children
{
print("---XML节点名:child.name:\(child.name)--")
}
print("-----------------------------")
print(xmlDoc.root["cats"]["cat"].value as Any)
print(xmlDoc.root["cats"]["cat"].string)
print(xmlDoc.root["dogs"]["dog"].last?.value as Any)
print(xmlDoc.root["dogs"].children[2].string)
print("--------------all---------------")
if let cats = xmlDoc.root["cats"]["cat"].all
{
for cat in cats
{
if let name = cat.value
{
print(name)
}
}
}
print("---------------withValue--------------")
if let cats = xmlDoc.root["cats"]["cat"].all(withValue: "Tinna")
{
for cat in cats
{
print(cat.string)
}
}
print("-------------withAttributes----------------")
if let cats = xmlDoc.root["cats"]["cat"].all(withAttributes: ["breed" : "Domestic", "color" : "yellow"])
{
for cat in cats
{
print(cat.string)
}
}
print("--------------attributes---------------")
for dog in xmlDoc.root["dogs"]["dog"].all!
{
if let color = dog.attributes["color"]
{
if color == "white"
{
print(dog.string)
}
}
}
}
catch
{
print("\(error)")
}
}
}
import UIKit
import AEXML
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
yeTyeXMLWrite()
}
func yeTyeXMLWrite(){
let soapRequest = AEXMLDocument()
let attributes = ["xmlns:xsi" : "https://www.baidu.com/2001/XMLSchema-instance",
"xmlns:xsd" : "https://www.baidu.com/2001/XMLSchema"]
let envelope = soapRequest.addChild(name: "soap:Envelope", attributes: attributes)
let employees = envelope.addChild(name: "Employees")
let managers = employees.addChild(name: "Managers")
managers.addChild(name: "Manager", attributes:["Name":"Jerry", "age":"40"])
managers.addChild(name: "Manager", attributes:["Name":"Peter", "age":"44"])
let engineers = employees.addChild(name: "Engineers")
engineers.addChild(name: "Engineer", attributes:["Name":"Bill", "age":"29"])
engineers.addChild(name: "Engineer", attributes:["Name":"Somus", "age":"31"])
print("---生成的XML格式数据:\(soapRequest.xml)--")
}
}