Extracting Json from xml text file in iOS Swift - ios

i have to extract and parse json from a text file, i know how to parse json but i'm just unable to extract it correctly from xml format. this is my xml which contains json.
<Data>
<Persons>[{"ID":"2","Name":"Catagory 1"},{"ID":"3","Name":"Catagory 2”</Persons>
<class>[{"ID":"3","Name":"WEAVING”}]</class>
</Data>
what i want is to get json sepratly with its tags, like for example.
"Persons":"[{"ID":"2","Name":"Catagory 1"},{"ID":"3","Name":"Catagory 2”}]"

Please find the sample code for parsing xml below:
import UIKit
import Foundation
class ViewController: UIViewController {
var parser:XMLParser?
var foundChars: String = ""
var personsStr: String = ""
override func viewDidLoad() {
super.viewDidLoad()
parseXML()
// Do any additional setup after loading the view, typically from a nib.
}
func parseXML() {
let str: NSString = "<Data><Persons>[{\"ID\":\"2\",\"Name\":\"Catagory 1\"},{\"ID\":\"3\",\"Name\":\"Catagory 2\"}]</Persons><class>[{\"ID\":\"3\",\"Name\":\"WEAVING\"}]</class></Data>"
if let data = str.data(using: String.Encoding.utf8.rawValue) {
parser = XMLParser.init(data: data)
parser!.delegate = self
parser!.parse()
}
}
}
extension ViewController: XMLParserDelegate {
public func parserDidEndDocument(_ parser: XMLParser) {
debugPrint("Person str is:: " + self.personsStr)
//TODO: You have to build your json object from the PersonStr now
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
self.foundChars = self.foundChars + string
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
debugPrint("end element::" + elementName)
if (elementName == "Persons") {
self.personsStr = self.foundChars
}
self.foundChars = ""
}
}

Related

XMLParser Delegate Methods not Called

I'm trying to abstract an XML parser into a custom class to run it from a VC. It compiles perfectly and my error handler shows up success. However, the actual delegate methods are skipped over. No data is getting parsed.
It all ran fine when I had every running it the VC, but I am now try to get away from spaghetti code.
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let parser = XMLParserHelper()
//try create file for persistent data
//CreatePlist.createPlist()
parser.runParser()
}
}
class XMLParserHelper: NSObject, XMLParserDelegate {
//list type variables to hold XML values (update list base on XML structure):
static var station: String = ""
static var latitude: String = ""
static var longitude: String = ""
private static var code: String = ""
private static var id: String = ""
//reusable method type veriales (do not touch)
static var strXMLData:String = ""
static var currentElement:String = ""
static var passData:Bool=false
static var passName:Bool=false
static var xmlParser = XMLParser()
//parser methods
func runParser(){
let xmlPath = Bundle.main.url(forResource: "station", withExtension: "xml")
let xmlParser = XMLParser(contentsOf: (xmlPath)!)
xmlParser?.delegate = self
let success:Bool = xmlParser!.parse()
xmlParser?.parse()
if success {
print("parse success!")
print(XMLParserHelper.currentElement)
} else {
print("parse failure!")
}
}
private static func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
XMLParserHelper.currentElement=elementName;
if (elementName=="StationDesc" || elementName=="StationLatitude" || elementName=="StationLongitude" || elementName=="StationCode" || elementName=="StationId" ) {
if (elementName=="StationDesc") {
XMLParserHelper.passName=true;
}
XMLParserHelper.passData=true;
}
}
private static func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
XMLParserHelper.currentElement="";
if (elementName=="StationDesc" || elementName=="StationLatitude" || elementName=="StationLongitude" || elementName=="StationCode" || elementName=="StationId" ) {
if(elementName=="StationDesc") {
XMLParserHelper.passName=false;
}
XMLParserHelper.passData=false;
}
}
private static func parser(_ parser: XMLParser, foundCharacters string: String) {
if (XMLParserHelper.passName) {
XMLParserHelper.strXMLData=XMLParserHelper.strXMLData+"\n\n"+string
}
if (XMLParserHelper.passData) {
//ready content for codable struct
switch XMLParserHelper.currentElement {
case "StationDesc":
XMLParserHelper.station = string
case "StationLatitude":
XMLParserHelper.latitude = string
case "StationLongitude":
XMLParserHelper.longitude = string
case "StationCode":
XMLParserHelper.code = string
case "StationId":
XMLParserHelper.id = string
print(string)
default:
XMLParserHelper.id = string
}
}
}
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
print("failure error: ", parseError)
}
}
To make XMLParserDelegate methods work, all the methods needs to be non-static, non-private methods.
So, all the properties should also be non-static.
class XMLParserHelper: NSObject, XMLParserDelegate {
//list type variables to hold XML values (update list base on XML structure):
var station: String = ""
var latitude: String = ""
var longitude: String = ""
private var code: String = ""
private var id: String = ""
//reusable method type veriales (do not touch)
var strXMLData: String = ""
var currentElement: String = ""
var passData: Bool = false
var passName: Bool = false
//parser methods
func runParser() {
let xmlURL = Bundle.main.url(forResource: "station", withExtension: "xml")!
let xmlParser = XMLParser(contentsOf: xmlURL)!
xmlParser.delegate = self
let success = xmlParser.parse()
if success {
print("parse success!")
print(currentElement)
} else {
print("parse failure!")
}
}
//MARK: XMLParserDelegate methods
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
currentElement = elementName
if elementName == "StationDesc"
|| elementName == "StationLatitude"
|| elementName == "StationLongitude"
|| elementName == "StationCode"
|| elementName == "StationId"
{
if elementName == "StationDesc" {
passName = true
}
passData = true
}
}
func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
currentElement = ""
if elementName == "StationDesc"
|| elementName == "StationLatitude"
|| elementName == "StationLongitude"
|| elementName == "StationCode"
|| elementName == "StationId"
{
if elementName == "StationDesc" {
passName = false
}
passData = false
}
}
func parser(_ parser: XMLParser, foundCharacters string: String) {
if passName {
strXMLData = strXMLData+"\n\n"+string
}
if passData {
//ready content for codable struct
switch currentElement {
case "StationDesc":
station = string
case "StationLatitude":
latitude = string
case "StationLongitude":
longitude = string
case "StationCode":
code = string
case "StationId":
id = string
print(string)
default:
id = string
}
}
}
func parser(_ parser: XMLParser, parseErrorOccurred parseError: Error) {
print("failure error: ", parseError)
}
}
The XMLParser just needs to be held while parse() is running, so you have no need to declare xmlParser as a property of XMLParserHelper.
You need a strong reference
class ViewController: UIViewController{
var parser:XMLParserHelper!
override func viewDidLoad() {
super.viewDidLoad()
parser = XMLParserHelper()
parser.runParser()
}
}

xml parsing in iOS swift

I am having bar or QR code scanning of Aadhar card.I am getting the response as the following xml format.How to convert this into dictionary format using xml parsing?
<?xml version="1.0" encoding="UTF-8"?><PrintLetterBarcodeData uid="685860050795" name="Sangeetha D" gender="F" yob="1989" co="W/O: Dhanansekaran" house="632" street="saradhambal nagar" lm="agaramel" vtc="Nazarathpettai" po="Nazarethpettai" dist="Tiruvallur" subdist="Poonamallee" state="Tamil Nadu" pc="600123" dob="03/06/1989"/>
I tried the following code for parsing
public func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
currentElement=elementName;
print(currentElement)
}
public func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
currentElement="";
}
public func parser(parser: NSXMLParser, foundCharacters string: String) {
}
But its returning always the currentElement as "PrintLetterBarcodeData"
Here's some parsing code I wrote in Swift 3 based off of a Google News RSS reader I previously wrote in Swift 2.0. I have this code modified to handle a list of PrintLetterBarcodeData elements as well as a single one:
class BarcodeData {
var uid: String
var name: String
var gender: String
var yob: String
var co: String
var house: String
var street: String
var lm: String
var vtc: String
var po: String
var dist: String
var subdist: String
var state: String
var pc: String
var dob: String
init?(dictionary: [String : String]) {
guard let uid = dictionary["uid"],
let name = dictionary["name"],
let gender = dictionary["gender"],
let yob = dictionary["yob"],
let co = dictionary["co"],
let house = dictionary["house"],
let street = dictionary["street"],
let lm = dictionary["lm"],
let vtc = dictionary["vtc"],
let po = dictionary["po"],
let dist = dictionary["dist"],
let subdist = dictionary["subdist"],
let state = dictionary["state"],
let pc = dictionary["pc"],
let dob = dictionary["dob"] else {
return nil
}
self.uid = uid
self.name = name
self.gender = gender
self.yob = yob
self.co = co
self.house = house
self.street = street
self.lm = lm
self.vtc = vtc
self.po = po
self.dist = dist
self.subdist = subdist
self.state = state
self.pc = pc
self.dob = dob
}
}
class MyParser: NSObject {
var parser: XMLParser
var barcodes = [BarcodeData]()
init(xml: String) {
parser = XMLParser(data: xml.data(using: String.Encoding.utf8)!)
super.init()
parser.delegate = self
}
func parseXML() -> [BarcodeData] {
parser.parse()
return barcodes
}
}
extension MyParser: XMLParserDelegate {
func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
if elementName == "PrintLetterBarcodeData" {
if let barcode = BarcodeData(dictionary: attributeDict) {
barcodes.append(barcode)
}
}
}
}
Usage:
let xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><PrintLetterBarcodeData uid=\"685860050795\" name=\"Sangeetha D\" gender=\"F\" yob=\"1989\" co=\"W/O: Dhanansekaran\" house=\"632\" street=\"saradhambal nagar\" lm=\"agaramel\" vtc=\"Nazarathpettai\" po=\"Nazarethpettai\" dist=\"Tiruvallur\" subdist=\"Poonamallee\" state=\"Tamil Nadu\" pc=\"600123\" dob=\"03/06/1989\"/>"
let parser = MyParser(xml: xmlString)
let barcodes = parser.parseXML() // array of barcodes
barcodes.first // your barcode
It appears as though your expected XML structure only consists of the root element PrintLetterBarcodeData and its attributes.
You will find the attributes of your root element in the attributeDict property in the didStartElement delegate method.
For example, to extract the name property, you would do:
public func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
currentElement=elementName;
print(currentElement)
//print name
if let name = attributeDict["name"] {
print(name) //prints Sangeetha D
}
}

