下面列出了怎么用org.xml.sax.helpers.XMLReaderAdapter的API类实例代码及写法,或者点击链接到github查看源代码。
private void parse(){
try{
Parser p = new XMLReaderAdapter(XMLUtil.createXMLReader());
p.setDocumentHandler(this);
String externalForm = url.toExternalForm();
InputSource is = new InputSource(externalForm);
try {
p.parse(is);
} catch (NullPointerException npe) {
npe.printStackTrace();
if (npe.getCause() != null) {
npe.getCause().printStackTrace();
}
}
}
catch(java.io.IOException ie){
ie.printStackTrace();
}
catch(org.xml.sax.SAXException se){
se.printStackTrace();
}
}
private void parseTheme(){
try{
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
Parser p = new XMLReaderAdapter(factory.newSAXParser().getXMLReader());
p.setDocumentHandler(this);
String externalForm = themeURL.toExternalForm();
InputSource is = new InputSource(externalForm);
p.parse(is);
activeThemes=null; //dispose of now useless hashtable
locator = null;
}
catch(java.io.IOException ie){
System.err.println ("IO exception reading theme file"); //NOI18N
} catch(org.xml.sax.SAXException se){
System.err.println ("Error parsing theme file " + (locator != null ? "line " + locator.getLineNumber() : "")); //NOI18N
} catch (ParserConfigurationException e) {
System.err.println ("Couldn't create XML parser for theme file"); //NOI18N
}
}
public void startPrefixMapping(String prefix, String uri) {
System.out.println("startPrefixMapping() is invoked for " + prefix + " : " + uri);
try {
new XMLReaderAdapter().startPrefixMapping(prefix, uri);
} catch (SAXException e) {
e.printStackTrace();
}
}
public void endPrefixMapping(String prefix) {
System.out.println("\nendPrefixMapping() is invoked for " + prefix);
try {
new XMLReaderAdapter().endPrefixMapping(prefix);
} catch (SAXException e) {
e.printStackTrace();
}
}
public void skippedEntity(String name) {
try {
System.out.println("skippedEntity() is invoked for : " + name);
new XMLReaderAdapter().skippedEntity(name);
} catch (SAXException e) {
e.printStackTrace();
}
}
@Test(expectedExceptions = SAXException.class)
public void testcreateXMLReader() throws SAXException, ParserConfigurationException {
String className = SAXParserFactory.newInstance().newSAXParser()
.getXMLReader().getClass().getName();
setSystemProperty(SAX_PROPNAME, className + "nosuch");
XMLReaderAdapter adapter = new XMLReaderAdapter();
}
/**
* Initiate ParserAdapter.
* @throws Exception If any errors occur.
*/
ParserAdapterTest() throws Exception {
SAXParserFactory spf = SAXParserFactory.newInstance();
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
XMLReaderAdapter xmlReaderAdapter = new XMLReaderAdapter(xmlReader);
parserAdapter = new ParserAdapter(xmlReaderAdapter);
}
/**
* To test the parse method. The specification says that this method
* will throw an exception if the embedded XMLReader does not support
* the http://xml.org/sax/features/namespace-prefixes property.
*
* @throws Exception If any errors occur.
*/
@Test
public void parse01() throws Exception {
try (FileInputStream fis = new FileInputStream(XML_DIR + "namespace1.xml")) {
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
if (!xmlReader.getFeature(NM_PREFIXES_PROPERTY)) {
xmlReader.setFeature(NM_PREFIXES_PROPERTY, true);
}
XMLReaderAdapter xmlRA = new XMLReaderAdapter(xmlReader);
xmlRA.setDocumentHandler(new HandlerBase());
xmlRA.parse(new InputSource(fis));
}
}
public void testXMLReaderAdapter() {
System.setProperty("org.xml.sax.driver",
"org.apache.harmony.tests.org.xml.sax.support.DoNothingXMLReader");
try {
new XMLReaderAdapter();
} catch (SAXException e) {
throw new RuntimeException("Unexpected exception", e);
}
}
/**
* @deprecated
*/
public org.xml.sax.Parser getParser() throws SAXException {
return new XMLReaderAdapter(reader);
}
/**
* @deprecated
*/
public org.xml.sax.Parser getParser() throws SAXException {
return new XMLReaderAdapter(reader);
}
/**
* @deprecated
*/
public org.xml.sax.Parser getParser() throws SAXException {
return new XMLReaderAdapter(reader);
}
/**
* @deprecated
*/
public org.xml.sax.Parser getParser() throws SAXException {
return new XMLReaderAdapter(reader);
}
/**
* @deprecated
*/
public org.xml.sax.Parser getParser() throws SAXException {
return new XMLReaderAdapter(reader);
}
/**
* To test the constructor that uses XMLReader.
*
* @throws Exception If any errors occur.
*/
@Test
public void constructor02() throws Exception {
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
assertNotNull(new XMLReaderAdapter(xmlReader));
}
/**
* @deprecated
*/
public org.xml.sax.Parser getParser() throws SAXException {
return new XMLReaderAdapter(reader);
}
/**
* @deprecated
*/
public org.xml.sax.Parser getParser() throws SAXException {
return new XMLReaderAdapter(reader);
}
/**
* @deprecated
*/
public org.xml.sax.Parser getParser() throws SAXException {
return new XMLReaderAdapter(reader);
}
/**
* @deprecated
*/
public org.xml.sax.Parser getParser() throws SAXException {
return new XMLReaderAdapter(reader);
}
/**
* To test the constructor that uses "org.xml.sax.driver" property
* @throws org.xml.sax.SAXException If the embedded driver cannot be
* instantiated or if the org.xml.sax.driver property is not specified.
*/
@Test
public void constructor01() throws SAXException {
assertNotNull(new XMLReaderAdapter());
}