Search code examples
iphonexmlios8qr-codebarcode-scanner

how to parse different type of xml data or similar to xml structure


I have scanned a image using my iphone and get the below type result.

<?xml version="1.0" encoding="UTF-8"?>
<PrintLetterBarcodeData uid="229636134720" name="Nandan Mohan Nilekani" gender="M" 
    yob="1955" house="856, 13th Main , 3rd Block" loc="Koramangala" vtc="Bangalore South" 
    po="Koramangala" dist="Bangalore" state="Karnataka" pc="560034"/>

How can best way to split or parse the above xml data.


Solution

  • Please check this code

        - (void)viewDidLoad{
    
    [super viewDidLoad];
    
    NSString *DataPath = @"<PrintLetterBarcodeData uid=\"229636134720\" name=\"Nandan Mohan Nilekani\" gender=\"M\" yob=\"1955\" house=\"856, 13th Main , 3rd Block\" loc=\"Koramangala\" vtc=\"Bangalore South\" po=\"Koramangala\" dist=\"Bangalore\" state=\"Karnataka\" pc=\"560034\"/>";
    NSData* data=[DataPath dataUsingEncoding:NSUTF8StringEncoding];;
    NSXMLParser* rssParser = [[NSXMLParser alloc] initWithData:data];
    
    [rssParser setDelegate:self];
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];
    [rssParser parse];}
    
    - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {
    
    NSDictionary* dic;
    
    currentElementValue = [[NSMutableString alloc] init];
    if ([elementName isEqualToString:@"PrintLetterBarcodeData"])
    {
        dic=attributeDict;
    }
    NSLog(@"%@",dic);}
    

    Output: {

    dist = Bangalore;

    gender = M;
    
    house = "856, 13th Main , 3rd Block";
    
    loc = Koramangala;
    
    name = "Nandan Mohan Nilekani";
    
    pc = 560034;
    
    po = Koramangala;
    
    state = Karnataka;
    
    uid = 229636134720;
    
    vtc = "Bangalore South";
    
    yob = 1955;
    

    }