Swift package only. Add this repo url to your package dependencies. Xcode 11 supports by default.
Usage
Initialize a Doc first
let xmlDoc = try! Doc(fileURL: assetURL(forName: "xml.xml"), kind: .xml)
// or if you want to use on HTML
let htmlDoc = try! Doc(fileURL: assetURL(forName: "html.html"), kind: .html)
// you can also init from data or string
let dataDoc = try! Doc(data: #<data>, kind: .xml)
let stringDoc = try! Doc(string: "<html><body>Hello World</body></html>", kind: .html)
Navigate by XPath
// func select(xpath: String) -> [Node]
let nodes = htmlDoc.root.select(xpath: "//p") // Select all `p` on root node:
// func first(xpath: String) -> Node?
let p = htmlDoc.root.first(xpath: "//p") // Select first `p` on root node:
let span = p.first("./span") // Select first child span on `p`
Evaluate XPath Expression
func eval(expr: String) -> Node.EvalResult?
// count how many p tags
let count = htmlDoc.root.eval(expr: "count(//p)")
Node Info
var tag: String?
var content: String?
var rawContent: String?
var innerRawContent: String?
var attributes: AnySequence<Node.Attribute>?
func value(forAttribute name: String) -> String?
Node Hierarchies
var parent: Node?
var childNodes: AnySequence<Node>?
var firstNode: Node?
func childNode(at index: Int) -> Node?
var prev: Node?
var next: Node?
Advanced usage on namespace
By default, XPaver will solve namespaces for you internally, you don’t need to care namespaces if document has only one default namespace.
But if you encounter a XML which contains more than one namespace like this:
XPaver
Make XML navigation by XPath easier.
Install
Swift package only. Add this repo url to your package dependencies. Xcode 11 supports by default.
Usage
Initialize a
Doc
firstNavigate by XPath
Evaluate XPath Expression
Node Info
Node Hierarchies
Advanced usage on namespace
By default,
XPaver
will solve namespaces for you internally, you don’t need to care namespaces if document has only one default namespace.But if you encounter a XML which contains more than one namespace like this:
You need to register namespaces and write namespaces in XPath directly:
About Me
LICENSE
MIT.