NameSpace.java~10~
资源名称:Java.rar [点击查看]
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:2k
源码类别:
Java编程
开发平台:
Java
- /**********************************************************************
- 这个类的作用就是打印出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 );
- System.out.println("here1");
- 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){
- System.out.println("Characters"+strData);
- }
- }
- 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");
- }
- }