When building a sample application using XMLParser, this project builds and runs properly on macOS 11.3, but fails with the following output on a shiny new Windows 10 installation built following the instructions on the Download page of swift.org:
C:\Users\Me\Desktop\XML>swift build
warning: Failed creating default cache locations, Error Domain=NSCocoaErrorDomain Code=256 "(null)"
warning: Failed creating default cache locations, Error Domain=NSCocoaErrorDomain Code=256 "(null)"
[1/1] Planning build
[1/1] Planning build
* Build Completed!
[1/3] Compiling XMLTest main.swift
C:\Users\Matt Stoker\Desktop\XML\main.swift:3:8: error: missing required modules: 'CFXMLInterface', 'CoreFoundation'
import FoundationXML
^
Package.swift
// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "XMLTest",
platforms: [
.macOS(.v10_15),
.iOS(.v13),
.tvOS(.v13),
.watchOS(.v6),
],
products: [
.executable(
name: "xml-test",
targets: ["XMLTest"]
),
],
dependencies: [
],
targets: [
.executableTarget(
name: "XMLTest",
path: "."
)
]
)
main.swift
import Foundation
#if canImport(FoundationXML)
import FoundationXML
#endif
let svg =
"""
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0.0,0.0,936.0 1008.0">
<path stroke="black" fill="lightblue" d="
M 0.0,0.0 L 936.0,0.0 L 936.0,1008.0 L 0.0,1008.0 L 0.0,0.0 Z
"/>
</svg>
"""
guard let svgData = svg.data(using: .utf8) else { fatalError() }
let parserDelegate = SVGParserDelegate()
let parser = XMLParser.init(data: svgData)
parser.delegate = parserDelegate
print("Parsing...")
let parseCompleted = parser.parse()
print("Parse Completed. Result: \(parseCompleted)")
class SVGParserDelegate: NSObject, XMLParserDelegate {
func parserDidStartDocument(_ parser: XMLParser) {
}
func parserDidEndDocument(_ parser: XMLParser) {
}
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes: [String: String] = [:]) {
switch elementName.lowercased() {
case "svg", "g":
print("Started group tag")
case "path":
guard let pathData = attributes["d"] else {
print("Started path tag with no path data")
return
}
print("Started path: \(pathData)")
default:
break
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
switch elementName.lowercased() {
case "svg", "g":
print("Ended group tag")
case "path":
print("Ended path tag")
default:
break
}
}
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
print("Parse Error: \(parseError)")
}
}
This appears to be the correct way to import Foundation's XML functionality. Anyone have ideas on why the standard library modules can't be found?