---------------XmlWrite.swift-----------------
// // XmlWrite.swift // CreateXmlFile // // Created by 黄涛 on 16/5/25. // Copyright © 2016年 黄涛. All rights reserved. // import UIKit class XmlWrite: NSObject { fileprivate let xmlHead = "<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>" fileprivate var dirPath = "" fileprivate var fileName = "" fileprivate var filePath = "" fileprivate var root = ""; init(dir: String,fileName: String,root:String) { super.init() self.dirPath = dir self.fileName = fileName self.filePath = dirPath + self.fileName self.root = root; items = []; } var items:[Item]?; func saveXml() { let data = NSMutableData()//可变数据 // xml 文件声明 data.append("\(xmlHead)\n<\(root)>\n".data(using: String.Encoding.utf8,allowLossyConversion: true)!) // 遍历 item 第一层 for i in 0..<items!.count { if items![i].attributes != nil { // 获取item 属性string let itemStr:String? = ItemToString(items![i]) // 添加item属性 data.append(" \(itemStr!)\n".data(using: String.Encoding.utf8,allowLossyConversion: true)!) } // 当前item 的node集合 let nodes = items![i].nodes; for j in 0..<nodes!.count { //获取node 属性string let node = (nodes?[j])!; // 判断当前节点的子节点 let isSub = node.subNode?.count > 0 ? true : false; let nodeStr:String? = NodeToString(node,isSub: isSub); // print(nodeStr) // 添加node属性 data.append(" \(nodeStr!)\n".data(using: String.Encoding.utf8,allowLossyConversion: true)!) if node.subNode?.count > 0 { for k in 0..<node.subNode!.count { //获取node 属性string let snode = node.subNode?[k]; let snodeStr:String? = NodeToString(snode!,isSub: false); // print(snodeStr) // 添加node属性 data.append(" \(snodeStr!)\n".data(using: String.Encoding.utf8,allowLossyConversion: true)!) data.write(toFile: filePath,atomically: true) } // 当前 子节点 结束 data.append(" </\(node.name)>\n".data(using: String.Encoding.utf8,allowLossyConversion: true)!) } } // 当前 item 结束 data.append(" </\(items![i].name)>\n".data(using: String.Encoding.utf8,allowLossyConversion: true)!) data.write(toFile: filePath,atomically: true) } // 当前 root 结束 data.append("</\(root)>\n".data(using: String.Encoding.utf8,allowLossyConversion: true)!) //写入文件中 data.write(toFile: filePath,atomically: true) print(filePath) } func ItemToString(_ item:Item) ->String { // 1 拼接 节点名称 let nodeName = item.name var nodeStr:String = nodeName.elementStartTag() + " " if item.attributes?.count == 0 { return nodeStr; } // 2 拼接节点 属性 for (key,value) in (item.attributes)! { nodeStr += "\(key)=\"\(value)\" " } nodeStr += ">" return nodeStr } func NodeToString(_ node:Node,isSub:Bool) ->String { // 1 拼接 节点名称 let nodeName = node.name var nodeStr:String = nodeName.elementStartTag() + " " // 2 拼接节点 属性 for (key,value) in (node.attributes)! { nodeStr += "\(key)=\"\(value)\" " } if isSub { nodeStr += ">" } else { nodeStr += "/>" } return nodeStr } } class Item { init(name:String) { self.name = name; nodes = []; } var name:String = ""; var attributes:[String:String]? var nodes:[Node]?; func addNode(_ node:Node){ nodes?.append(node); } } class Node { init(name:String) { self.name = name; subNode = []; } init (){ subNode = []; } var id = 0; var name = "" var attributes:[String:String]? var subNode:[Node]?; func addSubNode(_ node:Node){ subNode?.append(node); } } extension String { func elementStartTag() -> String { return "<" + self } func elementEndAttributesTag() -> String { return self + ">" } func elementEndTag() -> String { return "</" + self + ">" } }
-----------------代码调用---------------
dispatch_async(dispatch_get_main_queue()) { () ->Void in
let xml =XmlWrite(dir: "/Users/huangtao/Desktop/",fileName:"data.xml")
// 2 创建第一个节点数据
let item1:Item =Item(name: "Item1");
item1.attributes = ["A1":"1","A2":"2"]
for(var i=0;i<5;i++) {
let node =Node(name:"note-\(i)");
node.id = i+1;
node.attributes = ["ID":"\(i)","Name":"我的名称-\(i)"]
item1.addNode(node);
}
xml.items?.append(item1);
// 3 创建第二个节点数据
let item2:Item =Item(name: "Item2");
for(var i=0;i<10;i++) {
let node =Node(name:"note-\(i)");
node.id = i+1;
node.attributes = ["ID":"\(i)","Name":"N-\(i)","Disp":"1","Appliance":"1","Icon":"ic_switch_4"]
item2.addNode(node);
}
xml.items?.append(item2);
// 4 保存文件
xml.saveXml()
}
解析xml见博客: