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
234 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

839 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