Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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

```

BBBBB 1
XXXXX

BBBBB 2_0
CCCCC 1
BBBBB 2_1

CCCCC 2

DDDDD 1

EEEEE 1
FFFFF
EEEEE 2

DDDDD 2

CCCCC 3
BBBBB 2_2

YYYYY
BBBBB 3
ZZZZZ

```

- 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"
]
}
}
```