简体   繁体   中英

parsing Inner tags of XML file

I need to parse XML file to get the values of tags present in the xml file. I have done it partially and got stuck up in the mid. my xml file is as follows, (sample xml file)

<?xml version="1.0" encoding="UTF-8"?>
<database>
<student name="abc">
<phone>6879987</phone>
<dept>eee</dept>
<college>act</college>
<semester>
<year>2</year>
<no.of.sub>7</no.of.sub>
</semester>
<hostel>
<year>3</year>
<block>d4</block>
</hostel>
</student>
<student name="ram">
<phone>65464</phone>
<dept>cse</dept>
<college>Mit</college>
<semester>
<year>4</year>
<no.of.sub>5</no.of.sub>
</semester>
<hostel>
<year>5</year
<block>y4</block>
</hostel>
</student> 
</database>

My implementation is as follows,

   public class MySaxParser extends DefaultHandler  {
   private String temp;
   private ArrayList<Account> accList = new ArrayList<Account>();
   private Account acct;


   public static void main(String[] args) throws IOException, SAXException,
                 ParserConfigurationException {


          SAXParserFactory spfac = SAXParserFactory.newInstance();


          SAXParser sp = spfac.newSAXParser();


          MySaxParser handler = new MySaxParser();


          sp.parse("test.xml", handler);

          handler.readList();

   }



   @Override
   public void characters(char[] buffer, int start, int length) {
          temp = new String(buffer, start, length);
   }



   @Override
   public void startElement(String uri, String localName,
                 String qName, Attributes attributes) throws SAXException {
          temp = "";
          if (qName.equalsIgnoreCase("Student")) {
                 acct = new Account();
                 //acct.setType(attributes.getValue("type"));

          }
   }

   @override
   public void endElement(String uri, String localName, String qName)
                 throws SAXException {

          if (qName.equalsIgnoreCase("Student")) {
                 // add it to the list
                 accList.add(acct);

          } else if (qName.equalsIgnoreCase("phone")) {
                 acct.setphone(temp);
          } else if (qName.equalsIgnoreCase("dept")) {
                 acct.setdept((temp));
          } else if (qName.equalsIgnoreCase("College")) {
                 acct.setcollege(temp);

          }
   }



   private void readList() {

          Iterator<Account> it = accList.iterator();
          while (it.hasNext()) {
                 System.out.println(it.next().toString());
          }
   }

 }

It is possible for me to parse the values of phone,dept college. But year tag is the subtag of both semester and hostel. I need to get both the year value. when i simply use,

    else if (qName.equalsIgnoreCase("year")) {
    acct.setyear(temp); 

only year values of hostel is getting printed skipping the semester. 1) how can i parse through these sub tags. Thanks in advance

Yes, that's because hostel tag comes after semester. So, you always end up having the most recent tag. If you want to print both, you use some boolean flags like isSemester and isHotel and set these as they encounter. Then when you encounter year, check these flags and see to which one the "current year" is related to.

You need a way to "remember" whether you have entered a hostel tag or not. Simplest way would be to do the following inside startElement :

 if (qName.equalsIgnoreCase("hostel")) {
     this.insideHostel = true;
 }

And in endElement :

 if (qName.equalsIgnoreCase("hostel")) {
     this.insideHostel = false;
 }

Now you can do:

 } else if (qName.equalsIgnoreCase("year") && insideHostel) {
     acct.setyear(temp); 
 }

sample xml file,

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <Issue>
    <Primary>
    <Section>      
             String a=null;
             if(a==null) hi;);
             else bye;;
              }
    </Section>
    </Primary>
    <Source>
    <Section>
              String a="sbi";
              if(a==null) hi;);
               else bye;;
    </Section>
    </Source>
    </Issue>

I need to get all the characters within the section tag. My implementation as follows,

     public void endElement(String uri, String localName, String qName)
             throws SAXException {

      if (qName.equalsIgnoreCase("Issue")) {

             accList.add(acct);

      }
      if (qName.equalsIgnoreCase("Primary")) {

            this.isPrimary=false;

      }
      if (qName.equalsIgnoreCase("Source")) {
             this.isSource=false;

      }

    else if(qName.equalsIgnoreCase("section")&&isPrimary)
           {
               acct.setPrimarySnippet(temp);
           }
          else if(qName.equalsIgnoreCase("Section")&&isSource)
           {
               acct.setSourceSnippet(temp);
           }

o/p is : else bye;; },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM