Swift html解析,查找,操作和导出

时间:2016-12-29 18:05:35

标签: html ios css swift parsing

我有一个复杂的HTML,我想解析,查找和操作一些元素。

有人可以说明如何处理这个示例HTML吗?

"<!DOCTYPE html>" +
"<html>" +
"<head>" +
"<title>Page Title</title>" +
"</head>" +
"<body>" +
"<h1>This is a Heading</h1>" +
"<p class='paragraph'>This is a paragraph.</p>" +
"<p class='paragraph'>This is a paragraph 2.</p>" +
"</body>" +
"</html>";

1 个答案:

答案 0 :(得分:3)

使用SwiftSoup进行操作的简单方法是使用Swift库来处理真实HTML。

https://github.com/scinfu/SwiftSoup

这是一个示例代码:

do{
            let html = "<!DOCTYPE html>" +
                "<html>" +
                "<head>" +
                "<title>Page Title</title>" +
                "</head>" +
                "<body>" +
                "<h1>This is a Heading</h1>" +
                "<p class='paragraph'>This is a paragraph.</p>" +
                "<p class='paragraph'>This is a paragraph 2.</p>" +
                "</body>" +
            "</html>";

            let doc: Document = try SwiftSoup.parse(html)
            let els: Elements = try doc.getElementsByClass("paragraph")
            let el: Element? = els.first()//get first element
            print(try "\(el?.text())")//This is a paragraph.
            try el?.text("New paragraph")
            print(try "\(el?.text())")//New paragraph


            //add new element
            let newNode: Element =  Element(try Tag.valueOf("em"), "")
            try newNode.appendText("four")
            try doc.body()?.appendChild(newNode)

            //add html
            try doc.body()?.append("<p>new html</p>")

            print(try doc.html())
        }catch Exception.Error(let type, let message)
        {
            print("")
        }catch{
            print("")
        }

这里有新的HTML:

<!doctype html>
<html>
 <head>
  <title>Page Title</title>
 </head>
 <body>
  <h1>This is a Heading</h1>
  <p class="paragraph">New paragraph</p>
  <p class="paragraph">This is a paragraph 2.</p>
  <em>four</em>
  <p>new html</p>
 </body>
</html>