Code Monkey home page Code Monkey logo

shxmlparser's Introduction

SHXMLParser Build Status Cocoa Pod

Easy to use automatic XML Parser built on NSXMLParser. Convert your XML data into native 'Obj C object' in just 2 steps.

Installation:

CocoaPod

  1. Add pod 'SHXMLParser' to your Podfile.
  2. Run pod install

Manual

Add the library folder SHXMLParser to your project.

How To Use:

XML Sample

<rss>
    <channel>
        <item id='1'>
            <title>I am under surveillance by Canadian agents, my computer has been backdoored</title>
            <link>http://log.nadim.cc/?p=110</link>
            <comments>http://news.ycombinator.com/item?id=5194489</comments>
            <description><a href="http://news.ycombinator.com/item?id=5194489">Comments</a></description>
        </item>
        <item id='2'>
            <title>Why I Like Go</title>
            <link>https://gist.github.com/freeformz/4746274</link>
            <description><a href="http://news.ycombinator.com/item?id=5195257">Comments</a></description>
        </item>
    </channel>
</rss>

Use the below code to get the Array of Dictionary Objects from an XML like the one above. Attributes will be taken automatically.

SHXMLParser		*parser			= [[SHXMLParser alloc] init];
NSDictionary	*resultObject	= [parser parseData:self.webServicesData];
NSArray			*dataArray		= [SHXMLParser getDataAtPath:@"rss.channel.item" fromResultObject:resultObject];

Note: In case, the path used ("rss.channel.item") contains only one item, NSDictionary will be returned instead of NSArray. If the path leads to leaf with just a string value, string value will be returned.

If you want to convert dictionary objects in your data array into a class object for type safety, use code below.

NSArray *classVariables = [NSArray arrayWithObjects:@"title", @"link", @"comments", @"description", nil];
self.dataItems = [SHXMLParser convertDictionaryArray:dataArray toObjectArrayWithClassName:@"DataItem" classVariables:classVariables];

Note that for above conversion, class named 'DataItem' should contain public variables as mentioned in array 'classVariables', otherwise you will get runtime error.

Thats it, no need to write lot of node and element specific parsing code to retrieve data using NSXMLParser from XML data.

A small utility class method named getAsArray is there to get the nsdictionary object inside nsarray. This has been added because sometimes when the user is expecting an array from the parser it will be returned as nsdictionary if it contains only one object. Using this function will make sure that array is returned.

Requirements

It requires iOS 5.0+ and uses ARC. It is built and tested using Xcode 4.5+.

Feel free to fork and update the library

License

MIT License

shxmlparser's People

Contributors

simhanature avatar

Watchers

James Cloos avatar yugh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.