【问题标题】:How to read XML and show the data on label?如何读取 XML 并在标签上显示数据?
【发布时间】:2012-11-21 09:40:47
【问题描述】:

我有一个 XML

<?xml version="1.0" encoding="UTF-8"?>
<drivers>

<driver>
<img><![CDATA[45djx96.jpg]]></img>
<name><![CDATA[Alonso]]></name>
<teamname><![CDATA[farari]]></teamname>
<ref><![CDATA[45djx96]]></ref>
</driver>

<driver>
<img><![CDATA[1236.jpg]]></img>
<name><![CDATA[Alonso2]]></name>
<teamname><![CDATA[farari2]]></teamname>
<ref><![CDATA[1236]]></ref>
</driver>

<driver>
<img><![CDATA[1245FGt.jpg]]></img>
<name><![CDATA[Alonso3]]></name>
<teamname><![CDATA[farari3]]></teamname>
<ref><![CDATA[1245FGt]]></ref>
</driver>

</drivers>

我想显示唯一的细节

在 UILable 上表示 1236 的姓名和团队名称。

我第一次使用网络服务。我成功在控制台上捕获 XML,但无法读取 XML 并在 UILable 上显示名称和团队名称的值。

现在 xml 显示在控制台上,但无法解析。

有什么想法吗?

提前致谢。

【问题讨论】:

    标签: iphone objective-c xcode ipad ios5


    【解决方案1】:

    使用TouchXML,您可以将这些数据解析为数组,也可以将单个记录解析为字符串,我只需发布一些关于它的代码

    CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:data options:0 error:nil] autorelease];
    NSArray *nodes = [doc nodesForXPath:@"//driver" error:nil];
    
    for (CXMLElement *node in nodes) {      
        // Create Object Of Bean Class. if required
    
        for(int counter = 0; counter < [node childCount]; counter++) {          
            //Save Data in The Bean Class and Add In the Array.
    
            if ([[[node childAtIndex:counter] name] isEqualToString:@"teamname"]) {                 
                NSString *string = [[node childAtIndex:counter] stringValue];
                                yourLable.Text = string;
                //                NSLog(@"\n\n Title %@",string);
    
            }
            else if ([[[node childAtIndex:counter] name] isEqualToString:@"ref"]) {                 
                NSString *string = [[node childAtIndex:counter] stringValue];
               //                NSLog(@"\n\n Ref %@",string);
            } 
    }
    

    }

    对于 TouchXML,另请参阅本教程和示例

    1. TouchXML

    2. iphone-sdk-tutorial-building-an-advanced-rss-reader-using-touchxml

    【讨论】:

    • 感谢您的回复,但它对我不起作用。它说未知的接收者“CXMLDocument”
    • @AreebaKhan 是的,伙计,首先下载 touchXML 库并放入您的项目并导入头文件,如 #import "CXMLDocument.h" 然后它的工作,有关更多详细信息,请参阅链接教程和试用演示伴侣.. :)
    【解决方案2】:

    对于解析,您可以使用需要以下类的 TBXML。 TBXML.h,TBXML.m & NSDataAdditions.h,NSDataAdditions.m.

    我给出了一个示例。您可以根据您的代码进行更改

     NSString *cStr = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body><SetPatientBPXMLResponse xmlns=\"http://webservice.cruxmed.com/\"><SetPatientBPXMLResult><response><state><![CDATA[45djx96.jpg]]></state></response></SetPatientBPXMLResult></SetPatientBPXMLResponse></soap:Body></soap:Envelope>"];
    
        TBXML *tbxmlObj = [[TBXML alloc] initWithXMLString:cStr];
    
        // Obtain root element
        TBXMLElement * root = tbxmlObj.rootXMLElement;
        //-------------------------------------------
        if (root)
        {
            //-------------------------------------------
            // search for the first NewDataSet element within the root element's children
            // instantiate an NewDataSet objectGetDataInXMLFromStoredProcedureResponse
            TBXMLElement * SoapBody = [TBXML childElementNamed:@"soap:Body" parentElement:root];
    
            TBXMLElement * SoapResponse = [TBXML childElementNamed:@"SetPatientBPXMLResponse" parentElement:SoapBody];
    
            TBXMLElement * SoapResult = [TBXML childElementNamed:@"SetPatientBPXMLResult" parentElement:SoapResponse];
    
            //TBXMLElement * NewDataSet1 = [TBXML childElementNamed:@"response" parentElement:SoapResult];
    
            TBXMLElement * NewDataSet = [TBXML childElementNamed:@"response" parentElement:SoapResult];
            // if an _wspGetChartTemplateByAgent element was found
            while (NewDataSet != nil) {
    
    
                //cAccountName,fOrdTotTaxDEx,fOrdTotTax,;
    
                // instantiate a Order object
    
    
                // find the iInvoiceId
                TBXMLElement * AutoIndex = [TBXML childElementNamed:@"state" parentElement:NewDataSet];
    
                if (AutoIndex != nil)
                {
                    NSString* iVal=[TBXML textForElement:AutoIndex] ;
                    NSLog(@"iVal:%@",iVal);
                }
                // find the next sibling element named "_wspGetChartTemplateByAgent"
                NewDataSet = [TBXML nextSiblingNamed:@"response" searchFromElement:NewDataSet];
            }
        }
    

    【讨论】:

      【解决方案3】:

      嗯,我认为 NSXMLParser 类应该敲响警钟。 但是,它并不是那么方便,您可以找到许多更好的解析器,例如 TBXML (one of branches)。 此外,通常建议使用 JSON 而不是 XML,因为它更轻且更易于解析。最常见的 JSON 解析器库是 SBJSON。

      【讨论】:

        猜你喜欢
        • 2013-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-18
        • 1970-01-01
        • 2017-06-28
        相关资源
        最近更新 更多