UILabel not updating in Swift 2

I am having an issue in which I am parsing a String value from an API and I am trying to assign it to an UILabel. I have tested updating the label in the exact same position as it is now by setting the label (lblNamedata) to a String using the format:
self.lblNameData.text = "hello"
and it has worked fine.
I currently have:
if success {
print("parse success!")
print(strXMLData)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.lblNameData.text=self.strXMLData
})
} else {
print("parse failure!")
}
and this isn't working.
Below is my complete current code:
import UIKit
class ViewController: UIViewController,NSXMLParserDelegate {
var strXMLData:String = ""
var currentElement:String = ""
var passData:Bool=false
var passName:Bool=false
var parser = NSXMLParser()
#IBOutlet weak var lblNameData: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let url:String="http://www.stands4.com/services/v2/quotes.php?uid=idhere&tokenid=tokenhere&searchtype=xxxx&query=xxxx"
let urlToSend: NSURL = NSURL(string: url)!
// Parse the XML
parser = NSXMLParser(contentsOfURL: urlToSend)!
parser.delegate = self
let success:Bool = parser.parse()
if success {
print("parse success!")
print(strXMLData)
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.lblNameData.text=self.strXMLData
})
} else {
print("parse failure!")
}
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
currentElement=elementName;
if(elementName=="quote" || elementName=="author" || elementName=="result")
{
if(elementName=="quote"){
passName=true;
}
passData=true;
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
currentElement="";
if(elementName=="quote" || elementName=="author" || elementName=="result")
{
if(elementName=="quote"){
passName=false;
}
passData=false;
}
}
func parser(parser: NSXMLParser, foundCharacters string: String) {
if(passName){
strXMLData=strXMLData+"\n\n"+string
}
if(passData)
{
// print(string)
print("work")
}
}
func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) {
NSLog("failure error: %#", parseError)
}
}
UPDATE: So I believe I found the error: in my parser function I'm setting strXMLData = strXMLData + "\n\n" + string but after I delete the \n\n I can see that my label updates...why is this?
Alright, so after many instances of trial and error, I think I figured out the answer.
Something was up in my Main.storyboard that didn't like me creating multiple lines in the UILabel, so what I did was click on the label and navigated to the Attributes Inspector. I set the "Lines" parameter to 0 and I was able to see my text!

Parsing a REST XML API response

