├── .DS_Store ├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── XMLProcessor.xcodeproj └── project.pbxproj └── XMLProcessor ├── Assets.xcassets ├── AccentColor.colorset │ └── Contents.json ├── AppIcon.appiconset │ └── Contents.json └── Contents.json ├── ContentView.swift ├── MarkdownView.swift ├── Preview Content └── Preview Assets.xcassets │ └── Contents.json ├── XMLProcessor.swift └── XMLProcessorApp.swift /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chockenberry/XMLProcessor/05e50c834c502a35782f1a7d3a73706620cd7ff7/.DS_Store -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Craig Hockenberry 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XMLProcessor 2 | A simple XML parser that creates a JSON object graph in Swift. 3 | 4 | ## Example 5 | 6 | Parsing a string of XML into an object graph is simple: 7 | 8 | ```swift 9 | let data = "value".data(using: .utf8)! 10 | 11 | let processor = XMLProcessor() 12 | if let object = processor.parse(data: data) { 13 | if let root = object["root"] as? Dictionary { 14 | // do something with root dictionary 15 | } 16 | } 17 | ``` 18 | 19 | ## Processing 20 | 21 | To deal with the differences between XML and JavaScript objects (JSON), some processing is done on the XML. 22 | 23 | _NOTE:_ Do not assume that the order of the keys in the object dictionaries will be the same as they occurred in the XML. No order is preserved during processing (as is the case with JSON parsing). 24 | 25 | If the XML has multiple nodes with the same name, they are put into an array. For example, the following XML: 26 | 27 | ```xml 28 | 29 | Example 30 | 31 | First 32 | 33 | 34 | Second 35 | 36 | 37 | ``` 38 | 39 | Will generate: 40 | 41 | ```json 42 | { 43 | "root": { 44 | "metadata": "Example", 45 | "entry": [ 46 | { 47 | "title": "First" 48 | }, 49 | { 50 | "title": "Second" 51 | } 52 | ] 53 | } 54 | } 55 | ``` 56 | 57 | When evaluating the result, you can use Swift's type coercion. Using the example above, `root["entry"] as? Array` will return a value. 58 | 59 | A node’s attributes are stored in a sibling object with a "$attrs" key. The dollar sign was chosen because it’s an invalid XML node name, but is a valid JavaScript property name. This makes it easier to access with a path. 60 | 61 | For example, this XML: 62 | 63 | ```xml 64 | 65 | value 66 | 67 | ``` 68 | 69 | Produces: 70 | 71 | ```json 72 | { 73 | "root" : { 74 | "node" : "value", 75 | "node$attrs" : { 76 | "first" : "1", 77 | "second" : "2", 78 | "third" : "3" 79 | } 80 | } 81 | } 82 | ``` 83 | 84 | Note that these two processing steps can be combined in some cases. An example is multiple link nodes with nothing but attributes: 85 | 86 | ```xml 87 | 88 | 89 | 90 | 91 | ``` 92 | 93 | Will only produce attribute dictionaries: 94 | 95 | ```json 96 | { 97 | "root" : { 98 | "link$attrs" : [ 99 | { 100 | "first" : "abc", 101 | "second" : "def" 102 | }, 103 | { 104 | "first" : "hij", 105 | "second" : "klm" 106 | } 107 | ] 108 | } 109 | } 110 | ``` 111 | 112 | Note also that text that’s not a part of a node will be ignored. For example: 113 | 114 | ```xml 115 | 116 | text 117 | value 118 | 119 | ``` 120 | 121 | Results: 122 | 123 | ```json 124 | { 125 | "root" : { 126 | "node" : "value" 127 | } 128 | } 129 | ``` 130 | 131 | This functionality should be enough to parse XML generated from hierarchical data, such as an RSS feed generated by a WordPress database of posts. 132 | 133 | ## Sample 134 | 135 | A sample app in SwiftUI shows how `XMLProcessor` can be used to read an Atom RSS feed and use it to create a `Text` view generated with Markdown from the feed. 136 | 137 | -------------------------------------------------------------------------------- /XMLProcessor.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 56; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 44EFD4F52A1300F700239BC1 /* XMLProcessorApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44EFD4F42A1300F700239BC1 /* XMLProcessorApp.swift */; }; 11 | 44EFD4F72A1300F700239BC1 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44EFD4F62A1300F700239BC1 /* ContentView.swift */; }; 12 | 44EFD4F92A1300F900239BC1 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 44EFD4F82A1300F900239BC1 /* Assets.xcassets */; }; 13 | 44EFD4FC2A1300F900239BC1 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 44EFD4FB2A1300F900239BC1 /* Preview Assets.xcassets */; }; 14 | 44EFD5032A13011300239BC1 /* XMLProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44EFD5022A13011300239BC1 /* XMLProcessor.swift */; }; 15 | 44EFD5052A141BE700239BC1 /* MarkdownView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 44EFD5042A141BE700239BC1 /* MarkdownView.swift */; }; 16 | 44EFD5072A144EDD00239BC1 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 44EFD5062A144EDD00239BC1 /* README.md */; }; 17 | /* End PBXBuildFile section */ 18 | 19 | /* Begin PBXFileReference section */ 20 | 44EFD4F12A1300F700239BC1 /* XMLProcessor.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = XMLProcessor.app; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | 44EFD4F42A1300F700239BC1 /* XMLProcessorApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLProcessorApp.swift; sourceTree = ""; }; 22 | 44EFD4F62A1300F700239BC1 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; 23 | 44EFD4F82A1300F900239BC1 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 24 | 44EFD4FB2A1300F900239BC1 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 25 | 44EFD5022A13011300239BC1 /* XMLProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = XMLProcessor.swift; sourceTree = ""; }; 26 | 44EFD5042A141BE700239BC1 /* MarkdownView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MarkdownView.swift; sourceTree = ""; }; 27 | 44EFD5062A144EDD00239BC1 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 28 | /* End PBXFileReference section */ 29 | 30 | /* Begin PBXFrameworksBuildPhase section */ 31 | 44EFD4EE2A1300F700239BC1 /* Frameworks */ = { 32 | isa = PBXFrameworksBuildPhase; 33 | buildActionMask = 2147483647; 34 | files = ( 35 | ); 36 | runOnlyForDeploymentPostprocessing = 0; 37 | }; 38 | /* End PBXFrameworksBuildPhase section */ 39 | 40 | /* Begin PBXGroup section */ 41 | 44EFD4E82A1300F700239BC1 = { 42 | isa = PBXGroup; 43 | children = ( 44 | 44EFD5062A144EDD00239BC1 /* README.md */, 45 | 44EFD4F32A1300F700239BC1 /* XMLProcessor */, 46 | 44EFD4F22A1300F700239BC1 /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | 44EFD4F22A1300F700239BC1 /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | 44EFD4F12A1300F700239BC1 /* XMLProcessor.app */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | 44EFD4F32A1300F700239BC1 /* XMLProcessor */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | 44EFD5022A13011300239BC1 /* XMLProcessor.swift */, 62 | 44EFD4F42A1300F700239BC1 /* XMLProcessorApp.swift */, 63 | 44EFD4F62A1300F700239BC1 /* ContentView.swift */, 64 | 44EFD5042A141BE700239BC1 /* MarkdownView.swift */, 65 | 44EFD4F82A1300F900239BC1 /* Assets.xcassets */, 66 | 44EFD4FA2A1300F900239BC1 /* Preview Content */, 67 | ); 68 | path = XMLProcessor; 69 | sourceTree = ""; 70 | }; 71 | 44EFD4FA2A1300F900239BC1 /* Preview Content */ = { 72 | isa = PBXGroup; 73 | children = ( 74 | 44EFD4FB2A1300F900239BC1 /* Preview Assets.xcassets */, 75 | ); 76 | path = "Preview Content"; 77 | sourceTree = ""; 78 | }; 79 | /* End PBXGroup section */ 80 | 81 | /* Begin PBXNativeTarget section */ 82 | 44EFD4F02A1300F700239BC1 /* XMLProcessor */ = { 83 | isa = PBXNativeTarget; 84 | buildConfigurationList = 44EFD4FF2A1300F900239BC1 /* Build configuration list for PBXNativeTarget "XMLProcessor" */; 85 | buildPhases = ( 86 | 44EFD4ED2A1300F700239BC1 /* Sources */, 87 | 44EFD4EE2A1300F700239BC1 /* Frameworks */, 88 | 44EFD4EF2A1300F700239BC1 /* Resources */, 89 | ); 90 | buildRules = ( 91 | ); 92 | dependencies = ( 93 | ); 94 | name = XMLProcessor; 95 | productName = XMLProcessor; 96 | productReference = 44EFD4F12A1300F700239BC1 /* XMLProcessor.app */; 97 | productType = "com.apple.product-type.application"; 98 | }; 99 | /* End PBXNativeTarget section */ 100 | 101 | /* Begin PBXProject section */ 102 | 44EFD4E92A1300F700239BC1 /* Project object */ = { 103 | isa = PBXProject; 104 | attributes = { 105 | BuildIndependentTargetsInParallel = 1; 106 | LastSwiftUpdateCheck = 1430; 107 | LastUpgradeCheck = 1430; 108 | TargetAttributes = { 109 | 44EFD4F02A1300F700239BC1 = { 110 | CreatedOnToolsVersion = 14.3; 111 | }; 112 | }; 113 | }; 114 | buildConfigurationList = 44EFD4EC2A1300F700239BC1 /* Build configuration list for PBXProject "XMLProcessor" */; 115 | compatibilityVersion = "Xcode 14.0"; 116 | developmentRegion = en; 117 | hasScannedForEncodings = 0; 118 | knownRegions = ( 119 | en, 120 | Base, 121 | ); 122 | mainGroup = 44EFD4E82A1300F700239BC1; 123 | productRefGroup = 44EFD4F22A1300F700239BC1 /* Products */; 124 | projectDirPath = ""; 125 | projectRoot = ""; 126 | targets = ( 127 | 44EFD4F02A1300F700239BC1 /* XMLProcessor */, 128 | ); 129 | }; 130 | /* End PBXProject section */ 131 | 132 | /* Begin PBXResourcesBuildPhase section */ 133 | 44EFD4EF2A1300F700239BC1 /* Resources */ = { 134 | isa = PBXResourcesBuildPhase; 135 | buildActionMask = 2147483647; 136 | files = ( 137 | 44EFD5072A144EDD00239BC1 /* README.md in Resources */, 138 | 44EFD4FC2A1300F900239BC1 /* Preview Assets.xcassets in Resources */, 139 | 44EFD4F92A1300F900239BC1 /* Assets.xcassets in Resources */, 140 | ); 141 | runOnlyForDeploymentPostprocessing = 0; 142 | }; 143 | /* End PBXResourcesBuildPhase section */ 144 | 145 | /* Begin PBXSourcesBuildPhase section */ 146 | 44EFD4ED2A1300F700239BC1 /* Sources */ = { 147 | isa = PBXSourcesBuildPhase; 148 | buildActionMask = 2147483647; 149 | files = ( 150 | 44EFD4F72A1300F700239BC1 /* ContentView.swift in Sources */, 151 | 44EFD4F52A1300F700239BC1 /* XMLProcessorApp.swift in Sources */, 152 | 44EFD5032A13011300239BC1 /* XMLProcessor.swift in Sources */, 153 | 44EFD5052A141BE700239BC1 /* MarkdownView.swift in Sources */, 154 | ); 155 | runOnlyForDeploymentPostprocessing = 0; 156 | }; 157 | /* End PBXSourcesBuildPhase section */ 158 | 159 | /* Begin XCBuildConfiguration section */ 160 | 44EFD4FD2A1300F900239BC1 /* Debug */ = { 161 | isa = XCBuildConfiguration; 162 | buildSettings = { 163 | ALWAYS_SEARCH_USER_PATHS = NO; 164 | CLANG_ANALYZER_NONNULL = YES; 165 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 166 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 167 | CLANG_ENABLE_MODULES = YES; 168 | CLANG_ENABLE_OBJC_ARC = YES; 169 | CLANG_ENABLE_OBJC_WEAK = YES; 170 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 171 | CLANG_WARN_BOOL_CONVERSION = YES; 172 | CLANG_WARN_COMMA = YES; 173 | CLANG_WARN_CONSTANT_CONVERSION = YES; 174 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 175 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 176 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 177 | CLANG_WARN_EMPTY_BODY = YES; 178 | CLANG_WARN_ENUM_CONVERSION = YES; 179 | CLANG_WARN_INFINITE_RECURSION = YES; 180 | CLANG_WARN_INT_CONVERSION = YES; 181 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 182 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 183 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 184 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 185 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 186 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 187 | CLANG_WARN_STRICT_PROTOTYPES = YES; 188 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 189 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 190 | CLANG_WARN_UNREACHABLE_CODE = YES; 191 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 192 | COPY_PHASE_STRIP = NO; 193 | DEBUG_INFORMATION_FORMAT = dwarf; 194 | ENABLE_STRICT_OBJC_MSGSEND = YES; 195 | ENABLE_TESTABILITY = YES; 196 | GCC_C_LANGUAGE_STANDARD = gnu11; 197 | GCC_DYNAMIC_NO_PIC = NO; 198 | GCC_NO_COMMON_BLOCKS = YES; 199 | GCC_OPTIMIZATION_LEVEL = 0; 200 | GCC_PREPROCESSOR_DEFINITIONS = ( 201 | "DEBUG=1", 202 | "$(inherited)", 203 | ); 204 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 205 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 206 | GCC_WARN_UNDECLARED_SELECTOR = YES; 207 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 208 | GCC_WARN_UNUSED_FUNCTION = YES; 209 | GCC_WARN_UNUSED_VARIABLE = YES; 210 | IPHONEOS_DEPLOYMENT_TARGET = 16.4; 211 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 212 | MTL_FAST_MATH = YES; 213 | ONLY_ACTIVE_ARCH = YES; 214 | SDKROOT = iphoneos; 215 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 216 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 217 | }; 218 | name = Debug; 219 | }; 220 | 44EFD4FE2A1300F900239BC1 /* Release */ = { 221 | isa = XCBuildConfiguration; 222 | buildSettings = { 223 | ALWAYS_SEARCH_USER_PATHS = NO; 224 | CLANG_ANALYZER_NONNULL = YES; 225 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 226 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++20"; 227 | CLANG_ENABLE_MODULES = YES; 228 | CLANG_ENABLE_OBJC_ARC = YES; 229 | CLANG_ENABLE_OBJC_WEAK = YES; 230 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 231 | CLANG_WARN_BOOL_CONVERSION = YES; 232 | CLANG_WARN_COMMA = YES; 233 | CLANG_WARN_CONSTANT_CONVERSION = YES; 234 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 235 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 236 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 237 | CLANG_WARN_EMPTY_BODY = YES; 238 | CLANG_WARN_ENUM_CONVERSION = YES; 239 | CLANG_WARN_INFINITE_RECURSION = YES; 240 | CLANG_WARN_INT_CONVERSION = YES; 241 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 242 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 243 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 244 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 245 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 246 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 247 | CLANG_WARN_STRICT_PROTOTYPES = YES; 248 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 249 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 250 | CLANG_WARN_UNREACHABLE_CODE = YES; 251 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 252 | COPY_PHASE_STRIP = NO; 253 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 254 | ENABLE_NS_ASSERTIONS = NO; 255 | ENABLE_STRICT_OBJC_MSGSEND = YES; 256 | GCC_C_LANGUAGE_STANDARD = gnu11; 257 | GCC_NO_COMMON_BLOCKS = YES; 258 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 259 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 260 | GCC_WARN_UNDECLARED_SELECTOR = YES; 261 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 262 | GCC_WARN_UNUSED_FUNCTION = YES; 263 | GCC_WARN_UNUSED_VARIABLE = YES; 264 | IPHONEOS_DEPLOYMENT_TARGET = 16.4; 265 | MTL_ENABLE_DEBUG_INFO = NO; 266 | MTL_FAST_MATH = YES; 267 | SDKROOT = iphoneos; 268 | SWIFT_COMPILATION_MODE = wholemodule; 269 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 270 | VALIDATE_PRODUCT = YES; 271 | }; 272 | name = Release; 273 | }; 274 | 44EFD5002A1300F900239BC1 /* Debug */ = { 275 | isa = XCBuildConfiguration; 276 | buildSettings = { 277 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 278 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 279 | CODE_SIGN_STYLE = Automatic; 280 | CURRENT_PROJECT_VERSION = 1; 281 | DEVELOPMENT_ASSET_PATHS = "\"XMLProcessor/Preview Content\""; 282 | DEVELOPMENT_TEAM = RYQWBTQRPT; 283 | ENABLE_PREVIEWS = YES; 284 | GENERATE_INFOPLIST_FILE = YES; 285 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 286 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 287 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 288 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 289 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 290 | LD_RUNPATH_SEARCH_PATHS = ( 291 | "$(inherited)", 292 | "@executable_path/Frameworks", 293 | ); 294 | MARKETING_VERSION = 1.0; 295 | PRODUCT_BUNDLE_IDENTIFIER = com.iconfactory.XMLProcessor; 296 | PRODUCT_NAME = "$(TARGET_NAME)"; 297 | SWIFT_EMIT_LOC_STRINGS = YES; 298 | SWIFT_VERSION = 5.0; 299 | TARGETED_DEVICE_FAMILY = "1,2"; 300 | }; 301 | name = Debug; 302 | }; 303 | 44EFD5012A1300F900239BC1 /* Release */ = { 304 | isa = XCBuildConfiguration; 305 | buildSettings = { 306 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 307 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 308 | CODE_SIGN_STYLE = Automatic; 309 | CURRENT_PROJECT_VERSION = 1; 310 | DEVELOPMENT_ASSET_PATHS = "\"XMLProcessor/Preview Content\""; 311 | DEVELOPMENT_TEAM = RYQWBTQRPT; 312 | ENABLE_PREVIEWS = YES; 313 | GENERATE_INFOPLIST_FILE = YES; 314 | INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES; 315 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 316 | INFOPLIST_KEY_UILaunchScreen_Generation = YES; 317 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 318 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 319 | LD_RUNPATH_SEARCH_PATHS = ( 320 | "$(inherited)", 321 | "@executable_path/Frameworks", 322 | ); 323 | MARKETING_VERSION = 1.0; 324 | PRODUCT_BUNDLE_IDENTIFIER = com.iconfactory.XMLProcessor; 325 | PRODUCT_NAME = "$(TARGET_NAME)"; 326 | SWIFT_EMIT_LOC_STRINGS = YES; 327 | SWIFT_VERSION = 5.0; 328 | TARGETED_DEVICE_FAMILY = "1,2"; 329 | }; 330 | name = Release; 331 | }; 332 | /* End XCBuildConfiguration section */ 333 | 334 | /* Begin XCConfigurationList section */ 335 | 44EFD4EC2A1300F700239BC1 /* Build configuration list for PBXProject "XMLProcessor" */ = { 336 | isa = XCConfigurationList; 337 | buildConfigurations = ( 338 | 44EFD4FD2A1300F900239BC1 /* Debug */, 339 | 44EFD4FE2A1300F900239BC1 /* Release */, 340 | ); 341 | defaultConfigurationIsVisible = 0; 342 | defaultConfigurationName = Release; 343 | }; 344 | 44EFD4FF2A1300F900239BC1 /* Build configuration list for PBXNativeTarget "XMLProcessor" */ = { 345 | isa = XCConfigurationList; 346 | buildConfigurations = ( 347 | 44EFD5002A1300F900239BC1 /* Debug */, 348 | 44EFD5012A1300F900239BC1 /* Release */, 349 | ); 350 | defaultConfigurationIsVisible = 0; 351 | defaultConfigurationName = Release; 352 | }; 353 | /* End XCConfigurationList section */ 354 | }; 355 | rootObject = 44EFD4E92A1300F700239BC1 /* Project object */; 356 | } 357 | -------------------------------------------------------------------------------- /XMLProcessor/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "color" : { 5 | "color-space" : "srgb", 6 | "components" : { 7 | "alpha" : "1.000", 8 | "blue" : "0.573", 9 | "green" : "0.186", 10 | "red" : "1.000" 11 | } 12 | }, 13 | "idiom" : "universal" 14 | } 15 | ], 16 | "info" : { 17 | "author" : "xcode", 18 | "version" : 1 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /XMLProcessor/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "platform" : "ios", 6 | "size" : "1024x1024" 7 | } 8 | ], 9 | "info" : { 10 | "author" : "xcode", 11 | "version" : 1 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /XMLProcessor/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /XMLProcessor/ContentView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ContentView.swift 3 | // XMLProcessor 4 | // 5 | // Created by Craig Hockenberry on 5/15/23. 6 | // 7 | 8 | import SwiftUI 9 | 10 | struct Entry: Hashable { 11 | var title: String 12 | var date: Date 13 | var link: String 14 | } 15 | 16 | struct ContentView: View { 17 | 18 | @State private var entries: Array = [] 19 | 20 | var body: some View { 21 | NavigationStack { 22 | Form { 23 | ForEach(entries, id: \.self) { entry in 24 | NavigationLink { 25 | MarkdownView(entry: entry) 26 | } label: { 27 | HStack { 28 | Text(entry.title) 29 | Spacer() 30 | Text(entry.date, style: .date) 31 | .font(.caption) 32 | .foregroundColor(.secondary) 33 | } 34 | } 35 | } 36 | } 37 | .formStyle(.grouped) 38 | .navigationTitle("Daring Fireball Atom Feed") 39 | .navigationBarTitleDisplayMode(.inline) 40 | .onAppear { 41 | Task { 42 | let url = URL(string: "https://daringfireball.net/feeds/main")! 43 | if let (xml, _) = try? await URLSession.shared.data(from: url) { 44 | let processor = XMLProcessor() 45 | if let object = processor.parse(data: xml, debug: false) { 46 | 47 | // NOTE: At this point, the object can be turned into JSON with: 48 | if let data = try? JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted]) { 49 | if let debug = String(data: data, encoding: .utf8) { 50 | print(debug) 51 | } 52 | } 53 | 54 | // NOTE: It sure would be nice to have an easy way to unbox the feed Dictionary with a Decoder, 55 | // but there isn't, so we'll just poke around in the data and unbox the values ourself. 56 | if let feed = object["feed"] as? Dictionary { 57 | if let entries = feed["entry"] as? Array { 58 | self.entries = entries.compactMap({ element in 59 | if let entry = element as? Dictionary { 60 | if let title = entry["title"] as? String, 61 | let published = entry["published"] as? String, 62 | let linkAttributes = entry["link$attrs"] as? Array { 63 | var relatedLink: String? 64 | var alternateLink: String? 65 | for linkAttribute in linkAttributes { 66 | if let dictionary = linkAttribute as? Dictionary { 67 | if let linkRelationship = dictionary["rel"] as? String { 68 | if linkRelationship == "related" { 69 | if let linkValue = dictionary["href"] as? String { 70 | relatedLink = linkValue 71 | } 72 | } 73 | else if linkRelationship == "alternate" { 74 | if let linkValue = dictionary["href"] as? String { 75 | alternateLink = linkValue 76 | } 77 | } 78 | } 79 | } 80 | } 81 | let link = relatedLink ?? alternateLink ?? "https://daringfireball.net" 82 | if let date = ISO8601DateFormatter().date(from: published) { 83 | return Entry(title: title, date: date, link: link) 84 | } 85 | } 86 | } 87 | return nil 88 | }) 89 | } 90 | } 91 | } 92 | } 93 | } 94 | } 95 | } 96 | } 97 | } 98 | 99 | struct ContentView_Previews: PreviewProvider { 100 | static var previews: some View { 101 | ContentView() 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /XMLProcessor/MarkdownView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // MarkdownView.swift 3 | // XMLProcessor 4 | // 5 | // Created by Craig Hockenberry on 5/16/23. 6 | // 7 | 8 | import SwiftUI 9 | 10 | struct MarkdownView: View { 11 | let entry: Entry 12 | 13 | @State private var markdown: String = "**Loading…**" 14 | 15 | var body: some View { 16 | ScrollView { 17 | let options = AttributedString.MarkdownParsingOptions(allowsExtendedAttributes: true, interpretedSyntax: .full) 18 | let attributedText = (try? AttributedString(markdown: markdown, options: options)) ?? AttributedString("Parse Failure") 19 | Text(attributedText.styledText) 20 | .padding() 21 | .onAppear { 22 | Task { 23 | if let url = URL(string: entry.link + ".text") { 24 | if let (data, _) = try? await URLSession.shared.data(from: url) { 25 | if let string = String(data: data, encoding: .utf8) { 26 | print(string) 27 | markdown = string 28 | return 29 | } 30 | } 31 | } 32 | markdown = "_Load Failure_" 33 | } 34 | } 35 | } 36 | .navigationTitle(entry.title) 37 | .navigationBarTitleDisplayMode(.inline) 38 | } 39 | } 40 | 41 | extension AttributedString { 42 | 43 | // NOTE: For more information on how this styling works, check out Frank Rausch's project on GitHub: 44 | // https://github.com/frankrausch/AttributedStringStyledMarkdown 45 | 46 | var styledText: AttributedString { 47 | get { 48 | let fontSize: CGFloat = 18 49 | 50 | let font = UIFont.systemFont(ofSize: fontSize) 51 | let italicFont = UIFont.italicSystemFont(ofSize: fontSize) 52 | let boldFont = UIFont.boldSystemFont(ofSize: fontSize) 53 | let boldItalicFont: UIFont 54 | if let fontDescriptor = font.fontDescriptor.withSymbolicTraits([.traitBold, .traitItalic]) { 55 | boldItalicFont = UIFont(descriptor: fontDescriptor, size: fontSize) 56 | } 57 | else { 58 | boldItalicFont = boldFont 59 | } 60 | 61 | let foregroundColor = UIColor.label 62 | 63 | do { 64 | var result = self 65 | result.font = font 66 | result.foregroundColor = foregroundColor 67 | 68 | let paragraphStyle = NSMutableParagraphStyle() 69 | paragraphStyle.lineSpacing = 1.0 70 | paragraphStyle.paragraphSpacing = 12.0 71 | paragraphStyle.lineBreakMode = .byTruncatingTail 72 | result.paragraphStyle = paragraphStyle 73 | 74 | for run in result.runs { 75 | let intent = run.attributes[AttributeScopes.FoundationAttributes.InlinePresentationIntentAttribute.self] 76 | if intent == .emphasized { 77 | result[run.range].font = italicFont 78 | } 79 | else if intent == .stronglyEmphasized { 80 | result[run.range].font = boldFont 81 | } 82 | else if intent == [.stronglyEmphasized, .emphasized] { 83 | result[run.range].font = boldItalicFont 84 | } 85 | 86 | if run.attributes[AttributeScopes.FoundationAttributes.LinkAttribute.self] != nil { 87 | result[run.range].link = nil // because UILabel is dumb and wants to make it blue. 88 | result[run.range].foregroundColor = UIColor(named: "AccentColor") 89 | } 90 | } 91 | 92 | for (intentBlock, intentRange) in result.runs[AttributeScopes.FoundationAttributes.PresentationIntentAttribute.self].reversed() { 93 | guard let intentBlock else { continue } 94 | 95 | var listOrdered = false 96 | 97 | // NOTE: The paragraph styles only work in UIKit. They aren't supported yet in SwiftUI. 98 | 99 | for intent in intentBlock.components { 100 | switch intent.kind { 101 | case .paragraph: 102 | let paragraphStyle = NSMutableParagraphStyle() 103 | paragraphStyle.firstLineHeadIndent = 10.0 104 | paragraphStyle.headIndent = 0 105 | paragraphStyle.tailIndent = 0 106 | paragraphStyle.paragraphSpacing = 12.0 107 | 108 | result[intentRange].paragraphStyle = paragraphStyle 109 | case .header(level: let level): 110 | let headerFontSize = (fontSize + 8.0) - (CGFloat(level) * 2.0) 111 | let headerFont = UIFont.systemFont(ofSize: headerFontSize) 112 | 113 | result[intentRange].font = headerFont 114 | case .blockQuote: 115 | let blockParagraphStyle = NSMutableParagraphStyle() 116 | blockParagraphStyle.firstLineHeadIndent = 10.0 117 | blockParagraphStyle.headIndent = 10.0 118 | blockParagraphStyle.tailIndent = -10.0 119 | blockParagraphStyle.paragraphSpacing = 12.0 120 | 121 | result[intentRange].paragraphStyle = blockParagraphStyle 122 | result[intentRange].foregroundColor = .secondary 123 | case .orderedList: 124 | listOrdered = true 125 | case .unorderedList: 126 | listOrdered = false 127 | case .listItem(ordinal: let ordinal): 128 | if listOrdered { 129 | result.characters.insert(contentsOf: "\(ordinal) ", at: intentRange.lowerBound) 130 | } 131 | else { 132 | result.characters.insert(contentsOf: "• ", at: intentRange.lowerBound) 133 | } 134 | default: 135 | break 136 | } 137 | } 138 | 139 | result.characters.insert(contentsOf: "\n\n", at: intentRange.lowerBound) 140 | } 141 | return result 142 | } 143 | } 144 | } 145 | 146 | } 147 | 148 | struct MarkdownView_Previews: PreviewProvider { 149 | static var previews: some View { 150 | let entry = Entry(title: "Test Entry", date: Date(), link: "https://daringfireball.net/linked/2023/05/02/browser-company-swift-windows") 151 | MarkdownView(entry: entry) 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /XMLProcessor/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /XMLProcessor/XMLProcessor.swift: -------------------------------------------------------------------------------- 1 | // 2 | // XMLProcessor.swift 3 | // XMLProcessor 4 | // 5 | // Created by Craig Hockenberry on 5/15/23. 6 | // 7 | 8 | import Foundation 9 | 10 | class XMLProcessor: NSObject { 11 | 12 | typealias Collector = Dictionary 13 | 14 | var textValue: String? 15 | var textNode: Bool = false 16 | 17 | var elementNameStack: [String] = [] 18 | var elementAttributesStack: [[String : String]] = [] 19 | 20 | var collectorStack: [Collector] = [[:]] 21 | 22 | var level = 0 23 | var debug = false 24 | 25 | func parse(data: Data, debug: Bool = false) -> (Collector?) { 26 | self.debug = debug 27 | 28 | let parser = XMLParser(data: data) 29 | parser.delegate = self 30 | parser.parse() 31 | 32 | if debug { 33 | if let data = try? JSONSerialization.data(withJSONObject: collectorStack.first!, options: [.prettyPrinted]) { 34 | if let debug = String(data: data, encoding: .utf8) { 35 | print(debug) 36 | } 37 | } 38 | } 39 | 40 | return collectorStack.first 41 | } 42 | 43 | } 44 | 45 | extension XMLProcessor: XMLParserDelegate { 46 | 47 | private func levelMessage(_ message: String) { 48 | if debug { 49 | let indent = String(repeating: ". ", count: level) 50 | print("\(indent)\(message)") 51 | } 52 | } 53 | 54 | private func updateCollector(_ currentCollector: Collector, elementName: String, elementValue: Any) -> Collector { 55 | var collector = currentCollector 56 | 57 | if collector.keys.contains(elementName) { 58 | // NOTE: If the dictionary already contains the key for the element name, put the existing values, and 59 | // any subsequent values, into an array. 60 | if var array = collector[elementName] as? Array { 61 | array.append(elementValue) 62 | collector[elementName] = array 63 | } 64 | else { 65 | let array = [collector[elementName] as Any, elementValue] 66 | collector[elementName] = array 67 | } 68 | } 69 | else { 70 | collector[elementName] = elementValue 71 | } 72 | 73 | return collector 74 | } 75 | 76 | private func elementAttributeName(for elementName: String) -> String { 77 | return elementName + "$attrs" 78 | } 79 | 80 | // MARK: - 81 | 82 | func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String] = [:]) { 83 | levelMessage("start \(elementName)") 84 | level += 1 85 | 86 | textValue = nil 87 | 88 | elementNameStack.append(elementName) 89 | elementAttributesStack.append(attributeDict) 90 | 91 | collectorStack.append([:]) 92 | } 93 | 94 | func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) { 95 | level -= 1 96 | levelMessage("end \(elementName) \(textNode ? " (TEXT) " : "")") 97 | 98 | let elementName = elementNameStack.removeLast() 99 | let elementAttributes = elementAttributesStack.removeLast() 100 | 101 | if let lastCollector = collectorStack.popLast() { 102 | if var parentCollector = collectorStack.popLast() { 103 | if textNode { 104 | if let textValue { 105 | parentCollector = updateCollector(parentCollector, elementName: elementName, elementValue: textValue) 106 | } 107 | if !elementAttributes.isEmpty { 108 | let attributesElementName = elementAttributeName(for: elementName) 109 | parentCollector = updateCollector(parentCollector, elementName: attributesElementName, elementValue: elementAttributes) 110 | } 111 | collectorStack.append(parentCollector) 112 | } 113 | else { 114 | // skip empty element values 115 | if !lastCollector.isEmpty { 116 | parentCollector = updateCollector(parentCollector, elementName: elementName, elementValue: lastCollector) 117 | } 118 | if !elementAttributes.isEmpty { 119 | let attributesElementName = elementAttributeName(for: elementName) 120 | parentCollector = updateCollector(parentCollector, elementName: attributesElementName, elementValue: elementAttributes) 121 | } 122 | collectorStack.append(parentCollector) 123 | } 124 | } 125 | } 126 | 127 | textNode = false 128 | } 129 | 130 | func parser(_ parser: XMLParser, foundCharacters string: String) { 131 | let text = string.trimmingCharacters(in: .whitespacesAndNewlines) 132 | if text.count > 0 { 133 | levelMessage("text = \(text)") 134 | if let textValue { 135 | self.textValue = textValue.appending(text) 136 | } 137 | else { 138 | self.textValue = text 139 | } 140 | textNode = true 141 | } 142 | } 143 | 144 | func parser(_ parser: XMLParser, foundCDATA CDATABlock: Data) { 145 | 146 | // TODO: Handle other encodings? 147 | if let dataText = String(data: CDATABlock, encoding: .utf8) { 148 | levelMessage("dataText = \(dataText)") 149 | if let textValue { 150 | self.textValue = textValue.appending(dataText) 151 | } 152 | else { 153 | self.textValue = dataText 154 | } 155 | textNode = true 156 | } 157 | 158 | } 159 | 160 | func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) { 161 | assert(false, "XML parser failed with \(parseError)") 162 | } 163 | 164 | } 165 | -------------------------------------------------------------------------------- /XMLProcessor/XMLProcessorApp.swift: -------------------------------------------------------------------------------- 1 | // 2 | // XMLProcessorApp.swift 3 | // XMLProcessor 4 | // 5 | // Created by Craig Hockenberry on 5/15/23. 6 | // 7 | 8 | import SwiftUI 9 | 10 | @main 11 | struct XMLProcessorApp: App { 12 | var body: some Scene { 13 | WindowGroup { 14 | ContentView() 15 | } 16 | } 17 | } 18 | --------------------------------------------------------------------------------