📄 basexmlparser.java
字号:
/* * BadgeXMLParser.java * * Created on July 31, 2003, 8:53 PM */package gov.nist.examples.bps.presenceserver;import java.io.*;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.*;import org.xml.sax.helpers.DefaultHandler;import org.xml.sax.SAXException;/** * * @author olivier * @version */public class BaseXMLParser extends DefaultHandler { private Base base; private XMLReader xmlReader; /** Start the parsing * @param file to parse */ public BaseXMLParser() { try{ SAXParserFactory saxParserFactory=SAXParserFactory.newInstance(); SAXParser saxParser = saxParserFactory.newSAXParser(); xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(this); xmlReader.setFeature ("http://xml.org/sax/features/validation",false); // parse the xml specification for the event tags. } catch (SAXParseException spe) { spe.printStackTrace(); } catch (SAXException sxe) { sxe.printStackTrace(); } catch(ParserConfigurationException pce){ pce.printStackTrace(); } } public Base parseString(String text) { try { StringReader stringReader=new StringReader(text); InputSource inputSource=new InputSource(stringReader); xmlReader.parse(inputSource); return base; } catch (Exception e) { e.printStackTrace(); return null; } } //=========================================================== // SAX DocumentHandler methods //=========================================================== public void startDocument() throws SAXException { try { System.out.println("Parsing XML bases file"); } catch (Exception e) { throw new SAXException("BasesXMLParser error", e); } } public void endDocument() throws SAXException { try { System.out.println("XML Bases file parsed successfully!!!"); } catch (Exception e) { throw new SAXException("BasesXMLParser error", e); } } public void startElement(String namespaceURI, String lName, // local name String qName, // qualified name Attributes attrs) throws SAXException { String element=qName; if (element.compareToIgnoreCase("base") ==0 ) { String identifier=attrs.getValue("identifier"); if (identifier==null || identifier.trim().equals("") ) { throw new SAXException("ERROR, BaseXMLParser, startElement()"+ " the identifier attribute is missing!!!"); } String location=attrs.getValue("location"); if (location==null || location.trim().equals("") ) { throw new SAXException("ERROR, BaseXMLParser, startElement()"+ " the location attribute is missing!!!"); } base=new Base(identifier,location); } } public void endElement(String namespaceURI, String sName, // simple name String qName // qualified name ) throws SAXException { String element=qName; } public void characters(char buf[], int offset, int len) throws SAXException { String str = new String(buf, offset, len); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -