Search code examples
iosswiftmacosswift2osx-elcapitan

read comment or commented node from xml in swift


First, consider a xml structure like below:

<!-- 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>

From this xml i can read all the included name using NSXmlParser.

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

    if(elementName == "include"){

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

But the commented test cases are not parsed using this method.

Is there any way to read only the commented testcase name from xml using the xml parser or this is not possible to read the commented tag from xml in swift?

No solution found over the net.

I am using

xcode 7.3 OSX 10.11 and swift 2.2

Solution

  • First of all, i want to thanks AlexanderMomchliov for his great suggestion and I hope you know how to parse a xml file.

    parser has some overloaded methods, from there, just call this:

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

    if the xml file is has some commented node like:

    <!-- 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>
    

    Than the output will be like:

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