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>

现在请有没有人帮忙找出来,我怎样才能从 xml 数据中获取这些 url 使用NSXMLParser或任何其他 xml 解析器假设TBXML在 IOS 中?

编辑:您还可以给我libxml这个 xml 的解析器示例。

提前致谢。

4

3 回答 3

20

试试这个:

- (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);
    }
}
于 2013-03-02T10:33:43.563 回答
3

好的,你问了一个libxml例子。我在一个项目中使用了它,但是用TBXML而不是NSXMLParser因为这个导致了编码和数据检索的重要问题。

首先,您必须从网上下载文件TBXML.m并将TBXML.h它们导入您的项目。然后,您还必须在Link Binary with Librarieslibxml2.dylib中链接到您的项目。

完成此操作后,您必须执行此操作以检索数据(基于您的 XML 源):

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);
}

您可能必须更改此代码,但在这里您拥有所需的所有基本内容。

于 2013-03-02T10:19:14.743 回答
2

这是您可以使用 NSXMLParser 的方式:

在您的 .h 文件中声明:

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

//---xml parsing---

NSXMLParser         *xmlParserPortFolio;
BOOL                elementFoundPortFolio;
NSMutableURLRequest *req;

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

在您的 .m 文件中使用以下代码:

-(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;
}
于 2013-03-02T09:22:24.123 回答