驯鹿
描述
- 使用 libxml2
- 基于 https://github.com/mattt/Ono
- 用于 Snowflake
使用方法
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>
</htmllet 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="https://w3schools.org.cn">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查询
- 获取元素信息
let element = document.rootElement.child(index: 0)
element.name
element.ns
element.line
element.attributes
element.parent
element.nextSibling
element.previousSibling- 获取子元素
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"
}- 将元素转换为字符串
element.toAttributeString()
element.toXMLString()
安装
Reindeers 可通过 CocoaPods 获取。要安装,只需将以下行添加到您的 Podfile
pod 'Reindeers'Reindeers 还可通过 Carthage 获取。要安装,只需将以下内容写入您的 Cartfile
github "onmyway133/Reindeers"Reindeers 还可手动安装。只需下载并将在项目中将 Sources 文件夹拖入。
作者
Khoa Pham,[email protected]
贡献
我们希望能为 Reindeers 做贡献,请查看 CONTRIBUTING 文件获取更多信息。
许可证
Reindeers 的发布坚持 MIT 许可证。更多详情请参阅 LICENSE 文件。
