如何用SAX方式解析下面的xml?最后将获得的xml数据放到数组中。<?xml version="1.0"?> 
<notexml> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"            targetNamespace="http://www.w3school.com.cn"            xmlns="http://www.w3school.com.cn"            elementFormDefault="qualified">     <xs:element name="notexml">         <xs:complexType>             <xs:sequence>                 <xs:element name="to" type="xs:string"/>                 <xs:element name="from" type="xs:string"/>                 <xs:element name="heading" type="xs:string"/>                 <xs:element name="body" type="xs:string"/>             </xs:sequence>         </xs:complexType>     </xs:element> </xs:schema>    <to>George</to>     <from>John</from>     <heading>Reminder</heading>     <body>Don't forget the meeting this weekend!</body> </notexml>

解决方案 »

  1.   

    具体实现代码如下:
    /**
     * 
     */
    package com.demo.xml.sax;/**
     * @author Administrator
     *
     */
    public class NoteXml
    {
        private String to;
        private String from;
        private String heading;
        private String body;
        public String getTo()
        {
            return to;
        }
        public void setTo(String to)
        {
            this.to = to;
        }
        public String getFrom()
        {
            return from;
        }
        public void setFrom(String form)
        {
            this.from = form;
        }
        public String getHeading()
        {
            return heading;
        }
        public void setHeading(String heading)
        {
            this.heading = heading;
        }
        public String getBody()
        {
            return body;
        }
        public void setBody(String body)
        {
            this.body = body;
        }
        
    }/**
     * 
     */
    package com.demo.xml.sax;import java.util.ArrayList;
    import java.util.List;
    import java.util.Stack;import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;
    import org.xml.sax.helpers.DefaultHandler;/**
     * @author Administrator
     * 
     */
    public class NodeXmlHandler extends DefaultHandler
    {
        // 保存已经读到过但还没关闭的标签
        Stack<String> tagsStack = new Stack<String>();
        List<NoteXml> noteXmls = new ArrayList<NoteXml>();
        NoteXml noteXml = null;    /**
         * 当遇到文档的开头的时候,调用这个方法,可以在其中做一些预处理的工作
         */
        public void startDocument() throws SAXException
        {
            System.out.println("-----------Parse begin-----------");
        }    /**
         * 当文档结束的时候,调用这个方法,可以在其中做一些善后的工作
         */
        public void endDocument() throws SAXException
        {
            System.out.println("-----------Parse end-----------");
        }    /**
         * 当读到一个开始标签会出发这个任务
         */
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException
        {
            tagsStack.push(qName);
            if (noteXml == null)
            {
                if ("notexml".equals(qName))
                {
                    noteXml = new NoteXml();
                }
            }
        }    /**
         * 当遇到结束标签会调用这个方法
         */
        public void endElement(String uri, String localName, String qName)
                throws SAXException
        {
            String currenttag = tagsStack.pop();
            if (currenttag != qName)
            {
                throw new SAXException("XML文档格式不正确,标签不匹配!");
            }
            if("Notexml".equals(qName))
            {
                noteXmls.add(noteXml);
                noteXml = null;
            }
        }
        /**
         * 处理在xml文件中读到的字符串
         */
        public void characters(char[] ch, int start, int length)
                throws SAXException
        {
            //从栈中得到当前节点的信息
            String tag = tagsStack.peek();
            String value = new String(ch,start,length);
            if("to".equals(tag))
            {
                noteXml.setTo(value);
                System.out.println("to:"+value);
            }
            if("from".equals(tag))
            {
                noteXml.setFrom(value);
                System.out.println("from:"+value);
            }
            if("heading".equals(tag))
            {
                noteXml.setHeading(value);
                System.out.println("heading:"+value);
            }
            if("body".equals(tag))
            {
                noteXml.setBody(value);
                System.out.println("body:"+value);
            }
        }
        public List<NoteXml> getResult()
        {
            return noteXmls;
        }
    }/**
     * 
     */
    package com.demo.xml.sax;import java.io.File;
    import java.util.List;import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;/**
     * 利用sax解析xml
     * 
     * @author Administrator
     * 
     */
    public class SaxXml
    {    public static List<NoteXml> readXml(String path) throws Exception
        {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            NodeXmlHandler handler = new NodeXmlHandler();
            sp.parse(new File(path), handler);
            return handler.getResult();
        }    /**
         * @param args
         */
        public static void main(String[] args)
        {
            // TODO Auto-generated method stub
            String fileName = "conf/test.xml";
            List<NoteXml> list = null;
            try
            {
                list = readXml(fileName);
            } catch (Exception e)
            {
                System.err.println(e.getMessage());
            }
        }
    }
    ---------------------打印结果如下----------------
    -----------Parse begin-----------
    to:George
    from:John
    heading:Reminder
    body:Don't forget the meeting this weekend!
    -----------Parse end-----------
    希望对楼主有帮助
      

  2.   

    /** 
        * 当遇到结束标签会调用这个方法 
        */ 
        public void endElement(String uri, String localName, String qName) 
                throws SAXException 
        { 
            String currenttag = tagsStack.pop(); 
            if (currenttag != qName) 
            { 
                throw new SAXException("XML文档格式不正确,标签不匹配!"); 
            } 
            if("Notexml".equals(qName)) 
            { 
                noteXmls.add(noteXml); 
                noteXml = null; 
            } 
        } 
    修改红色部分 把大写N 改为 小写 n
      

  3.   

    你是不是问题出在,无法解析schema数据?
    你的XML是否把schema 定义和数据本身捆绑了?