0

I'm having trouble doing a simple things in iOS. I'm parsing an XML and I want to populate an object with the data I retrieve.

do {
    TBXMLAttribute * attribute = element->firstAttribute;

    while (attribute) {
        Position *position = [[Position alloc]init];
            if ([[TBXML elementName:element] isEqualToString:@"referencecolis"]) {
                [position.cab addObject:[TBXML textForElement:element]];
                NSLog(@"array cab : %@",position.cab);
            };
        };

        if ([[TBXML elementName:element] isEqualToString:@"idagencedepart"]) {
            position.idagencedepart = [TBXML textForElement:element];
            NSLog(@"idagencedepart : %@",[TBXML textForElement:element]);
        };

        [arrayPositions addObject:position];
        attribute = attribute->next;
    }

    if (element->firstChild)
        [self traverseElement:element->firstChild];
} while ((element = element->nextSibling));

The " [position.cab addObject:[TBXML textForElement:element]];" is supposed to add the several "referencecolis" of the xml into the array declared in my class "Position".

When I print the array containg all my position objects, it's empty.

What am I doing wrong ?

Here is the part of the xml that matters in my problem :

       <idagencefinale type="java.lang.Integer">4</idagencefinale>
    <idclientreglantport type="java.lang.Integer">38201</idclientreglantport>
    <idcomptefacturation type="java.lang.Integer">15596</idcomptefacturation>
    <idconditiontaxation type="java.lang.Integer">13109</idconditiontaxation>
    <idconditiontaxationgeneral type="java.lang.Integer">13109</idconditiontaxationgeneral>
    <iddestinataire type="java.lang.Integer">40245</iddestinataire>
    <idexpediteur type="java.lang.Integer">38201</idexpediteur>
    <idfeuillechauffeur type="java.lang.Integer"/>
    <idfluxedi type="java.lang.Integer">143821</idfluxedi>
    <idforfaitechelle type="java.lang.Integer"/>
    <idgroupage type="java.lang.Integer"/>
    <idlocalitedestinataire type="java.lang.Integer">196</idlocalitedestinataire>
    <idlocaliteexpediteur type="java.lang.Integer">250</idlocaliteexpediteur>
    <idpositionapproche type="java.lang.Integer"/>
    <idpositionlivraison type="java.lang.Integer"/>
    <idpositionprecedente type="java.lang.Integer"/>
    <idreleveassignecompte type="java.lang.Integer"/>
    <idreleveassignecompteReexpedition type="java.lang.Integer"/>
    <idreleveportducompte type="java.lang.Integer"/>
    <idreleveportducompteReexpedition type="java.lang.Integer"/>
    <idremise type="java.lang.Integer"/>
    <idremisegeneral type="java.lang.Integer"/>
    <idserviceechelle type="java.lang.Integer"/>
    <idtarif type="java.lang.Integer">48271</idtarif>
    <idtarifgeneral type="java.lang.Integer">48271</idtarifgeneral>
    <idtiers type="java.lang.Integer"/>
    <iduser type="java.lang.Integer">498</iduser>
    <libellePort type="java.lang.String">PORT PAYE</libellePort>
    <locked type="boolean">false</locked>
    <lockedByFacturation type="boolean">false</lockedByFacturation>
    <longueur type="java.math.BigDecimal"/>
    <motifannulation type="java.lang.String"/>
    <origine type="java.lang.String">edi</origine>
    <palette type="java.lang.Integer">0</palette>
    <paletteConsignee type="java.lang.Integer">0</paletteConsignee>
    <poids type="java.math.BigDecimal">181.0000</poids>
    <poidsPalette type="java.math.BigDecimal">0.0000</poidsPalette>
    <poidsTotal type="java.math.BigDecimal">181.0000</poidsTotal>
    <port type="java.lang.Integer">0</port>
    <portDuRegle type="boolean">false</portDuRegle>
    <positionAdresseList type="java.util.Collection">
      <Object hashcode="75b25" type="fezfsfs.position.PositionAdresse">
        <adresse1 type="java.lang.String">fezfezfz</adresse1>
        <adresse2 type="java.lang.String">Zfezfzfezf</adresse2>
        <adresse3 type="java.lang.String"/>
        <codepostal type="java.lang.String">01700</codepostal>
        <datecreation type="java.util.Date">2013-09-06 17:44:12.268</datecreation>
        <datemodification type="java.util.Date"/>
        <datesuppression type="java.util.Date"/>
        <deleted type="boolean">false</deleted>
        <id type="java.lang.Integer">482085</id>
        <idlocalite type="java.lang.Integer">250</idlocalite>
        <iduser type="java.lang.Integer">498</iduser>
        <pays type="java.lang.String">FRANCE</pays>
        <position hashcode="3aba5" type="ffezfezfezfze.position.Position"/>
        <raisonsociale type="java.lang.String">fezfezfezfez</raisonsociale>
        <typeadresse type="java.lang.Integer">0</typeadresse>
        <ville type="java.lang.String">fezfez</ville>
      </Object>
      <Object hashcode="75b26" type="fr.truckinfo.etreck.hibernate.core.position.PositionAdresse">
        <adresse1 type="java.lang.String">fezfezfzfze</adresse1>
        <adresse2 type="java.lang.String">fezfezfze</adresse2>
        <adresse3 type="java.lang.String"/>
        <codepostal type="java.lang.String">01480</codepostal>
        <datecreation type="java.util.Date">2013-09-06 17:44:12.268</datecreation>
        <datemodification type="java.util.Date"/>
        <datesuppression type="java.util.Date"/>
        <deleted type="boolean">false</deleted>
        <id type="java.lang.Integer">482086</id>
        <idlocalite type="java.lang.Integer">196</idlocalite>
        <iduser type="java.lang.Integer">498</iduser>
        <pays type="java.lang.String">FRANCE</pays>
        <position hashcode="3aba5" type="kofkeokfoezfpezf.position.Position"/>
        <raisonsociale type="java.lang.String">foofejozjfoejfoezjfez_</raisonsociale>
        <typeadresse type="java.lang.Integer">1</typeadresse>
        <ville type="java.lang.String">kfoekofkeoe</ville>
      </Object>
    </positionAdresseList>
    <positionAffectationChauffeursList type="java.util.Collection"/>
    <positionAffretementList type="java.util.Collection"/>
    <positionCRTList type="java.util.Collection"/>
    <positionColisList type="java.util.Collection">
      <Object hashcode="f6ed9" type="fr.truckinfo.etreck.hibernate.core.position.PositionColis">
        <complement type="java.lang.String">000000340863000642886657</complement>
        <dateaffectation type="java.util.Date">2013-09-06 13:36:33.052</dateaffectation>
        <datecreation type="java.util.Date"/>
        <datemodification type="java.util.Date"/>
        <datesuppression type="java.util.Date"/>
        <deleted type="boolean">false</deleted>
        <id type="java.lang.Integer">1011417</id>
        <idfluxedicolis type="java.lang.Integer">633374</idfluxedicolis>
        <iduser type="java.lang.Integer"/>
        <poids type="java.math.BigDecimal">0.000</poids>
        <position hashcode="3aba5" type="*****.position.Position"/>
        <referencecolis type="java.lang.String">000000340863000642886657</referencecolis>
        <typeaffectation type="java.lang.String"/>
      </Object>
    </positionColisList>
Caleb
  • 124,013
  • 19
  • 183
  • 272
Romain
  • 151
  • 1
  • 12
  • If your array is empty then it's a parsing problem. If your array it's nill it's probably because you didn't alloc/init it. – danypata Sep 16 '13 at 20:56
  • The array is allocated "NSMutableArray *arrayPositions = [[NSMutableArray alloc]init];", the Position object in the while loop. The NSLogs print well the data parsed. – Romain Sep 16 '13 at 21:10
  • Step through your code, and type a 'po position.cab' into the debugger now and then, and watch your array grow. You don't show the point where you're printing it out, but is it possible that you haven't used it afterwards, and ARC is throwing it away because you don't need it anymore? Make sure if it's a property, you're using @property (strong, nonatomic). – Owen Hartnett Sep 16 '13 at 21:34
  • The Position object is empty whereas the NSLog shows the xml is well parsed. The array (a property(strong, nonatomic) ) that contains all the Positions object grows but with the empty Position objects. – Romain Sep 17 '13 at 07:37
  • I've found the problem, "position.cab =" is stupid, "position setCab" is better ... – Romain Sep 17 '13 at 12:53

0 Answers0