Yes, SwiftSoup is a pure Swift library available for iOS and macOS development that provides the ability to parse and manipulate HTML documents. It is inspired by the popular Java library, Jsoup, and offers a similar set of features that enable developers to navigate the HTML DOM (Document Object Model), select, edit, and extract data from HTML.
Here are some examples of how you can manipulate the HTML structure using SwiftSoup:
Parsing HTML
First, you need to parse an HTML string into a document object before performing any manipulation:
import SwiftSoup
let html = "<html><head><title>Sample Title</title></head><body><p>This is a sample text.</p></body></html>"
do {
let doc: Document = try SwiftSoup.parse(html)
// Now you can manipulate `doc`
} catch Exception.Error(let type, let message) {
print(message)
} catch {
print("error")
}
Modifying Elements
You can modify elements by selecting them and then using various methods to alter their content or attributes:
do {
let doc: Document = try SwiftSoup.parse(html)
// Select the first paragraph
if let paragraph = try doc.select("p").first() {
// Change the text of the paragraph
try paragraph.text("This is a modified text.")
}
// Print the modified HTML
print(try doc.outerHtml())
} catch {
print("error")
}
Adding Elements
You can also add new elements to the DOM:
do {
let doc: Document = try SwiftSoup.parse(html)
// Create a new element
let newElement = Element(tag: Tag("div"), baseUri: "", attributes: Attributes())
try newElement.text("A new div element.")
// Add the new element to the body
if let body = doc.body() {
try body.appendChild(newElement)
}
// Print the modified HTML
print(try doc.outerHtml())
} catch {
print("error")
}
Removing Elements
To remove elements from the DOM:
do {
let doc: Document = try SwiftSoup.parse(html)
// Select the title and remove it
if let title = try doc.select("title").first() {
try title.remove()
}
// Print the modified HTML
print(try doc.outerHtml())
} catch {
print("error")
}
Setting Attributes
You can set or modify attributes of elements:
do {
let doc: Document = try SwiftSoup.parse(html)
// Select the first paragraph and set an id attribute
if let paragraph = try doc.select("p").first() {
try paragraph.attr("id", "main-paragraph")
}
// Print the modified HTML
print(try doc.outerHtml())
} catch {
print("error")
}
These are just a few examples of what you can do with SwiftSoup. The library provides a wide range of functions to manipulate HTML content, allowing developers to make changes to the DOM as needed programmatically.
Remember to always handle errors appropriately when using SwiftSoup, as many of its methods can throw exceptions. Also, be sure to use SwiftSoup responsibly and respect the terms of service and robots.txt of websites when scraping and manipulating HTML data.