在Swift中对XML元素进行分组

时间:2016-08-29 22:59:28

标签: xml swift

我有一系列CGPoints(当然还有他们的x / y坐标)。我试图创建xml结构并将其导出到文件中,但我无法正确排列元素。我需要将cgpoints分组为4,然后在每个组的末尾添加源元素(使用结束映射标记)。像这样:

<map>
   <point id="1" top="0" left="0"/>
   <point id="2" top="0" left="532"/>
   <point id="3" top="211" left="532"/>
   <point id="4" top="211" left="0"/>
   <source image="mix" top="0" left="0" width="532" height="211"/>
</map>
<map>
   <point id="5" top="0" left="532"/>
   <point id="6" top="0" left="686"/>
   <point id="7" top="211" left="686"/>
   <point id="8" top="211" left="532"/>
   <source image="mix" top="0" left="532" width="154" height="211"/>
</map>

但我得到的结果,看起来像这样:

<map>
   <point id="1" top="0" left="0"/>
   <point id="2" top="0" left="532"/>
   <point id="3" top="211" left="532"/>
   <point id="4" top="211" left="0"/>
   <point id="5" top="0" left="532"/>
   <point id="6" top="0" left="686"/>
   <point id="7" top="211" left="686"/>
   <point id="8" top="211" left="532"/>
   <source image="mix" top="0" left="532" width="154" height="211"/>
</map>

我做错了什么?这是一个代码:

   let map = NSXMLElement(name: "map")

   let array = myArray.flatMap { return $0.myCoord() }  
      for (ind, dot) in array.enumerate() {  
         let x1 = dot.x
         let y1 = dot.y

   let point = NSXMLElement(name: "point")
        point.attributes = [
            xmlAttributeWithName("id", value: "\(ind+1)"),
            xmlAttributeWithName("top", value: "\(Int(x1))"),
            xmlAttributeWithName("left", value: "\(Int(y1))")
        ]
        map.addChild(point)
    }

    let source = NSXMLElement(name: "source")
        source.attributes = [
            xmlAttributeWithName("image", value: "mix"),
            xmlAttributeWithName("top", value: "\(Int(x1))"),
            xmlAttributeWithName("left", value: "\(Int(y1))"),
            xmlAttributeWithName("width", value: "\(Int(x3))"),
            xmlAttributeWithName("height", value: "\(Int(y3))")
        ]
        map.addChild(source)

1 个答案:

答案 0 :(得分:1)

将2个循环嵌套在一起。外循环应该逐步为4,内循环步长为1并对应于每个map元素:

let root = NSXMLElement(name: "root")

for i in 0.stride(to: array.count, by: 4) {
    let map = NSXMLElement(name: "map")

    for j in i..<(i + 4) {
        let point = NSXMLElement(name: "point")

        point.attributes = [
            xmlAttributeWithName("id", value: "\(j)"),
            xmlAttributeWithName("top", value: "\(Int(array[j].y))"),
            xmlAttributeWithName("left", value: "\(Int(array[j].x))")
        ]
        map.addChild(point)
    }

    let source = NSXMLElement(name: "source")
    source.attributes = [
        xmlAttributeWithName("image", value: "mix"),
        xmlAttributeWithName("top", value: "\(Int(array[i+2].y))"),
        xmlAttributeWithName("left", value: "\(Int(array[i].x))"),
        xmlAttributeWithName("width", value: "\(Int(array[i+3].x))"),
        xmlAttributeWithName("height", value: "\(Int(array[i+1].y))")
    ]
    map.addChild(source)
    root.addChild(map)
}

print(root)

(假设array的长度可以被4整除