1

我正在尝试开发一个基于 XIDEL 的非常简单的 XML 验证器。

到目前为止,这是我的 SwiftUI 代码,它执行嵌入式 XIDEL 二进制文件,但我不知道如何传递应该验证的 XML。我的目标是从我的计算机中选择一个 XML 文件,并在我的应用程序内的内容视图中显示 XIDEL 结果。

struct ContentView: View {

@State var message = "Hello, World!"
@State var isRunning = false

var body: some View {
    VStack {
        Text("XML Validator")
            .font(.largeTitle)
            .padding()
        HStack {
            TextField("Message", text: $message)
                .padding(.leading)
            Button(action: {

                let task = Process()
                let bundle = Bundle.main
                let execURL = bundle.url(forResource: "xidel", withExtension: nil)
                guard execURL != nil else {
                    print("XIDEL executable could not be found!")
                    return
                }
                task.executableURL = execURL!
                task.arguments = ["-e=//recipe/flavor1/text() my.xml"]
                do {
                    try task.run()
                    print("XIDEL executed successfully!")
                    self.isRunning = true
                } catch {
                    print("Error running XIDEL: \(error)")
                    self.isRunning = false
                }
                                    
            }) {
                Text("Validate")
            }.disabled(isRunning)
                .padding(.trailing)
        }
    }.frame(maxWidth: .infinity, maxHeight: .infinity)
}

}

在此处输入图像描述

4

1 回答 1

1

尝试以下

    guard let path = Bundle.main.path(forResource: "my", ofType: "xml") else {
       print("my.xml could not be found!")
       return 
    }
    task.arguments = ["-e=//recipe/flavor1/text()", path]
于 2020-08-19T11:05:17.353 回答