Link to home
Start Free TrialLog in
Avatar of phantom2005
phantom2005

asked on

SAX parser -- if there is two elements with same name in different place in the xml, getting the first one but next one is missing..

Hi,
I have a xml (part of it) as follows:

  <p:FTTPServedInfo ErrorCode="" ErrorDesc="">

                      <p:FTTPFlag>Y</p:FTTPFlag>

                        <!-- Possible Values are 'Y' , 'N' -->

                      <p:isPackageBundled/>

                        <!-- Possible Values are 'Y' , 'N' -->
  </p:FTTPServedInfo>

  <p:FTTPCapableInfo ErrorCode="" ErrorDesc="">
                         <p:FTTPFlag>N</p:FTTPFlag>
                        <!-- Possible Values are 'Y' , 'N' -->
                        <p:AccessType/>
                      <!-- Possible Values are 'G' , 'C' , 'O' -->
</p:FTTPCapableInfo>


Whe I am parsing (using SAX parser in java )it. I am gettting the first  <p:FTTPFlag>Y</p:FTTPFlag> but, the next one is coming as nothing..


The code snippet is as follows:

else if(lName.equalsIgnoreCase(FTTPFLAG)) { // mCurrentFttpServedInfo is an object
            if (mCurrentFttpServedInfo != null)
            {
                mCurrentFttpServedInfo.setFttpFlag(mCharBuf.toString());
            }
        }

 else if(lName.equalsIgnoreCase(PASSCODE)) {
            if (mCurrentAccount != null)
                    mCurrentAccount.setPassCode(mCharBuf.toString());
        }

else if(lName.equalsIgnoreCase(FTTPFLAG)) {  //// mCurrentFttpCapableInfo is a different object then mCurrentFttpServedInfo as specified earlier
           
 if (mCurrentFttpCapableInfo != null)
            {
                mCurrentFttpCapableInfo.setFttpFlag(mCharBuf.toString());
            }
        }


can u pls help..

Thanks

Avatar of kumvjuec
kumvjuec

Use this, your problem will be solved :)

else if(lName.equalsIgnoreCase(FTTPFLAG))
  {
      if (mCurrentFttpServedInfo != null)
      {
            mCurrentFttpServedInfo.setFttpFlag(mCharBuf.toString());
      }
      else if (mCurrentFttpCapableInfo != null)
      {
            mCurrentFttpCapableInfo.setFttpFlag(mCharBuf.toString());
      }
  }

  else if(lName.equalsIgnoreCase(PASSCODE))
  {
      if (mCurrentAccount != null)
            mCurrentAccount.setPassCode(mCharBuf.toString());
  }
ASKER CERTIFIED SOLUTION
Avatar of drichards
drichards

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Actually the prefix in the variable ("current") suggested to me that the variable must be null in case it is not current. However, if the serverinfo tag comes always before the capableinfo tag, then you can use this ...

else if(lName.equalsIgnoreCase(FTTPFLAG))
  {
     if (mCurrentFttpCapableInfo != null)
     {
          mCurrentFttpCapableInfo.setFttpFlag(mCharBuf.toString());
     }
     else if (mCurrentFttpServedInfo != null)
     {
          mCurrentFttpServedInfo.setFttpFlag(mCharBuf.toString());
     }
  }

  else if(lName.equalsIgnoreCase(PASSCODE))
  {
     if (mCurrentAccount != null)
          mCurrentAccount.setPassCode(mCharBuf.toString());
  }
Yes, that would work, but it leaves you dependent on the order of elements.  If you can guarantee that they are always in the same order, that's great, but it's preferable to code such that it will work no matter what order the elements appear.