【问题标题】:parsing Inner tags of XML file解析 XML 文件的内部标签
【发布时间】:2013-04-19 10:51:13
【问题描述】:

我需要解析 XML 文件以获取 xml 文件中存在的标签的值。我已经完成了部分工作,并且卡在了中间。我的xml文件如下,(示例xml文件)

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

我的实现如下,

   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());
          }
   }

 }

我可以解析 phone,dept college 的值。但是年标签是学期和宿舍的子标签。我需要同时获得年份值。当我简单地使用时,

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

只有宿舍的年份值会跳过学期打印出来。 1)我如何解析这些子标签。在此先感谢

【问题讨论】:

    标签: java xml sax saxparser


    【解决方案1】:

    是的,那是因为宿舍标签是在学期之后出现的。因此,您最终总是拥有最新的标签。如果你想打印两者,你可以使用一些布尔标志,比如 isSemester 和 isHotel 并在遇到时设置它们。然后当您遇到年份时,检查这些标志并查看“当前年份”与哪一个相关。

    【讨论】:

      【解决方案2】:

      您需要一种方法来“记住”您是否输入了hostel 标签。最简单的方法是在startElement 中执行以下操作:

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

      endElement:

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

      现在你可以这样做了:

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

      【讨论】:

        【解决方案3】:

        示例 xml 文件,

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

        我需要获取 section 标签中的所有字符。我的实现如下,

             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 是:否则再见;; },

        【讨论】:

        • 如何打印section标签中的所有字符??
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-11-19
        • 2015-04-28
        • 1970-01-01
        • 1970-01-01
        • 2013-07-07
        • 1970-01-01
        • 2015-09-17
        相关资源
        最近更新 更多