xmltest.java

来自「一个简单的游戏程序」· Java 代码 · 共 89 行

JAVA
89
字号
package pushbook;


import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
import javax.xml.parsers.*;


public class XMLTest extends DefaultHandler
{
    public void startDocument() throws SAXException
    {
    }

    public void endDocument () throws SAXException
    {
    }


    public void startElement(String namespaceURI, String localName, String qName, Attributes attr) throws
        SAXException
    {
        if (qName.equals("title"))
        {
            System.out.print("关卡名称:");
        }
        else if (qName.equals("music"))
        {
            System.out.print("背景音乐:");
        }
        else if (qName.equals("unitSize"))
        {
            System.out.print("图形元素尺寸:");
        }
        else if (qName.equals("element"))
        {
            System.out.println("元素名称:" +
                               attr.getValue("ID") +
                               "    图片:" + attr.getValue("img"));
        }
        else if (qName.equals("mainMap"))
        {
            System.out.println("主地图:");
        }
        else if (qName.equals("activeMap"))
        {
            System.out.println("可动元素地图:");
        }
    }


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


    public void characters(char[] ch, int start, int length) throws SAXException
    {
        try
        {
            String tt = new String(ch);
            tt = tt.substring(start, start + length);
            if (tt.indexOf("\n") >= 0)return;
            System.out.println(tt);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }


    public static void main(String[] argv)
    {
        try
        {
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser saxparser = saxparser = spf.newSAXParser();
            File XMLFile = new File("D:/Projects/PushBook/src/resource/level_001.xml");
            saxparser.parse(XMLFile, new XMLTest());
        }
        catch (Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?