How to Upload an XML using Document Picker and parse it

First I added 2 properties in the plist as
  1. Support opening documents in place = YES

  2. Application supports iTunes file sharing = YES

  3. Then Created a button to upload the xml file

Code Block
@IBAction func didTapUploadFile(_ sender: Any) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeItem as String], in: .import)
        documentPicker.delegate = self
        documentPicker.allowsMultipleSelection = false
        present(documentPicker, animated: true, completion: nil)
    }


4. Added a extension as

Code Block
extension ViewController: UIDocumentPickerDelegate {
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let selectedFileUrl = urls.first else { return }
let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
        let sandboxFile = dir.appendingPathComponent(selectedFileUrl.lastPathComponent)
if FileManager.default.fileExists(atPath: sandboxFile.path) {
            print("File Already\(config)")
        } else {
            do {                
                let contentFile = try NSString(contentsOf: selectedFileUrl, encoding: String.Encoding.utf8.rawValue)
                config = contentFile as String
                print("File Upload\(config)")
            } catch {
                print("Error \(error)")
            }
        }
}
}


In this case I'm able to upload a file but not unable to parse the file is there some way to parse the file and get the content inside the xml file



The url passed to you is security-scoped bookmarked so you need to wrap your url accessing code with start/stopAccessingSecurityScopedResource(), as shown below:

Code Block
if url.startAccessingSecurityScopedResource() {
  …… /* Your code to access the file.*/
  url.stopAccessingSecurityScopedResource()
}

For more information, please see security-scope bookmark.
Will get back to you on this.
```



[

link

Text](https://www.example.com/)

(https://sap.apple.com/orums/content/attachment/5cd991d2-5abd-4b2a-a231-d3){: .log-attachment}
[hhhh](https://developer.apple.com.cn/forums/content/attachment/28ae61e1-7777-4ceb-b8d2-841a13e7c060)

```
```



[

link

Text](https://www.example.com/)

(https://sap.apple.com/orums/content/attachment/5cd991d2-5abd-4b2a-a231-d3){: .log-attachment}
[hhhh](https://developer.apple.com.cn/forums/content/attachment/28ae61e1-7777-4ceb-b8d2-841a13e7c060)

```



How to Upload an XML using Document Picker and parse it
 
 
Q