+ Start a Discussion
Max Alexander 4Max Alexander 4 

parse XML question

I ned to parse xml that has tags with same name, i have no issues getting any unique ones
<organisationName>Department of Innovation Industry Science and Research</organisationName>
<effectiveFrom>2007-12-03</effectiveFrom> </mainName>
<mainBusinessPhysicalAddress> <stateCode>ACT</stateCode> <postcode>2600</postcode> <effectiveFrom>2007-12-03</effectiveFrom> <effectiveT o>0001-01-01</effectiveT o>

i need to extract effectiveFrom date out of mainBusinessPhysicalAddress
here is what i use
if ('effectiveto' == reader.getLocalName()) {
               if(reader.getEventType() == XmlTag.START_ELEMENT) reader.next();
                    resultRecord.effectiveAdressDate = reader.getText();

however it just pick the first one how do i point it to the second part of XML?
you have to do some workaround. i would say just keep track of parent element in a variable and use that with the if condition.

String lastParent = '';

if (reader.getLocalName() == 'mainName' || reader.getLocalName()=='mainBusinessPhysicalAddress') {

  lastParent = reader.getLocalName();

use the last parent

if ('effectiveto' == reader.getLocalName() && lastParent == 'mainBusinessPhysicalAddress')