Kanna

repository·master·Indexed 25 days ago

https://github.com/tid-kijyun/kanna

A cross-platform XML and HTML parser for macOS, iOS, tvOS, watchOS, and Linux, inspired by Nokogiri. Kanna allows users to parse HTML and XML strings and search for nodes using CSS selectors or XPath, including support for namespaces in XML.

Tokens
1.3K
Snippets
6
Records
8
Agent score
82%

What's inside Kanna

  1. Manual Installation of Kanna

    master

    To install Kanna manually, follow these steps:

    1. Add the following files to your project:
      • Source/Kanna.swift
      • Source/CSS.swift
      • Source/libxml/libxmlHTMLDocument.swift
      • Source/libxml/libxmlHTMLNode.swift
      • Source/libxml/libxmlParserOption.swift
      • The Modules directory
    2. In your target settings, add $(SDKROOT)/usr/include/libxml2 to the Search Paths > Header Search Paths field.
    3. In your target settings, add $(SRCROOT)/Modules to the Swift Compiler - Search Paths > Import Paths field.
  2. Install Kanna via Swift Package Manager

    master

    To use Kanna with Swift Package Manager, follow these steps:

    1. Install libxml2 dependencies

    Kanna requires libxml2 to be installed on your system.

    macOS:

    brew install libxml2
    brew link --force libxml2

    Linux (Ubuntu):

    sudo apt-get install libxml2-dev

    2. Configure Package.swift

    Add Kanna to your Package.swift dependencies and target dependencies:

    // swift-tools-version:4.0
    import PackageDescription
    
    let package = Package(
        name: "YourProject",
        dependencies: [
            .package(url: "https://github.com/tid-kijyun/Kanna.git", from: "4.0.0")
        ],
        targets: [
            .target(
                name: "YourTarget",
                dependencies: ["Kanna"]
            ),
        ]
    )

    3. Build

    Run the build command:

    swift build
  3. Install Kanna manually

    master

    For manual installation, follow these steps:

    1. Add the following files to your project:
      • Source/Kanna.swift
      • Source/CSS.swift
      • Source/libxml/libxmlHTMLDocument.swift
      • Source/libxml/libxmlHTMLNode.swift
      • Source/libxml/libxmlParserOption.swift
      • The Modules directory
    2. In target settings, add $(SDKROOT)/usr/include/libxml2 to the Search Paths > Header Search Paths field.
    3. In target settings, add $(SRCROOT)/Modules to the Swift Compiler - Search Paths > Import Paths field.
  4. Parse HTML and search using CSS or XPath

    master

    You can parse HTML strings using the HTML(html:encoding:) initializer. Once parsed, you can search for nodes using CSS selectors via .css() or XPath via .xpath(). You can also access attributes using subscript syntax (e.g., link["href"]).

    import Kanna
    
    let html = "<html>...</html>"
    
    if let doc = try? HTML(html: html, encoding: .utf8) {
        print(doc.title)
        
        // Search for nodes by CSS
        for link in doc.css("a, link") {
            print(link.text)
            print(link["href"])
        }
        
        // Search for nodes by XPath
        for link in doc.xpath("//a | //link") {
            print(link.text)
            print(link["href"])
        }
    }
  5. Parse XML and use namespaces with XPath

    master

    Use Kanna.XML(xml:encoding:) to parse XML strings. When performing XPath queries on namespaced XML, pass a dictionary of namespaces to the at_xpath or xpath methods.

    let xml = "..."
    if let doc = try? Kanna.XML(xml: xml, encoding: .utf8) {
        let namespaces = [
                        "o":  "urn:schemas-microsoft-com:office:office",
                        "ss": "urn:schemas-microsoft-com:office:spreadsheet"
                    ]
        if let author = doc.at_xpath("//o:Author", namespaces: namespaces) {
            print(author.text)
        }
    }