在 swift 中阅读来自 xml 的评论或评论节点

read comment or commented node from xml in swift

首先,考虑如下 xml 结构:

<!-- this is a comment -->
<methods>
                <include name="test" />
                <include name="test" />
<!--            <include name="testcase-commented" />-->
<!--            <include name="testcase-commented" />-->
<!--            <include name="testcase-commented" />-->
                <include name="test" />
</methods>

从这个 xml 我可以使用 NSXmlParser 读取所有包含的名称。

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {

    if(elementName == "include"){

        testCaseName.append(attributeDict["name"]!)
    }
}

但是注释的测试用例没有使用这种方法解析。

有什么方法可以使用 xml 解析器只读取来自 xml 的注释测试用例名称,或者无法从 [=27] 中的 xml 读取注释标签=]?

未在网上找到解决方案。

我正在使用

xcode 7.3 OSX 10.11 and swift 2.2

首先,我要感谢 AlexanderMomchliov 的宝贵建议,希望您知道如何解析 xml 文件。

解析器有一些重载的方法,从那里,只需调用这个:

func parser(parser: NSXMLParser, foundComment comment: String) {

    print(comment)
}

如果 xml 文件有一些注释节点,例如:

<!-- this is a comment -->
<test name="testOne">
<classes>
    <class name="TestClassOne">
        <methods>
            <include name="test_480" />
<!--                <include name="test_481" />-->
<!--                <include name="test_482" />-->
        </methods>
    </class>
</classes>
</test>

输出结果如下:

this is a comment
<include name="test_481" />
<include name="test_482" />

您可以覆盖 NSXMLParserDelegate 中的 parser(_:foundComment:) 方法,以便在找到评论时收到通知。

func parser(parser: NSXMLParser, foundComment comment: String) {
    print(comment)
}