首先,考虑如下所示的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中只读取注释过的测试用例名称,或者这不可能在swift中从xml中读取注释过的标签?
在网上找不到解决方案。
我正在使用
xcode 7.3 OSX 10.11 and swift 2.2
发布于 2016-08-05 23:28:39
首先,我想感谢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" />
发布于 2016-08-05 23:53:31
您可以重写NSXMLParserDelegate
中的parser(_:foundComment:)
方法,以便在找到注释时收到通知。
func parser(parser: NSXMLParser, foundComment comment: String) {
print(comment)
}
https://stackoverflow.com/questions/38769042
复制相似问题