commit
574006f80d
26
README.md
26
README.md
|
@ -107,16 +107,16 @@ After parsing a document, and finding some elements, you'll want to get at the d
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
do {
|
do {
|
||||||
let html: String = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>";
|
let html: String = "<p>An <a href='http://example.com/'><b>example</b></a> link.</p>"
|
||||||
let doc: Document = try SwiftSoup.parse(html)
|
let doc: Document = try SwiftSoup.parse(html)
|
||||||
let link: Element = try doc.select("a").first()!
|
let link: Element = try doc.select("a").first()!
|
||||||
|
|
||||||
let text: String = try doc.body()!.text(); // "An example link"
|
let text: String = try doc.body()!.text() // "An example link."
|
||||||
let linkHref: String = try link.attr("href"); // "http://example.com/"
|
let linkHref: String = try link.attr("href") // "http://example.com/"
|
||||||
let linkText: String = try link.text(); // "example""
|
let linkText: String = try link.text() // "example"
|
||||||
|
|
||||||
let linkOuterH: String = try link.outerHtml(); // "<a href="http://example.com"><b>example</b></a>"
|
let linkOuterH: String = try link.outerHtml() // "<a href="http://example.com/"><b>example</b></a>"
|
||||||
let linkInnerH: String = try link.html(); // "<b>example</b>"
|
let linkInnerH: String = try link.html() // "<b>example</b>"
|
||||||
} catch Exception.Error(let type, let message) {
|
} catch Exception.Error(let type, let message) {
|
||||||
print(message)
|
print(message)
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -258,7 +258,7 @@ Like the other methods in `Element`, the attr methods return the current `Elemen
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
do {
|
do {
|
||||||
try doc.select("div.masthead").attr("title", "swiftsoup").addClass("round-box");
|
try doc.select("div.masthead").attr("title", "swiftsoup").addClass("round-box")
|
||||||
} catch Exception.Error(let type, let message) {
|
} catch Exception.Error(let type, let message) {
|
||||||
print(message)
|
print(message)
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -275,7 +275,7 @@ Use the HTML setter methods in `Element`:
|
||||||
```swift
|
```swift
|
||||||
do {
|
do {
|
||||||
let doc: Document = try SwiftSoup.parse("<div>One</div><span>One</span>")
|
let doc: Document = try SwiftSoup.parse("<div>One</div><span>One</span>")
|
||||||
let div: Element = try doc.select("div").first()! // <div></div>
|
let div: Element = try doc.select("div").first()! // <div>One</div>
|
||||||
try div.html("<p>lorem ipsum</p>") // <div><p>lorem ipsum</p></div>
|
try div.html("<p>lorem ipsum</p>") // <div><p>lorem ipsum</p></div>
|
||||||
try div.prepend("<p>First</p>")
|
try div.prepend("<p>First</p>")
|
||||||
try div.append("<p>Last</p>")
|
try div.append("<p>Last</p>")
|
||||||
|
@ -285,7 +285,7 @@ do {
|
||||||
let span: Element = try doc.select("span").first()! // <span>One</span>
|
let span: Element = try doc.select("span").first()! // <span>One</span>
|
||||||
try span.wrap("<li><a href='http://example.com/'></a></li>")
|
try span.wrap("<li><a href='http://example.com/'></a></li>")
|
||||||
print(doc)
|
print(doc)
|
||||||
// now: <li><a href="http://example.com/"><span>One</span></a></li>
|
// now: <html><head></head><body><div><p>First</p><p>lorem ipsum</p><p>Last</p></div><li><a href="http://example.com/"><span>One</span></a></li></body></html>
|
||||||
} catch Exception.Error(let type, let message) {
|
} catch Exception.Error(let type, let message) {
|
||||||
print(message)
|
print(message)
|
||||||
} catch {
|
} catch {
|
||||||
|
@ -312,7 +312,7 @@ Use the text setter methods of `Element`:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
do {
|
do {
|
||||||
let doc: Document = try SwiftSoup.parse("")
|
let doc: Document = try SwiftSoup.parse("<div></div>")
|
||||||
let div: Element = try doc.select("div").first()! // <div></div>
|
let div: Element = try doc.select("div").first()! // <div></div>
|
||||||
try div.text("five > four") // <div>five > four</div>
|
try div.text("five > four") // <div>five > four</div>
|
||||||
try div.prepend("First ")
|
try div.prepend("First ")
|
||||||
|
@ -516,9 +516,9 @@ print(txt)
|
||||||
```swift
|
```swift
|
||||||
let xml = "<?xml version='1' encoding='UTF-8' something='else'?><val>One</val>"
|
let xml = "<?xml version='1' encoding='UTF-8' something='else'?><val>One</val>"
|
||||||
guard let doc = try? SwiftSoup.parse(xml, "", Parser.xmlParser()) else { return }
|
guard let doc = try? SwiftSoup.parse(xml, "", Parser.xmlParser()) else { return }
|
||||||
guard let element = try? doc.getElementsByTag("val").first() // Find first element
|
guard let element = try? doc.getElementsByTag("val").first() else { return } // Find first element
|
||||||
element.text("NewValue") // Edit Value
|
try element.text("NewValue") // Edit Value
|
||||||
let valueString = element.text() // "NewValue"
|
let valueString = try element.text() // "NewValue"
|
||||||
```
|
```
|
||||||
|
|
||||||
## How to get all `<img src>`
|
## How to get all `<img src>`
|
||||||
|
|
Loading…
Reference in New Issue