以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  sax新手,一个简单的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=54769)


--  作者:0messiah
--  发布时间:11/2/2007 12:02:00 AM

--  sax新手,一个简单的问题
package xmlsax;

import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
*
* @author Administrator
*/
public class XMLParser extends DefaultHandler{
    Stack tags;
    int count;
    
    private static String sex;
    
    private String name;
    
    private String brith;
    
    private String pro;
    
    private String grade;
    
    private String classname;
    
    private String ElementName;
    
    private String proat;
    
    /** Creates a new instance of XMLParser */
    public XMLParser() {
        count = 0;
    }
    public void characters(char[] ch,int start,int length) {
        String tagBodyText=new String(ch,start,length);  //获得标签体的文字串内容
/*
        下列的代码主要是识别是否是指定的名称的标签,如果是并且识别其标签体是否为空,最后获得标签体的文字串
*/
        
        if(this.ElementName.equals("姓名") && tagBodyText.equals("张三")) {
            this.name=tagBodyText;
        }
        if(this.ElementName.equals("出生日期")&& !tagBodyText.trim().equals("")) {
            this.brith=tagBodyText;
        }
        if(this.ElementName.equals("专业")&& !tagBodyText.trim().equals("")) {
            this.pro=tagBodyText;
        }
        if(this.ElementName.equals("年级") && !tagBodyText.trim().equals("")) {
            this.grade=tagBodyText;
        }
        if(this.ElementName.equals("班级") && !tagBodyText.trim().equals("")) {
            this.classname=tagBodyText;
        }
        
        
    }
    public void startDocument(){
        tags = new Stack();
        //count = 0;
    }
    public void endDocument(){
        System.out.println("总共有"+count+"个学生");
        System.out.println("姓名:"+name);
        System.out.println("性别:"+sex);
        System.out.println("出生日期:"+brith);
        System.out.println("专业:"+this.pro);
        System.out.println("年级:"+grade);
        System.out.println("班级:"+classname);
        System.out.println("方向:"+this.proat);
    }
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts){
        tags.push(qName);
        if (qName.equals("学生信息")){
            count++;
            this.sex = atts.getValue(0);}this.ElementName=qName;        this.proat = atts.getValue(0);
    }
    public void endElement(String namespaceURI,String localName,String qName) throws SAXException {
        tags.pop();     //从Stack中获得标签的名称字符串
    }
    public static void main(String[] args){
        String filename = "student.xml";
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser saxparser = null;
        try {
            saxparser = spf.newSAXParser();
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
        try {
            saxparser.parse(new File(filename),new XMLParser());
        } catch (SAXException ex) {
            ex.printStackTrace();
            System.exit(1);
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    }
}
student.xml
<?xml version="1.0" encoding="GB2312" standalone="yes" ?>
<软件学院学生信息>
 <学生信息 性别="男">
  <姓名>张三</姓名>
  <出生日期>1987/10/18</出生日期>
  <专业>软件工程</专业>
  <年级>3</年级>
<班级 方向="J2EE软件开发">软件1班</班级>
 </学生信息>
 <学生信息 性别="女">
  <姓名>李四</姓名>
  <出生日期>1988/2/18</出生日期>
  <专业>软件工程</专业>
  <年级>3</年级>
  <班级 方向="数字媒体方向">软件2班</班级>
 </学生信息>
</软件学院学生信息>

解析结果不对……


--  作者:fangel2000
--  发布时间:11/5/2007 2:47:00 PM

--  
额,这个问题等会回来看看,有挑战性
--  作者:fangel2000
--  发布时间:11/5/2007 3:07:00 PM

--  
不正确的原因主要是因为
        if(this.ElementName.equals("姓名") && tagBodyText.equals("张三")) {
            this.name=tagBodyText;
        }
再者你的输出只有一个,是因为你将输入写到了public void endDocument()函数中,在结束整个文档的时候才调用这个函数的。所以只会显示最后一个学生的信息
--  作者:fangel2000
--  发布时间:11/5/2007 3:08:00 PM

--  
修改后的代码如下所示:


import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
*
* @author Administrator
*/
public class XMLParser extends DefaultHandler{
    Stack tags;
    int count;
    
    private static String sex;
    
    private String name;
    
    private String brith;
    
    private String pro;
    
    private String grade;
    
    private String classname;
    
    private String ElementName;
    
    private String proat;
    
    /** Creates a new instance of XMLParser */
    public XMLParser() {
        count = 0;
    }
    public void characters(char[] ch,int start,int length) {
        String tagBodyText=new String(ch,start,length);  //获得标签体的文字串内容
/*
        下列的代码主要是识别是否是指定的名称的标签,如果是并且识别其标签体是否为空,最后获得标签体的文字串
*/
   
        if(this.ElementName.equals("姓名") && !tagBodyText.trim().equals("")) {
            this.name=tagBodyText;
        }
        if(this.ElementName.equals("出生日期")&& !tagBodyText.trim().equals("")) {
            this.brith=tagBodyText;
        }
        if(this.ElementName.equals("专业")&& !tagBodyText.trim().equals("")) {
            this.pro=tagBodyText;
        }
        if(this.ElementName.equals("年级") && !tagBodyText.trim().equals("")) {
            this.grade=tagBodyText;
        }
        if(this.ElementName.equals("班级") && !tagBodyText.trim().equals("")) {
            this.classname=tagBodyText;
        }
                
    }
    public void startDocument(){
        tags = new Stack();
        //count = 0;
    }
    public void endDocument(){
        System.out.println("总共有"+count+"个学生");
        
    }
    public void startElement(String namespaceURI, String localName, String qName, Attributes atts){
        tags.push(qName);
        if (qName.equals("学生信息")){
            count++;
            this.sex = atts.getValue(0);
         }
        this.ElementName=qName;        
        this.proat = atts.getValue(0);
    }
    public void endElement(String namespaceURI,String localName,String qName) throws SAXException {
        String s = tags.pop().toString();
     if(s.equals("姓名"))     //从Stack中获得标签的名称字符串
     {
         System.out.println("姓名:"+name);
         System.out.println("性别:"+sex);
     }
     else if(s.equals("出生日期"))
         System.out.println("出生日期:"+brith);
     else if(s.equals("专业"))
         System.out.println("专业:"+this.pro);
     else if(s.equals("年级"))
         System.out.println("年级:"+grade);
     else if(s.equals("班级"))
     {
         System.out.println("班级:"+classname);
         System.out.println("方向:"+this.proat);
         System.out.println("-------------------------------");
     }
    }
    public static void main(String[] args){
        String filename = "student.xml";
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser saxparser = null;
        try {
            saxparser = spf.newSAXParser();
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
        try {
            saxparser.parse(new File(filename),new XMLParser());
        } catch (SAXException ex) {
            ex.printStackTrace();
            System.exit(1);
        } catch (IOException ex) {
            ex.printStackTrace();
            System.exit(1);
        }
    }
}


--  作者:0messiah
--  发布时间:11/8/2007 12:05:00 AM

--  
谢谢楼上的了,sax确实比dom难以理解一些。
马上要学ajax了,不知道是不是更难一些。
--  作者:fangel2000
--  发布时间:11/8/2007 2:58:00 PM

--  
sax我感觉还好吧,不就是几个触发事件么,遇到元素开始和结束,都会触发startelemnet和endelement事件
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
78.125ms