0

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.

Amberoot
  • 3
  • 4
  • Is this for iOS? A DOM parser uses a lot of memory for a large XML file. You are better off using `NSXMLParser` and extract just the data you need. – rmaddy Dec 20 '17 at 03:42
  • Yes,this is for iOS.Thank you for your advice,but I need all the data in the XML file, and I'll try NSXMLParser. – Amberoot Dec 20 '17 at 05:46

1 Answers1

0

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
}
Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
  • Thank you for your advice.I use your code,the error is simple:Error Domain=DDXMLErrorDomain Code=1 "(null)". I try NSXMLParser,The same problem arises.Small XML file can work,large XML file doesn't work.Error is: NSXMLParserErrorDomain error 4. – Amberoot Dec 20 '17 at 07:07
  • At line no. 97 in [this code](https://github.com/robbiehanson/KissXML/blob/ca4aaeaa38bedaf0f42b450e7c75170dc3a788da/KissXML/DDXMLDocument.m) it is returning this error from `xmlParseMemory` – Inder Kumar Rathore Dec 20 '17 at 07:16
  • Please check you document is proper or not. – Inder Kumar Rathore Dec 20 '17 at 07:16
  • I tried 11MB xml and it works, it seems your xml has some problem. – Inder Kumar Rathore Dec 20 '17 at 07:41
  • You are right!I changed the file and gave it a try,and it worked. Thank you very much! – Amberoot Dec 20 '17 at 07:58