Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 314
noor Avatar asked Jun 15 '26 09:06

noor


2 Answers

You can override the parser(_:foundComment:) method in your NSXMLParserDelegate to be notified when comments are found.

func parser(parser: NSXMLParser, foundComment comment: String) {
    print(comment)
}
like image 199
Alexander Avatar answered Jun 16 '26 22:06

Alexander


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" />
like image 24
noor Avatar answered Jun 16 '26 23:06

noor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!