Search code examples
swiftxmlkissxml

Parsing large XML files fails--ERROR:Error Domain=DDXMLErrorDomain Code=1 "(null)"


I'm parsing XML using KissXML. I can successfully parse small XML but have problem with large XML. Here's my code

let url = URL(fileURLWithPath: xmlPath!)
let xmlData = try! Data(contentsOf: url)
    do {
        let doc = try DDXMLDocument(data: xmlData, options:0)// This is not working if xml is large (6MB)
        let project = try! doc.nodes(forXPath: "//Project") as! [DDXMLElement]

        for user in project {

            let ProjectName = user.attribute(forName: "ProjectName")!.stringValue
            let userTime = user.attribute(forName: "UseTime")!.stringValue
            print("ProjectName:\(ProjectName!),userTime:\(userTime!)")
        }
    }
    catch {
        print("\(error)") //Get some idea from this error
    }

When parsing 12k XML was successful, but 6M XML was a failure. When parsing large XML(6M),doc equal to nil. I try to use NSXMLParser,the same problem arises,small file can work, big files can't.ERROR:NSXMLParserErrorDomain error 4.


Solution

  • You should not ignore the error using try?, always enclose it in do - catch construct. Use below code and see what error are you getting and then try to resolve it. Don't shoot in the dark, get some idea from the error and if nothing works post your error message in the question.

    do {
        let doc = try DDXMLDocument(data: xmlData, options:0)
        // Your next line of code
    }
    catch {
        print("\(error)") //Get some idea from this error
    }