7
<root>
    <table name="radios">
        <column name="nameradio">Radio1</column>
        <column name="logo">http://app.syndicationradio.fr/demo/logo1.png</column>
        <column name="stream">http://cloud2.syndicationradio.fr:8020</column>
        <column name="twitter">http://www.twitter.com/#syndicationradio</column>
        <column name="facebook">http://www.facebook.com/syndicationradio</column>
        <column name="titre">http://app.syndicationradio.fr/demo/title.xml</column>
    </table>
    <table name="radios">
        <column name="nameradio">Radio2</column>
        <column name="logo">http://app.syndicationradio.fr/demo/logo1.png</column>
        <column name="stream">http://cloud2.syndicationradio.fr:8020</column>
        <column name="twitter">http://www.twitter.com/#syndicationradio</column>
        <column name="facebook">http://www.facebook.com/syndicationradio</column>
        <column name="titre">http://app.syndicationradio.fr/demo/title.xml</column>
    </table>
</root>

Now please is there anybody help to find out that, how can i get those url from the xml data using NSXMLParser or any other xml parser suppose TBXML in IOS?

Edit: you can also give me example of libxml parser for this xml.

Thanks In Advance.

Mobile App Dev
  • 1,824
  • 3
  • 20
  • 45
Emon
  • 452
  • 3
  • 11
  • 21

3 Answers3

20

Try this:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSURL *url = [[NSURL alloc] initWithString:@"yourURL"];
    NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [parser setDelegate:self];
    BOOL result = [parser parse];
    // Do whatever with the result
}
               
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    NSLog(@"Did start element");
    if ([elementName isEqualToString:@"root"]) {
        NSLog(@"found rootElement");
        return;
    }
}
        
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
    NSLog(@"Did end element");
    if ([elementName isEqualToString:@"root"]) {
        NSLog(@"rootelement end");
    }           
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
    NSString *tagName = @"column";
    if ([tagName isEqualToString:@"column"]) {
        NSLog(@"Value %@",string);
    }
}
Arainty
  • 91
  • 9
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • I have a string, not a url: NSString *urlString = [NSString stringWithFormat:@"http://www.somewebsite.com/RunPHPtoOutputXML.php?id=%d", ii]; //ii is an integer I then run the following and am getting the XML: NSURLRequest * urlRequest = [NSURLRequest requestWithURL: [NSURL URLWithString:urlString]]; NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; – user3741598 Jul 14 '14 at 03:17
  • @user3741598 Exactly what you wanna ask ? – Dharmbir Singh Jul 14 '14 at 05:01
  • I got cut off - too many characters - will open a new question. Although I just came up with a potential quick, dirty possible answer at work that I will try when I get home. Thanks for asking. – user3741598 Jul 14 '14 at 14:03
3

Ok you asked for a libxml example. I used it in a project but with TBXML instead of NSXMLParser because this one caused important problems of encoding and data retrieving.

First you have to download TBXML.m and TBXML.h files from the web and import them into your project. Then you also have to link libxml2.dylib to your project in Link Binary with Libraries.

Once this done, you will have to do this to retrieve your data (based on your XML source) :

NSData *xmlData = [NSData dataWithContentsOfURL:yourURL];
TBXML *tbxml = [TBXML newTBXMLWithXMLData:data error:nil];
[self getData:tbxml.rootXMLElement];

- (void) getData : (TBXMLElement *) element
{
    do {
        if([[TBXML elementName:element] isEqualToString:@"table"])
        {
            if([[TBXML elementName:element] isEqualToString:@"column"])
            { 
                if([[TBXML attributeName:element] isEqualToString:@"nameradio"])
                {
                    // You decide what to do here
                }
            }
        }
        if (element->firstChild) [self getData:element->firstChild];
    } while(element = element->nextSibling);
}

You probably will have to change this code but here you have all the basic things you need.

Rob
  • 15,732
  • 22
  • 69
  • 107
  • thanks.that is so nice and clear code from you.could you give me the link of TBXML.h and TBXML.m. because i have downloaded one but its showing me some error. – Emon Mar 02 '13 at 10:36
  • With pleasure ;) [Here](https://github.com/71squared/TBXML/blob/master/TBXML-Headers/TBXML.h) you will find the header file, and [here](https://github.com/71squared/TBXML/blob/master/TBXML-Code/TBXML.m) the code file. This is version 1.5, the one I used and worked fine for me. – Rob Mar 02 '13 at 10:42
  • sorry to say but here is another problem is ` if([[TBXML attributeName:element] isEqualToString:@"nameradio"])` showing some waring something like that `incompatible pointer type TBXMLElement* ` – Emon Mar 02 '13 at 11:52
  • thank you very much for your help but i think my desire answer is my accepted answer. thanks again. – Emon Mar 02 '13 at 13:14
2

This is how you can use NSXMLParser :

In your .h file declare :

NSMutableData       *webPortFolio;
NSMutableString     *soapResultsPortFolio;
NSURLConnection     *conn;

//---xml parsing---

NSXMLParser         *xmlParserPortFolio;
BOOL                elementFoundPortFolio;
NSMutableURLRequest *req;

NSString            *theXMLPortFolio;
NSString            *strSoapMsg;
UIAlertView         *alertView;

In your .m file use the following code:

-(void)callURL
{

     //Your logic to call URL.

     conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
     if (conn)
     {
         webPortFolio = [[NSMutableData data] retain];
     }
}
And to handle the response you can use following functions :

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webPortFolio setLength:0];     
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webPortFolio appendData:data];
}

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error
{

    NSLog(@"error...................%@",[error description]);
    [webPortFolio release];
    [connection release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{

    //Check the request and returns the response.

    NSLog(@"DONE. Received Bytes: %d", [webPortFolio length]);

    theXMLPortFolio = [[NSString alloc] 
                      initWithBytes: [webPortFolio mutableBytes] 
                      length:[webPortFolio length] 
                      encoding:NSUTF8StringEncoding];

    //---shows the XML---

    NSLog(@"shows the XML %@",theXMLPortFolio);
    [theXMLPortFolio release];    

    if(xmlParserPortFolio)
    {
        [xmlParserPortFolio release];
    }
    xmlParserPortFolio = [[NSXMLParser alloc] initWithData: webPortFolio];
    [xmlParserPortFolio setDelegate: self];
    [xmlParserPortFolio setShouldResolveExternalEntities:YES];
    [xmlParserPortFolio parse];
    [webPortFolio release];
    [connection release];
}

//---when the start of an element is found---
-(void)  parser:(NSXMLParser *) parser 
didStartElement:(NSString *) elementName 
   namespaceURI:(NSString *) namespaceURI 
  qualifiedName:(NSString *) qName
     attributes:(NSDictionary *) attributeDict
{

    if( [elementName isEqualToString:@"your_tag_name"])
    {
        if (!soapResultsPortFolio)
        {
            soapResultsPortFolio = [[NSMutableString alloc] init];
        }
        elementFoundPortFolio = TRUE;
        NSLog(@"Registration...%@",soapResultsPortFolio);
    }
    else if([elementName isEqualToString:@"your_tag_name"])
    {
        elementFoundPortFolio = TRUE;
    }
    else if([elementName isEqualToString:@"your_tag_name"])
    {
        elementFoundPortFolio = TRUE;
    }
    else if([elementName isEqualToString:@"your_tag_name"])
    {
        elementFoundPortFolio = TRUE;
    }

}

-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
    if (elementFoundPortFolio)
    {
        [soapResultsPortFolio appendString: string];
    }      
}

- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    NSLog(@"Parser error %@ ",[parseError description]);
}


//---when the end of element is found---
-(void)parser:(NSXMLParser *)parser 
didEndElement:(NSString *)elementName 
 namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"your_tag_name"])
    {          
        NSLog(@"display the soap results%@",soapResultsPortFolio);
    }
    else if([elementName isEqualToString:@"your_tag_name"])
    {          
        //Perform required action
    }
    else if([elementName isEqualToString:@"your_tag_name"])
    {
        //Perform required action
    }
    else if([elementName isEqualToString:@"your_tag_name"])
    {
        //Perform required action
    }

    [soapResultsPortFolio setString:@""];
    elementFoundPortFolio = FALSE;
}
Rushi
  • 4,553
  • 4
  • 33
  • 46