Reindeers
Description
- Use libxml2
- Based on https://github.com/mattt/Ono
- Used in Snowflake
Usage
XML, SVG, RSS
<html>
<body>
<h1>My first SVG</h1>
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
</svg>
</body>
</html
let data = doItYourselfToLoadXMLStringFrom(fileName: "test4", ext: "svg")
let document = try! Document(data: data)
document.rootElement.name
document.rootElement.children()
document.rootElement.elements(XPath: "//svg").first?.attributes["width"]
document.rootElement.elements(XPath: "//circle").first?.attributes["stroke"]
HTML
<!DOCTYPE html>
<html>
<head>
<style>
h1 {
color: blue;
font-family: verdana;
font-size: 300%;
}
p {
color: red;
font-family: courier;
font-size: 160%;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<p>This is a paragraph.</p>
<a href="http://www.w3schools.com">Visit W3Schools</a>
</body>
</html>
let data = Utils.load(fileName: "test3", ext: "html")
let document = try? Document(data: data, kind: .html)
let body = document.rootElement.child(index: 1)
body?.elements(XPath: "//p").first?.content
Query
- Get element info
let element = document.rootElement.child(index: 0)
element.name
element.ns
element.line
element.attributes
element.parent
element.nextSibling
element.previousSibling
- Get child elements
element.children { child, index in
return child.name == "a"
}
element.children(name: "item")
element.children(indexes: [0, 2, 4])
- XPath
let body = document.rootElement.firstChild(name: "body")
body?.elements(XPath: "//a").first?.attributes["href"]
body?.elements(XPath: "//div") { element in
return element.attributes["width"] == "100"
}
- Turn Element to String
element.toAttributeString()
element.toXMLString()
Installation
Reindeers is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Reindeers'
Reindeers is also available through Carthage. To install just write into your Cartfile:
github "onmyway133/Reindeers"
Reindeers can also be installed manually. Just download and drop Sources
folders in your project.
Author
Khoa Pham, [email protected]
Contributing
We would love you to contribute to Reindeers, check the CONTRIBUTING file for more info.
License
Reindeers is available under the MIT license. See the LICENSE file for more info.