Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/erkanyildiz/eyxml2nsdictionary
A block based NSXMLParser wrapper for converting XML to NSDictionary on background thread
https://github.com/erkanyildiz/eyxml2nsdictionary
background-thread dictionary ios macos nsxmlparser nsxmlparser-wrapper objective-c parser tvos watchos xml
Last synced: 4 days ago
JSON representation
A block based NSXMLParser wrapper for converting XML to NSDictionary on background thread
- Host: GitHub
- URL: https://github.com/erkanyildiz/eyxml2nsdictionary
- Owner: erkanyildiz
- License: mit
- Created: 2016-10-11T10:37:06.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2018-03-04T16:07:38.000Z (almost 7 years ago)
- Last Synced: 2024-11-10T15:07:31.569Z (about 1 month ago)
- Topics: background-thread, dictionary, ios, macos, nsxmlparser, nsxmlparser-wrapper, objective-c, parser, tvos, watchos, xml
- Language: Objective-C
- Size: 5.86 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EYXML2NSDictionary
An `NSXMLParser` wrapper for converting XML to `NSDictionary` with blocks and background threading.- Based on powerful built-in `NSXMLParser`
- Asynchronous parsing and completion block with resulting `NSDictionary` or `NSError`
- Background threading for not blocking UI while parsing
- Adding attibutes as key-value pairs after prefixing keys with `-` to prevent collision
- Converting repeating elements to proper arrays
- Handling inner texts among tags as an array using `-InnerText` key
- Excluding comments in resulting `NSDictionary`## Usage
- With XML data as `NSData`
```
NSData* XMLData = [Read from disk or fetch from network];[EYXML2NSDictionary parseXMLData:XMLData completion:^(NSDictionary* dict, NSError* error)
{
if (!error)
NSLog(@"Result: %@", dict);
else
NSLog(@"Error: %@", error);
}];
```
- With XML string as `NSString````
NSString* XMLString = @""
""
" A"
" B"
" Fourth Title"
" "
" This is meessage body."
" Attachment 1"
" "
"";[EYXML2NSDictionary parseXMLString:XMLString completion:^(NSDictionary* dict, NSError* error)
{
if (!error)
NSLog(@"Result: %@", dict);
else
NSLog(@"Error: %@", error);
}];
```## Examples
### 1- Input
```
A
B
First Title
Second Title
Third Title
Fourth Title
This is meessage body.
Attachment 1
Attachment 2
Yet more text is here.
Attachment 3
This is the last text.
;
```- Output
```
{
"message": {
"body": {
"attachment": [
"Attachment 1",
"Attachment 2",
"Attachment 3"
],
"-InnerText": [
"This is meessage body.",
"Yet more text is here.",
"This is the last text."
]
},
"to": "A",
"title": [
{
"-anAttribute": "a1",
"-InnerText": "First Title",
"-anotherAttribute": "a2"
},
{
"-anAttribute": "b1",
"-InnerText": "Second Title",
"-anotherAttribute": "b2"
},
"Third Title",
"Fourth Title"
],
"from": "B"
}
}
```### 2
- Input
- Output
```
{
"a": {
"-attr": "A",
"b": [
{
"-attr2": "B1-2",
"-attr1": "B1-1",
"-InnerText": "BBBBB 1"
},
{
"-attr": "B2",
"-InnerText": [
"BBBBB 2_0",
"BBBBB 2_1",
"BBBBB 2_2"
],
"c": [
{
"-attr": "C1",
"-InnerText": "CCCCC 1"
},
{
"-attr": "C2",
"-InnerText": "CCCCC 2",
"d": {
"e": {
"f": "FFFFF",
"-InnerText": [
"EEEEE 1",
"EEEEE 2"
]
},
"-InnerText": [
"DDDDD 1",
"DDDDD 2"
]
}
},
{
"-attr": "C3",
"-InnerText": "CCCCC 3"
}
]
},
"BBBBB 3"
],
"-InnerText": [
"XXXXX",
"YYYYY",
"ZZZZZ"
]
}
}
```