0

目前我在使用 TBXML 解析 XML 服务时遇到问题。

目前....我正在这样做:

TBXML *configTBXML = [[TBXML alloc] initWithXMLData:[request responseData]];
TBXMLElement *root = configTBXML.rootXMLElement;
TBXMLElement *cddDataElement = [TBXML childElementNamed:@"data" parentElement:root];

NSMutableArray *anArray = [[NSMutableArray alloc] init];


if ([TBXML valueOfAttributeNamed:@"timezone" forElement:cddDataElement]) {

    NSString *key = [TBXML valueOfAttributeNamed:@"timezone" forElement:cddDataElement];
    [anArray addObject:key];
}

所以我注意到.. 在 XML 文件中,真的没有根...

<data timezone="US/Pacific-New-GMT-8" dst="1" ts="11-07-13 19:08:39" time="11-07-13 19:08" sun="DAY" weblanguage="ENGLISH" datalivequeue_start="0" datalivequeue_stop="0" datalivequeue_cnt="0" alarmsqueue_first="226" alarmsqueue_last="51" dhcpstatus="6" gateway="OK" upgrade_portal="OK" data_portal="ERROR" msgdatalive="564" upgrequests="343">
<cdd macrf="00:12:4B:00:02:8A:E0:14" sn="764040" countryselected="2" countryname="USA 208V-60Hz" idmodel="764040-3M60-1513" pout_kW="0.0000" pout_W="0.00" etot_Wh=" 0.00" etot_kWh="0.000" co2_t=" 0.00" status="WARNING"><edd sn="957188" pn="-3L03-" type="edd" macrf="00:12:4B:00:02:6A:D6:27" wy="0413" x="1" y="1" fwmicro="0000" p1_ids="0000" fwdsp="0000" p1_idi="0000" fwrf="000" Etot_Wh=" 0.00" Etot_kWh="0.000" Vout_V="0.00" Iout_A="0.000" Pout_W="0.00" Pout_kW="0.0000" Freq_Hz="0.00" Vin_V="0.00" Tdsp_degC="0.00" Tmos_degC="0.00" Pin_W="n.a" Pin_kW="n.a" Iin_A="n.a" model="n.a" eddmid="n.a" cddid="n.a" modeldesc="MICRO-0.3" invstat="START-UP" booststat="START-UP" alarmstat="0" rssi="0" rssizero="0" rssicnt="0" rssiavg="0" msgcnt="0" samples="15" ts="01-01-00 00:00:00" dspalarm="0x0000" mcualarm="0x0000" mcuwarning="0x0000" CS_crc="0x0000"/><edd sn="xxxxxx" pn="xxxxxx" type="edd" macrf="00:12:4B:00:02:6F:01:2C" wy="xxxx" x="1" y="1" fwmicro="0000" p1_ids="0000" fwdsp="0000" p1_idi="0000" fwrf="000" Etot_Wh=" 0.00" Etot_kWh="0.000" Vout_V="0.00" Iout_A="0.000" Pout_W="0.00" Pout_kW="0.0000" Freq_Hz="0.00" Vin_V="0.00" Tdsp_degC="0.00" Tmos_degC="0.00" Pin_W="n.a" Pin_kW="n.a" Iin_A="n.a" model="n.a" eddmid="n.a" cddid="n.a" modeldesc="MICRO-0.25" invstat="START-UP" booststat="START-UP" alarmstat="0" rssi="0" rssizero="0" rssicnt="0" rssiavg="0" msgcnt="0" samples="15" ts="01-01-00 00:00:00" dspalarm="0x0000" mcualarm="0x0000" mcuwarning="0x0000" CS_crc="0x0000"/><edd sn="xxxxxx" pn="xxxxxx" type="edd" macrf="00:12:4B:00:02:6F:14:5A" wy="xxxx" x="1" y="1" fwmicro="0000" p1_ids="0000" fwdsp="0000" p1_idi="0000" fwrf="000" Etot_Wh=" 0.00" Etot_kWh="0.000" Vout_V="0.00" Iout_A="0.000" Pout_W="0.00" Pout_kW="0.0000" Freq_Hz="0.00" Vin_V="0.00" Tdsp_degC="0.00" Tmos_degC="0.00" Pin_W="n.a" Pin_kW="n.a" Iin_A="n.a" model="n.a" eddmid="n.a" cddid="n.a" modeldesc="MICRO-0.25" invstat="START-UP" booststat="START-UP" alarmstat="0" rssi="0" rssizero="0" rssicnt="0" rssiavg="0" msgcnt="0" samples="15" ts="01-01-00 00:00:00" dspalarm="0x0000" mcualarm="0x0000" mcuwarning="0x0000" CS_crc="0x0000"/></cdd></data>