I am currently trying to parse a rest xml api response from a STANDS4 api...
the XML file would look something like this:
<results>
<result>
<quote>
To be free it is not enough to beat the system, one must beat the system every day.
</quote>
<author>Anonymous</author>
</result>
</results>
What I want to do is be able to print out the XML response and be able to assign the quote and author to a UILabel. What I currently have is the below code (I have my user ID and my token ID)
import UIKit
class ViewController: UIViewController,NSXMLParserDelegate {
var strXMLData:String = ""
var currentElement:String = ""
var passData:Bool=false
var passName:Bool=false
var parser = NSXMLParser()
#IBOutlet weak var lblNameData: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let url:String="http://www.stands4.com/services/v2/quotes.php?uid=youridhere&tokenid=youridherezf&searchtype=AUTHOR&query=Albert+Einstein"
let urlToSend: NSURL = NSURL(string: url)!
// Parse the XML
parser = NSXMLParser(contentsOfURL: urlToSend)!
parser.delegate = self
let success:Bool = parser.parse()
if success {
print("parse success!")
print(strXMLData)
lblNameData.text=strXMLData
} else {
print("parse failure!")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
currentElement=elementName;
if(elementName=="id" || elementName=="name" || elementName=="cost" || elementName=="description")
{
if(elementName=="name"){
passName=true;
}
passData=true;
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
currentElement="";
if(elementName=="id" || elementName=="name" || elementName=="cost" || elementName=="description")
{
if(elementName=="name"){
passName=false;
}
passData=false;
}
}
func parser(parser: NSXMLParser, foundCharacters string: String) {
if(passName){
strXMLData=strXMLData+"\n\n"+string
}
if(passData)
{
print(string)
}
}
func parser(parser: NSXMLParser, parseErrorOccurred parseError: NSError) {
NSLog("failure error: %#", parseError)
}
}
I currently get the response that the parse was a success but nothing is printed out. I got the above code from this tutorial:
http://ashishkakkad.com/2014/10/xml-parsing-in-swift-language-ios-9-nsxmlparser/
so there is most likely some changes I need to make to work with my current API, but I'm not sure what exactly to do.
Any help is appreciated!
Your first problem is that your XMLParsing functions never look for the proper element names (assuming your example xml is exactly what you are getting). Your XML parsing is looking for "id", "name", "cost", or "description". Your example XML provides "quote" and "author". You need to debug through the XML parsing functions and get a handle on what they are actually doing before using them. Then you should be good to go.

XML Parsing in Swift 2 (Xcode 7)

I am needing help parsing XML from a URL. I have read on this a lot and cannot figure out how to get this. All the examples I have found are a little ambiguous with the examples and how to grab the info from parsed XML data.
How would I parse this XML
<menu>
<menuitems>
<item price="$10">Pizza</item>
<item price="$5">Salad</item>
<item price="$3">Bread</item>
</menuitems>
</menu>
This is the tutorial I was going off of but I couldn't understand how this tutorial was accessing the data in the arrays and dictionary.
http://www.theappguruz.com/blog/xml-parsing-using-nsxmlparse-swift
In Xcode 7 (Swift 2), how would I parse and access this info. I know this is not how it would be accessed but I am looking for something like the code below.
print ("\(item[0].data)") // Pizza //
print ("\(item[0].price)") // $10 //
Here is the code from the tutorial but I couldn't figure out how to access all the data parsed from the URL. How would I edit this code to fit my structure?
import UIKit
import Foundation
class ViewController: UIViewController, NSXMLParserDelegate
{
var parser = NSXMLParser()
var posts = NSMutableArray()
var elements = NSMutableDictionary()
var element = NSString()
var title1 = NSMutableString()
var date = NSMutableString()
func beginParsing()
{
posts = []
parser = NSXMLParser(contentsOfURL:(NSURL(string:"http://images.apple.com/main/rss/hotnews/hotnews.rss"))!)!
parser.delegate = self
parser.parse()
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String])
{
element = elementName
if (elementName as NSString).isEqualToString("item")
{
elements = NSMutableDictionary()
elements = [:]
title1 = NSMutableString()
title1 = ""
date = NSMutableString()
date = ""
}
}
func parser(parser: NSXMLParser, foundCharacters string: String)
{
if element.isEqualToString("title") {
title1.appendString(string)
} else if element.isEqualToString("pubDate") {
date.appendString(string)
}
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?)
{
if (elementName as NSString).isEqualToString("item") {
if !title1.isEqual(nil) {
elements.setObject(title1, forKey: "title")
}
if !date.isEqual(nil) {
elements.setObject(date, forKey: "date")
}
posts.addObject(elements)
}
}
}
It looks like that parser code was borrowed from something else. If this really is your XML, think it's simpler than that. The only trick in your case is that price is not an element, but rather just an attributeDict of the item element. And I might create my own custom type, Item to capture the name and price of an item.
Anyway, you might end up with something like:
struct Item {
let name: String
let price: String
}
var itemName: String?
var itemPrice: String?
var items: [Item]!
func parserDidStartDocument(parser: NSXMLParser) {
items = []
}
func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
if elementName == "item" {
itemPrice = attributeDict["price"]
itemName = ""
}
}
func parser(parser: NSXMLParser, foundCharacters string: String) {
itemName?.appendContentsOf(string)
}
func parser(parser: NSXMLParser, didEndElement elementName: String, namespaceURI: String?, qualifiedName qName: String?) {
if elementName == "item" {
items.append(Item(name: itemName!, price: itemPrice!))
itemName = nil
itemPrice = nil
}
}
When I do that, and print items, I get:
[MyApp.ViewController.Item(name: "Pizza", price: "$10"),
MyApp.ViewController.Item(name: "Salad", price: "$5"),
MyApp.ViewController.Item(name: "Bread", price: "$3")]

Resources