xml in Swift
- Use libxml2
- Based on https://github.com/mattt/Ono
<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 = Utils.load(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"]
<!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
- 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"
}
Reindeer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'Reindeer', git: 'https://github.com/onmyway133/Reindeer'
Reindeer is also available through Carthage. To install just write into your Cartfile:
github "onmyway133/Reindeer"
Reindeer can also be installed manually. Just download and drop Sources
folders in your project.
Khoa Pham, [email protected]
We would love you to contribute to Reindeer, check the CONTRIBUTING file for more info.
Reindeer is available under the MIT license. See the LICENSE file for more info.