Solved

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..

Posted on 2004-08-12
4
243 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:phantom2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11786975
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());
  }
0
 
LVL 19

Accepted Solution

by:
drichards earned 500 total points
ID: 11788640
I don't think that will solve the problem.  You will always be entering the first case as you were before.  mCurrentFttpServedInfo will not be null because you hit that node first.  Then you will always enter the 'mCurrentFttpServedInfo != null' case.

You need to have a state variable that indicates whether you are processing the 'FTTPServedInfo' node or the 'FTTPCapableInfo' node. Assume you have a 'nodeState' variable that is 0 initially, you set it to 1 when processing the 'FTTPServedInfo' node and then to 2 when processing the 'FTTPCapableInfo' node (I assume you have handlers for those as well - if not you'll need to add them). Then do:

else if(lName.equalsIgnoreCase(FTTPFLAG))
  {
     switch (nodeState)
     {
     case 1: // don't need to check mCurrentFttpServedInfo != null because you did that before changing nodeState
          mCurrentFttpServedInfo.setFttpFlag(mCharBuf.toString());
          break;
     case 2:
          mCurrentFttpCapableInfo.setFttpFlag(mCharBuf.toString());
          break;
     }
  }
  else if(lName.equalsIgnoreCase(PASSCODE))
  {
     if (mCurrentAccount != null)
          mCurrentAccount.setPassCode(mCharBuf.toString());
  }


Or you could simply use a counter if your XML doc always has just two nodes in that order.  Before parsing document, set counter to 0.  Then:

else if(lName.equalsIgnoreCase(FTTPFLAG))
  {
     if ((counter == 0) && (mCurrentFttpServedInfo != null))
     {
          mCurrentFttpServedInfo.setFttpFlag(mCharBuf.toString());
          ++counter;
     }
     else if ((counter == 1) && (mCurrentFttpCapableInfo != null))
     {
          mCurrentFttpCapableInfo.setFttpFlag(mCharBuf.toString());
          ++counter;
     }
  }
  else if(lName.equalsIgnoreCase(PASSCODE))
  {
     if (mCurrentAccount != null)
          mCurrentAccount.setPassCode(mCharBuf.toString());
  }
0
 
LVL 3

Expert Comment

by:kumvjuec
ID: 11790878
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());
  }
0
 
LVL 19

Expert Comment

by:drichards
ID: 11793054
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.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question