XMLModelizer 0.0.2

XMLModelizer 0.0.2

测试已测试
Lang语言 SwiftSwift
许可 MIT
Released最新版本2016 年 10 月
SPM支持 SPM

Aoi Fukuoka 维护。



  • Aoi Fukuoka

XMLModelizer

sample

XMLModelizer 是一个简单的 XML 解析器和模型化器。
一旦您将模型类和 XML URL 传递给 XMLModelizer,您就可以获取包含您需要的 XML 元素模型实例。

示例

如果您想获取以下 xml 中每个 item 标签的 titlelinkcontent:mediacategorypubDate 值。

<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:nyt="http://www.nytimes.com/namespaces/rss/2.0" version="2.0">
    <channel>
        <item>
            <title>
                Xi Jinping May Delay Picking China’s Next Leader, Stoking Speculation
            </title>
            <link>
                http://www.nytimes.com/2016/10/05/world/asia/china-president-xi-jinping-successor.html?partner=rss&emc=rss
            </link>
            <guid isPermaLink="true">
                http://www.nytimes.com/2016/10/05/world/asia/china-president-xi-jinping-successor.html
            </guid>
            <atom:link rel="standout" href="http://www.nytimes.com/2016/10/05/world/asia/china-president-xi-jinping-successor.html?partner=rss&emc=rss"/>
            <media:content url="https://static01.nyt.com/images/2016/10/03/world/04CHINA-XI-web1/04CHINA-XI-web1-moth.jpg" medium="image" height="151" width="151"/>
            <media:description>
                President Xi Jinping of China during a ceremony observing Martyrs’ Day, a holiday that memorializes Chinese who died in battle against foreign powers, at Tiananmen Square in Beijing last week.
            </media:description>
            <media:credit>Mark Schiefelbein/Associated Press</media:credit>
            <description>
                The apparent plan by the Chinese president has unsettled the party elite and created uncertainty over whether Mr. Xi will try to stay in power beyond the usual two terms.
            </description>
            <dc:creator>CHRIS BUCKLEY</dc:creator>
            <pubDate>Tue, 04 Oct 2016 23:59:10 GMT</pubDate>
            <category domain="http://www.nytimes.com/namespaces/keywords/nyt_per">Xi Jinping</category>
            <category domain="http://www.nytimes.com/namespaces/keywords/nyt_org_all">Communist Party of China</category>
            <category domain="http://www.nytimes.com/namespaces/keywords/nyt_geo">China</category>
            <category domain="http://www.nytimes.com/namespaces/keywords/des">Appointments and Executive Changes</category>
            <category domain="http://www.nytimes.com/namespaces/keywords/nyt_per">Li Keqiang</category>
            <category domain="http://www.nytimes.com/namespaces/keywords/nyt_org_all">
                Politburo Standing Committee of the Communist Party of China
            </category>
        </item>
        <item>...

首先,定义您自定义的模型,继承自 XMLModelizerModel 并重写 xmlModelizerXpathKeyMap 函数,添加您想要的属性。

import XMLModelizer

class NYTimesArticle: XMLModelizerModel {

    //Add properties you want to get from xml.
    var title: [String]! // please define properties as [String]!
    var link: [String]!
    var media: [String]!
    var category: [String]!
    var pubDate: [String]!

    //Please define xpath to get contents of xml.
    //In dictionary, key is your property, and value is xpath of its key.
    // [property : xpath]
    override class func xmlModelizerXpathKeyMap() -> [String:String] {
        return [
            "title":"//item/title",
            "link":"//item/link",
            "media":"//item/media:content/@url",
            "category":"//item/category",
            "pubDate":"//item/pubDate",
        ]
    }
}

最后,您可以通过以下方式获取 Array<NYTimesArticle>

import XMLModelizer
// open static func modelize(modelClass: XMLModelizerModel.Type, urlString: String) -> [AnyObject]
let nytimesArticles: Array<NYTimesArticle> = XMLModelizer.modelize(NYTimesArticle.self, "http://rss.nytimes.com/services/xml/rss/nyt/World.xml")

let article: NYTimesArticle = nytimesArticles.first
print(article.title.first) // "Xi Jinping May Delay Picking China’s Next Leader, Stoking Speculation"
print(article.category[1])// "Communist Party of China"

注意

如果在 Xcode8 中您收到错误 “Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choose a Swift version or use the Build Settings editor to configure the build setting directly.,请将 XMLModelizer 的 plist 中的“Use Legacy Swift Language Version” 设置为 在您的 Pods 项目中。

要求

iOS 8.0 或更高版本。

安装

XMLModelizer 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile

pod "XMLModelizer"

作者

Aoi Fukuoka,8b0e5ater0地方0b0e5a4ee02070e5a0e40f1a5d0b0e96bff6a5b6e7">[email protected]

许可

XMLModelizer 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。