⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 namespace.java~13~

📁 用JAVA编写的TsinghuaIP功能的源代码
💻 JAVA~13~
字号:
/**********************************************************************
 这个类的作用就是打印出XML文档中元素以及属性的名称空间名
 **********************************************************************/
package tsinghuaip;

import org.xml.sax.XMLReader;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;

public class NameSpace extends DefaultHandler {
    public void startElement(String uri, String localName, String qName,
                             Attributes atts) throws SAXException {
        print("Elem: " + qName, uri, localName);

        for (int i = 0; i < atts.getLength(); i++) {
            print("Attr: " + atts.getQName(i) + "=" + atts.getValue(i),
                  atts.getURI(i),
                  atts.getLocalName(i));
        }
    }

    public void characters(char ch[], int start, int length) throws
        SAXException {
        String strData = new String(ch, start, length).trim();
        if (strData.length() > 0) {
            try {
                System.out.println("Characters" +
                                   new String(strData.getBytes("ISO-8859-1")));
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    public void print(String t, String u, String l) {
        System.out.println(t);
        System.out.println("URI: " + u);
        System.out.println("Local name: " + l);
    }

    public static void main(String[] args) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        XMLReader reader = spf.newSAXParser().getXMLReader();
        reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        reader.setContentHandler(new NameSpace());
        reader.parse("TsinghuaIPAssign.xml");
    }
}

⌨️ 快捷键说明

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