NameSpace.java~6~
资源名称:Java.rar [点击查看]
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:1k
源码类别:
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 );
- for (int i = 0; i < atts.getLength(); i++){
- print("Attr: " + atts.getQName(i) + "=" + atts.getValue(i),
- atts.getURI(i),
- atts.getLocalName(i));
- System.out.println("here");
- }
- }
- public void print(String t, String u, String l){
- System.out.println(t);
- System.out.println("URI: " + u);
- System.out.println("Local name: " + l);
- System.out.println("here");
- }
- 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");
- }
- }