对格式可怕的 xml 感到抱歉,但如果您注意到......文件的标题是......好吧......对我来说是必不可少的提取......

现在,如果我使用:

TBXMLElement *cddDataElement = [TBXML childElementNamed:@"data" parentElement:root];

我崩溃了......我明白为什么因为我正在访问 PARENT 元素,而子元素不存在......

所以我的问题是,如何访问根元素的值/键?

4

1 回答 1

1

问题已解决......我在 TBXML 文档中找到了这个函数,它实际上提取了每个元素属性和属性值,非常简单......

使用一点 C 代码...

- (void) traverseElement:(TBXMLElement *)element {
    do {
        // Display the name of the element
        NSLog(@"%@",[TBXML elementName:element]);

        // Obtain first attribute from element
        TBXMLAttribute * attribute = element->firstAttribute;

        // if attribute is valid
        while (attribute) {
            // Display name and value of attribute to the log window
            NSLog(@"%@->%@ = %@",  [TBXML elementName:element],
                  [TBXML attributeName:attribute],
                  [TBXML attributeValue:attribute]);

            // Obtain the next attribute
            attribute = attribute->next;

        }

        // if the element has child elements, process them
        if (element->firstChild)
            [self traverseElement:element->firstChild];

        // Obtain next sibling element
    } while ((element = element->nextSibling));
}

如您所见,实际上发生了很多......递归、c 指针等。我像这样调用这个方法:

    TBXML *configTBXML = [[TBXML alloc] initWithXMLData:[request responseData]];
    TBXMLElement *root = configTBXML.rootXMLElement;
    if (root) {
        [self traverseElement:root];
    }

这给出了以下输出:

2013-07-12 09:30:29.836 PowerOneApp[77609:3c07] data
2013-07-12 09:30:29.837 PowerOneApp[77609:3c07] data->timezone = US/Pacific-New-GMT-8
2013-07-12 09:30:29.837 PowerOneApp[77609:3c07] data->dst = 1
2013-07-12 09:30:29.838 PowerOneApp[77609:3c07] data->ts = 12-07-13 09:30:32
2013-07-12 09:30:29.838 PowerOneApp[77609:3c07] data->time = 12-07-13 09:30
2013-07-12 09:30:29.838 PowerOneApp[77609:3c07] data->sun = DAY
2013-07-12 09:30:29.838 PowerOneApp[77609:3c07] data->weblanguage = ENGLISH
2013-07-12 09:30:29.838 PowerOneApp[77609:3c07] data->datalivequeue_start = 0
2013-07-12 09:30:29.838 PowerOneApp[77609:3c07] data->datalivequeue_stop = 0
2013-07-12 09:30:29.839 PowerOneApp[77609:3c07] data->datalivequeue_cnt = 0
2013-07-12 09:30:29.839 PowerOneApp[77609:3c07] data->alarmsqueue_first = 229
2013-07-12 09:30:29.839 PowerOneApp[77609:3c07] data->alarmsqueue_last = 51
2013-07-12 09:30:29.839 PowerOneApp[77609:3c07] data->dhcpstatus = 6
2013-07-12 09:30:29.839 PowerOneApp[77609:3c07] data->gateway = OK
2013-07-12 09:30:29.839 PowerOneApp[77609:3c07] data->upgrade_portal = OK
2013-07-12 09:30:29.840 PowerOneApp[77609:3c07] data->data_portal = ERROR
2013-07-12 09:30:29.840 PowerOneApp[77609:3c07] data->msgdatalive = 564
2013-07-12 09:30:29.841 PowerOneApp[77609:3c07] data->upgrequests = 357
2013-07-12 09:30:29.841 PowerOneApp[77609:3c07] cdd
2013-07-12 09:30:29.841 PowerOneApp[77609:3c07] cdd->macrf = 00:12:4B:00:02:8A:E0:14
2013-07-12 09:30:29.841 PowerOneApp[77609:3c07] cdd->sn = 764040
2013-07-12 09:30:29.841 PowerOneApp[77609:3c07] cdd->countryselected = 2
2013-07-12 09:30:29.842 PowerOneApp[77609:3c07] cdd->countryname = USA 208V-60Hz
2013-07-12 09:30:29.842 PowerOneApp[77609:3c07] cdd->idmodel = 764040-3M60-1513
2013-07-12 09:30:29.842 PowerOneApp[77609:3c07] cdd->pout_kW = 0.0000
2013-07-12 09:30:29.842 PowerOneApp[77609:3c07] cdd->pout_W = 0.00
2013-07-12 09:30:29.843 PowerOneApp[77609:3c07] cdd->etot_Wh =  0.00
2013-07-12 09:30:29.843 PowerOneApp[77609:3c07] cdd->etot_kWh = 0.000
2013-07-12 09:30:29.843 PowerOneApp[77609:3c07] cdd->co2_t =  0.00
2013-07-12 09:30:29.843 PowerOneApp[77609:3c07] cdd->status = ALARM
2013-07-12 09:30:29.843 PowerOneApp[77609:3c07] edd
2013-07-12 09:30:29.844 PowerOneApp[77609:3c07] edd->sn = 957188
2013-07-12 09:30:29.844 PowerOneApp[77609:3c07] edd->pn = -3L03-
2013-07-12 09:30:29.844 PowerOneApp[77609:3c07] edd->type = edd
2013-07-12 09:30:29.844 PowerOneApp[77609:3c07] edd->macrf = 00:12:4B:00:02:6A:D6:27
2013-07-12 09:30:29.845 PowerOneApp[77609:3c07] edd->wy = 0413
2013-07-12 09:30:29.845 PowerOneApp[77609:3c07] edd->x = 1
2013-07-12 09:30:29.845 PowerOneApp[77609:3c07] edd->y = 1
2013-07-12 09:30:29.845 PowerOneApp[77609:3c07] edd->fwmicro = 0000
2013-07-12 09:30:29.846 PowerOneApp[77609:3c07] edd->p1_ids = 0000
2013-07-12 09:30:29.846 PowerOneApp[77609:3c07] edd->fwdsp = 0000
2013-07-12 09:30:29.846 PowerOneApp[77609:3c07] edd->p1_idi = 0000
2013-07-12 09:30:29.846 PowerOneApp[77609:3c07] edd->fwrf = 000
2013-07-12 09:30:29.846 PowerOneApp[77609:3c07] edd->Etot_Wh =  0.00
2013-07-12 09:30:29.847 PowerOneApp[77609:3c07] edd->Etot_kWh = 0.000
2013-07-12 09:30:29.847 PowerOneApp[77609:3c07] edd->Vout_V = 0.00
2013-07-12 09:30:29.847 PowerOneApp[77609:3c07] edd->Iout_A = 0.000
2013-07-12 09:30:29.847 PowerOneApp[77609:3c07] edd->Pout_W = 0.00
2013-07-12 09:30:29.848 PowerOneApp[77609:3c07] edd->Pout_kW = 0.0000
2013-07-12 09:30:29.848 PowerOneApp[77609:3c07] edd->Freq_Hz = 0.00
2013-07-12 09:30:29.848 PowerOneApp[77609:3c07] edd->Vin_V = 0.00
2013-07-12 09:30:29.848 PowerOneApp[77609:3c07] edd->Tdsp_degC = 0.00
2013-07-12 09:30:29.849 PowerOneApp[77609:3c07] edd->Tmos_degC = 0.00
2013-07-12 09:30:29.849 PowerOneApp[77609:3c07] edd->Pin_W = n.a
2013-07-12 09:30:29.849 PowerOneApp[77609:3c07] edd->Pin_kW = n.a
2013-07-12 09:30:29.849 PowerOneApp[77609:3c07] edd->Iin_A = n.a
2013-07-12 09:30:29.849 PowerOneApp[77609:3c07] edd->model = n.a
2013-07-12 09:30:29.850 PowerOneApp[77609:3c07] edd->eddmid = n.a
2013-07-12 09:30:29.850 PowerOneApp[77609:3c07] edd->cddid = n.a
2013-07-12 09:30:29.850 PowerOneApp[77609:3c07] edd->modeldesc = MICRO-0.3
2013-07-12 09:30:29.850 PowerOneApp[77609:3c07] edd->invstat = START-UP
2013-07-12 09:30:29.851 PowerOneApp[77609:3c07] edd->booststat = START-UP
2013-07-12 09:30:29.851 PowerOneApp[77609:3c07] edd->alarmstat = 1
2013-07-12 09:30:29.851 PowerOneApp[77609:3c07] edd->rssi = 0
2013-07-12 09:30:29.851 PowerOneApp[77609:3c07] edd->rssizero = 0
2013-07-12 09:30:29.851 PowerOneApp[77609:3c07] edd->rssicnt = 0
2013-07-12 09:30:29.852 PowerOneApp[77609:3c07] edd->rssiavg = 0
2013-07-12 09:30:29.852 PowerOneApp[77609:3c07] edd->msgcnt = 0
2013-07-12 09:30:29.852 PowerOneApp[77609:3c07] edd->samples = 15
2013-07-12 09:30:29.852 PowerOneApp[77609:3c07] edd->ts = 01-01-00 00:00:00
2013-07-12 09:30:29.853 PowerOneApp[77609:3c07] edd->dspalarm = 0x0000
2013-07-12 09:30:29.853 PowerOneApp[77609:3c07] edd->mcualarm = 0x8000
2013-07-12 09:30:29.853 PowerOneApp[77609:3c07] edd->mcuwarning = 0x0000
2013-07-12 09:30:29.853 PowerOneApp[77609:3c07] edd->CS_crc = 0x0000
2013-07-12 09:30:29.853 PowerOneApp[77609:3c07] edd
2013-07-12 09:30:29.854 PowerOneApp[77609:3c07] edd->sn = xxxxxx
2013-07-12 09:30:29.854 PowerOneApp[77609:3c07] edd->pn = xxxxxx
2013-07-12 09:30:29.854 PowerOneApp[77609:3c07] edd->type = edd
2013-07-12 09:30:29.854 PowerOneApp[77609:3c07] edd->macrf = 00:12:4B:00:02:6F:01:2C
2013-07-12 09:30:29.855 PowerOneApp[77609:3c07] edd->wy = xxxx
2013-07-12 09:30:29.855 PowerOneApp[77609:3c07] edd->x = 1
2013-07-12 09:30:29.855 PowerOneApp[77609:3c07] edd->y = 1
2013-07-12 09:30:29.855 PowerOneApp[77609:3c07] edd->fwmicro = 0000
2013-07-12 09:30:29.856 PowerOneApp[77609:3c07] edd->p1_ids = 0000
2013-07-12 09:30:29.856 PowerOneApp[77609:3c07] edd->fwdsp = 0000
2013-07-12 09:30:29.856 PowerOneApp[77609:3c07] edd->p1_idi = 0000
2013-07-12 09:30:29.856 PowerOneApp[77609:3c07] edd->fwrf = 000
2013-07-12 09:30:29.857 PowerOneApp[77609:3c07] edd->Etot_Wh =  0.00
2013-07-12 09:30:29.857 PowerOneApp[77609:3c07] edd->Etot_kWh = 0.000
2013-07-12 09:30:29.857 PowerOneApp[77609:3c07] edd->Vout_V = 0.00
2013-07-12 09:30:29.857 PowerOneApp[77609:3c07] edd->Iout_A = 0.000
2013-07-12 09:30:29.857 PowerOneApp[77609:3c07] edd->Pout_W = 0.00
2013-07-12 09:30:29.858 PowerOneApp[77609:3c07] edd->Pout_kW = 0.0000
2013-07-12 09:30:29.858 PowerOneApp[77609:3c07] edd->Freq_Hz = 0.00
2013-07-12 09:30:29.858 PowerOneApp[77609:3c07] edd->Vin_V = 0.00
2013-07-12 09:30:29.858 PowerOneApp[77609:3c07] edd->Tdsp_degC = 0.00
2013-07-12 09:30:29.859 PowerOneApp[77609:3c07] edd->Tmos_degC = 0.00
2013-07-12 09:30:29.859 PowerOneApp[77609:3c07] edd->Pin_W = n.a
2013-07-12 09:30:29.859 PowerOneApp[77609:3c07] edd->Pin_kW = n.a
2013-07-12 09:30:29.859 PowerOneApp[77609:3c07] edd->Iin_A = n.a
2013-07-12 09:30:29.859 PowerOneApp[77609:3c07] edd->model = n.a
2013-07-12 09:30:29.860 PowerOneApp[77609:3c07] edd->eddmid = n.a
2013-07-12 09:30:29.860 PowerOneApp[77609:3c07] edd->cddid = n.a
2013-07-12 09:30:29.860 PowerOneApp[77609:3c07] edd->modeldesc = MICRO-0.25
2013-07-12 09:30:29.860 PowerOneApp[77609:3c07] edd->invstat = START-UP
2013-07-12 09:30:29.861 PowerOneApp[77609:3c07] edd->booststat = START-UP
2013-07-12 09:30:29.861 PowerOneApp[77609:3c07] edd->alarmstat = 1
2013-07-12 09:30:29.861 PowerOneApp[77609:3c07] edd->rssi = 0
2013-07-12 09:30:29.861 PowerOneApp[77609:3c07] edd->rssizero = 0
2013-07-12 09:30:29.862 PowerOneApp[77609:3c07] edd->rssicnt = 0
2013-07-12 09:30:29.862 PowerOneApp[77609:3c07] edd->rssiavg = 0
2013-07-12 09:30:29.862 PowerOneApp[77609:3c07] edd->msgcnt = 0
2013-07-12 09:30:29.862 PowerOneApp[77609:3c07] edd->samples = 15
2013-07-12 09:30:29.862 PowerOneApp[77609:3c07] edd->ts = 01-01-00 00:00:00
2013-07-12 09:30:29.863 PowerOneApp[77609:3c07] edd->dspalarm = 0x0000
2013-07-12 09:30:29.863 PowerOneApp[77609:3c07] edd->mcualarm = 0x8000
2013-07-12 09:30:29.863 PowerOneApp[77609:3c07] edd->mcuwarning = 0x0000
2013-07-12 09:30:29.863 PowerOneApp[77609:3c07] edd->CS_crc = 0x0000
2013-07-12 09:30:29.864 PowerOneApp[77609:3c07] edd
2013-07-12 09:30:29.864 PowerOneApp[77609:3c07] edd->sn = xxxxxx
2013-07-12 09:30:29.864 PowerOneApp[77609:3c07] edd->pn = xxxxxx
2013-07-12 09:30:29.864 PowerOneApp[77609:3c07] edd->type = edd
2013-07-12 09:30:29.865 PowerOneApp[77609:3c07] edd->macrf = 00:12:4B:00:02:6F:14:5A
2013-07-12 09:30:29.865 PowerOneApp[77609:3c07] edd->wy = xxxx
2013-07-12 09:30:29.865 PowerOneApp[77609:3c07] edd->x = 1
2013-07-12 09:30:29.865 PowerOneApp[77609:3c07] edd->y = 1
2013-07-12 09:30:29.865 PowerOneApp[77609:3c07] edd->fwmicro = 0000
2013-07-12 09:30:29.866 PowerOneApp[77609:3c07] edd->p1_ids = 0000
2013-07-12 09:30:29.866 PowerOneApp[77609:3c07] edd->fwdsp = 0000
2013-07-12 09:30:29.866 PowerOneApp[77609:3c07] edd->p1_idi = 0000
2013-07-12 09:30:29.866 PowerOneApp[77609:3c07] edd->fwrf = 000
2013-07-12 09:30:29.867 PowerOneApp[77609:3c07] edd->Etot_Wh =  0.00
2013-07-12 09:30:29.867 PowerOneApp[77609:3c07] edd->Etot_kWh = 0.000
2013-07-12 09:30:29.867 PowerOneApp[77609:3c07] edd->Vout_V = 0.00
2013-07-12 09:30:29.867 PowerOneApp[77609:3c07] edd->Iout_A = 0.000
2013-07-12 09:30:29.868 PowerOneApp[77609:3c07] edd->Pout_W = 0.00
2013-07-12 09:30:29.868 PowerOneApp[77609:3c07] edd->Pout_kW = 0.0000
2013-07-12 09:30:29.868 PowerOneApp[77609:3c07] edd->Freq_Hz = 0.00
2013-07-12 09:30:29.868 PowerOneApp[77609:3c07] edd->Vin_V = 0.00
2013-07-12 09:30:29.869 PowerOneApp[77609:3c07] edd->Tdsp_degC = 0.00
2013-07-12 09:30:29.869 PowerOneApp[77609:3c07] edd->Tmos_degC = 0.00
2013-07-12 09:30:29.869 PowerOneApp[77609:3c07] edd->Pin_W = n.a
2013-07-12 09:30:29.869 PowerOneApp[77609:3c07] edd->Pin_kW = n.a
2013-07-12 09:30:29.870 PowerOneApp[77609:3c07] edd->Iin_A = n.a
2013-07-12 09:30:29.870 PowerOneApp[77609:3c07] edd->model = n.a
2013-07-12 09:30:29.870 PowerOneApp[77609:3c07] edd->eddmid = n.a
2013-07-12 09:30:29.870 PowerOneApp[77609:3c07] edd->cddid = n.a
2013-07-12 09:30:29.870 PowerOneApp[77609:3c07] edd->modeldesc = MICRO-0.25
2013-07-12 09:30:29.871 PowerOneApp[77609:3c07] edd->invstat = START-UP
2013-07-12 09:30:29.871 PowerOneApp[77609:3c07] edd->booststat = START-UP
2013-07-12 09:30:29.871 PowerOneApp[77609:3c07] edd->alarmstat = 1
2013-07-12 09:30:29.871 PowerOneApp[77609:3c07] edd->rssi = 0
2013-07-12 09:30:29.872 PowerOneApp[77609:3c07] edd->rssizero = 0
2013-07-12 09:30:29.872 PowerOneApp[77609:3c07] edd->rssicnt = 0
2013-07-12 09:30:29.872 PowerOneApp[77609:3c07] edd->rssiavg = 0
2013-07-12 09:30:29.872 PowerOneApp[77609:3c07] edd->msgcnt = 0
2013-07-12 09:30:29.873 PowerOneApp[77609:3c07] edd->samples = 15
2013-07-12 09:30:29.873 PowerOneApp[77609:3c07] edd->ts = 01-01-00 00:00:00
2013-07-12 09:30:29.873 PowerOneApp[77609:3c07] edd->dspalarm = 0x0000
2013-07-12 09:30:29.873 PowerOneApp[77609:3c07] edd->mcualarm = 0x8000
2013-07-12 09:30:29.874 PowerOneApp[77609:3c07] edd->mcuwarning = 0x0000
2013-07-12 09:30:29.874 PowerOneApp[77609:3c07] edd->CS_crc = 0x0000
于 2013-07-12T16:33:30.013 回答