Swift开发:创建XML文件,包含节点,属性值

前端之家收集整理的这篇文章主要介绍了Swift开发:创建XML文件,包含节点,属性值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

---------------@H_404_2@XmlWrite.swift@H_404_2@-----------------@H_404_2@@H_404_7@

@H_404_7@

@H_404_7@

//
//  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 + ">"
        }
                
}

@H_404_2@



@H_404_7@


@H_404_7@

-----------------@H_404_2@代码调用@H_404_2@---------------@H_404_2@
@H_404_7@

@H_404_2@@H_404_7@

@H_404_2@dispatch_async@H_404_2@(@H_404_2@dispatch_get_main_queue@H_404_2@()) { () ->@H_404_2@Void@H_404_2@ @H_404_2@in@H_404_2@@H_404_7@

@H_404_2@@H_404_7@

@H_404_2@// 1 @H_404_2@创建@H_404_2@xml@H_404_2@文件路径,名称@H_404_2@@H_404_7@

@H_404_2@let@H_404_2@ xml =@H_404_2@XmlWrite@H_404_2@(dir: @H_404_2@"/Users/huangtao/Desktop/"@H_404_2@,fileName:@H_404_2@"data.xml"@H_404_2@)@H_404_2@@H_404_7@

@H_404_2@@H_404_7@

@H_404_2@// 2 @H_404_2@创建第一个节点数据@H_404_2@@H_404_7@

@H_404_2@let@H_404_2@ item1:@H_404_2@Item@H_404_2@ =@H_404_2@Item@H_404_2@(name: @H_404_2@"Item1"@H_404_2@);@H_404_2@@H_404_7@

item1.@H_404_2@attributes@H_404_2@ = [@H_404_2@"A1"@H_404_2@:@H_404_2@"1"@H_404_2@,@H_404_2@"A2"@H_404_2@:@H_404_2@"2"@H_404_2@]@H_404_2@@H_404_7@

@H_404_2@for@H_404_2@(@H_404_2@var@H_404_2@ i=@H_404_2@0@H_404_2@;i<@H_404_2@5@H_404_2@;i++) {@H_404_2@@H_404_7@

@H_404_2@let@H_404_2@ node =@H_404_2@Node@H_404_2@(name:@H_404_2@"note-@H_404_2@\@H_404_2@(@H_404_2@i@H_404_2@)"@H_404_2@);@H_404_2@@H_404_7@

node.@H_404_2@id@H_404_2@ = i+@H_404_2@1@H_404_2@;@H_404_2@@H_404_7@

node.@H_404_2@attributes@H_404_2@ = [@H_404_2@"ID"@H_404_2@:@H_404_2@"@H_404_2@\@H_404_2@(@H_404_2@i@H_404_2@)"@H_404_2@,@H_404_2@"Name"@H_404_2@:@H_404_2@"@H_404_2@我的名称@H_404_2@-@H_404_2@\@H_404_2@(@H_404_2@i@H_404_2@)"@H_404_2@]@H_404_2@@H_404_7@

item1.@H_404_2@addNode@H_404_2@(node);@H_404_2@@H_404_7@

@H_404_2@@H_404_7@

}@H_404_2@@H_404_7@

xml.@H_404_2@items@H_404_2@?.@H_404_2@append@H_404_2@(item1);@H_404_2@@H_404_7@

@H_404_2@@H_404_7@

@H_404_2@// 3 @H_404_2@创建第二个节点数据@H_404_2@@H_404_7@

@H_404_2@let@H_404_2@ item2:@H_404_2@Item@H_404_2@ =@H_404_2@Item@H_404_2@(name: @H_404_2@"Item2"@H_404_2@);@H_404_2@@H_404_7@

@H_404_2@for@H_404_2@(@H_404_2@var@H_404_2@ i=@H_404_2@0@H_404_2@;i<@H_404_2@10@H_404_2@;i++) {@H_404_2@@H_404_7@

@H_404_2@let@H_404_2@ node =@H_404_2@Node@H_404_2@(name:@H_404_2@"note-@H_404_2@\@H_404_2@(@H_404_2@i@H_404_2@)"@H_404_2@);@H_404_2@@H_404_7@

node.@H_404_2@id@H_404_2@ = i+@H_404_2@1@H_404_2@;@H_404_2@@H_404_7@

node.@H_404_2@attributes@H_404_2@ = [@H_404_2@"ID"@H_404_2@:@H_404_2@"@H_404_2@\@H_404_2@(@H_404_2@i@H_404_2@)"@H_404_2@,@H_404_2@"Name"@H_404_2@:@H_404_2@"N-@H_404_2@\@H_404_2@(@H_404_2@i@H_404_2@)"@H_404_2@,@H_404_2@"Disp"@H_404_2@:@H_404_2@"1"@H_404_2@,@H_404_2@"Appliance"@H_404_2@:@H_404_2@"1"@H_404_2@,@H_404_2@"Icon"@H_404_2@:@H_404_2@"ic_switch_4"@H_404_2@]@H_404_2@@H_404_7@

item2.@H_404_2@addNode@H_404_2@(node);@H_404_2@@H_404_7@

@H_404_2@@H_404_7@

}@H_404_2@@H_404_7@

xml.@H_404_2@items@H_404_2@?.@H_404_2@append@H_404_2@(item2);@H_404_2@@H_404_7@

@H_404_2@@H_404_7@

@H_404_2@// 4 @H_404_2@保存文件@H_404_2@@H_404_7@

xml.@H_404_2@saveXml@H_404_2@()@H_404_2@@H_404_7@

@H_404_2@@H_404_7@

@H_404_2@@H_404_7@

}@H_404_2@@H_404_7@

@H_404_7@


-----------------@H_404_2@ @H_404_493@效果图@H_404_2@@H_404_2@ ---------------@H_404_2@


@H_404_2@@H_404_7@


@H_404_2@@H_404_7@


@H_404_2@@H_404_7@

解析xml见博客:@H_404_2@@H_404_7@

http://www.jb51.cc/article/p-xudpqbfh-bgc.html @H_404_2@@H_404_7@ 原文链接:https://www.f2er.com/swift/321935.html

猜你在找的Swift相关文章