Java examples for XML:StAX
new XML Output Factory
//Licensed under the Apache License, Version 2.0 (the "License"); import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLOutputFactory; public class Main{ public static void main(String[] argv) throws Exception{ System.out.println(newXMLOutputFactory()); }// w w w .ja va 2s . co m static final String CONFIGURED_OUTPUT_FACTORY = System .getProperty("javax.xml.stream.XMLOutputFactory"); static final boolean CHECK_PARENT = Boolean .getBoolean("protostuff.loader.check_parent"); private static final String[] OUTPUT_FACTORY_IMPLS = new String[] { "com.fasterxml.aalto.stax.OutputFactoryImpl", "com.ctc.wstx.stax.WstxOutputFactory", "com.sun.xml.fastinfoset.stax.factory.StAXOutputFactory", "com.sun.xml.internal.stream.XMLOutputFactoryImpl" }; static XMLOutputFactory newXMLOutputFactory() { if (CONFIGURED_OUTPUT_FACTORY != null) { Class<?> c = loadClass(CONFIGURED_OUTPUT_FACTORY, XmlIOFactoryUtil.class, CHECK_PARENT); if (c == null) throw new IllegalStateException("Could not load class: " + CONFIGURED_OUTPUT_FACTORY); try { return (XMLOutputFactory) c.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } } for (String s : OUTPUT_FACTORY_IMPLS) { Class<?> c = loadClass(s, XmlIOFactoryUtil.class, CHECK_PARENT); if (c != null) { try { return (XMLOutputFactory) c.newInstance(); } catch (Exception e) { // print stacktrace? // e.printStackTrace(); continue; } } } throw new IllegalStateException( "Cannot find impl for javax.xml.stream.XMLOutputFactory"); } /** * Loads a class from the classloader; * If not found, the classloader of the {@code context} class specified will be used. * If the flag {@code checkParent} is true, the classloader's parent is included in * the lookup. */ static Class<?> loadClass(String className, Class<?> context, boolean checkParent) { Class<?> clazz = null; try { clazz = Thread.currentThread().getContextClassLoader() .loadClass(className); } catch (ClassNotFoundException e) { if (context != null) { ClassLoader loader = context.getClassLoader(); while (loader != null) { try { clazz = loader.loadClass(className); return clazz; } catch (ClassNotFoundException e1) { loader = checkParent ? loader.getParent() : null; } } } } return clazz; } }