Swift XML 解析库

闲来无事根据KissXML的设计灵感开源了一个Swift XML解析库

SwiftXMLKit


SwiftXMLKit 是 Swift 的一个轻量级 XML 解析和操作框架,灵感来自于 KissXML。它提供了一个简单而直观的 API,用于在利用 libxml2 的功能和效率的同时处理 XML 文档。


特点

  • 基于 libxml2 的性能

  • 支持 XML 解析和创建

  • 支持 XPath 查询

  • XML 节点操作

  • 自动处理内存管理


安装方式

  • 支持SPM方式集成到项目.
  • 也可以手用把Sources拖拽到项目

用法

import XCTest
@testable import SwiftXMLKit

final class SwiftXMLKitTests: XCTestCase {
    func testBasicXMLCreation() throws {
        let doc = XMLDocument()
        let root = XMLElement(name: "root")
        doc.setRootElement(root)
        
        let child = XMLElement(name: "child", stringValue: "Hello World")
        child.setAttribute("1", forName: "id")
        root.addChild(child)
        
        let xmlString = try doc.xmlString()
        XCTAssertTrue(xmlString.contains(""))
        XCTAssertTrue(xmlString.contains("Hello World"))
    }
    
    func testXMLParsing() throws {
        let xmlString = """
        
        
            Hello
            World
        
        """
        
        let doc = try XMLDocument(xmlString: xmlString)
        let root = doc.rootElement
        XCTAssertNotNil(root)
        XCTAssertEqual(root?.name, "root")
        
        let children = root?.elements(forName: "child")
        XCTAssertEqual(children?.count, 2)
        XCTAssertEqual(children?[0].attribute(forName: "id"), "1")
        XCTAssertEqual(children?[0].stringValue, "Hello")
        XCTAssertEqual(children?[1].attribute(forName: "id"), "2")
        XCTAssertEqual(children?[1].stringValue, "World")
    }
    
    func testXPath() throws {
        let xmlString = """
        
        
            
                Swift
                Apple
                29.99
            
            
                Objectiv-C
                Apple
                39.99
            
        
        """
        
        let doc = try XMLDocument(xmlString: xmlString)
        
        // Test finding all books
        let books = try doc.nodes(forXPath: "//book")
        XCTAssertEqual(books.count, 2)
        XCTAssertTrue(books[0] is XMLElement)
        
        // Test finding fiction books
        let fictionBooks = try doc.nodes(forXPath: "//book[@category='fiction']")
        XCTAssertEqual(fictionBooks.count, 1)
        
        guard let fictionBook = fictionBooks[0] as? XMLElement else {
            XCTFail("Expected XMLElement for book node")
            return
        }
        
        let titles = fictionBook.elements(forName: "title")
        XCTAssertEqual(titles.count, 1, "Expected exactly one title element")
        
        guard let title = titles.first else {
            XCTFail("No title element found")
            return
        }
        
        XCTAssertEqual(title.stringValue, "Swift")
        
        // Test finding all prices
        let prices = try doc.nodes(forXPath: "//price")
        XCTAssertEqual(prices.count, 2)
        XCTAssertEqual(prices[0].stringValue, "29.99")
        XCTAssertEqual(prices[1].stringValue, "39.99")
    }
    
    func testNodeManipulation() throws {
        let doc = XMLDocument()
        let root = XMLElement(name: "root")
        doc.setRootElement(root)
        
        // Add children
        let child1 = XMLElement(name: "child")
        child1.setAttribute("1", forName: "id")
        root.addChild(child1)
        
        let child2 = XMLElement(name: "child")
        child2.setAttribute("2", forName: "id")
        root.addChild(child2)
        
        // Test removal
        child1.removeFromParent()
        
        let remainingChildren = root.elements(forName: "child")
        XCTAssertEqual(remainingChildren.count, 1)
        XCTAssertEqual(remainingChildren[0].attribute(forName: "id"), "2")
    }
}

你可能感兴趣的:(iOS,swift,xml,开发语言)