本文整理汇总了Java中javax.xml.transform.sax.SAXTransformerFactory.setAttribute方法的典型用法代码示例。如果您正苦于以下问题:Java SAXTransformerFactory.setAttribute方法的具体用法?Java SAXTransformerFactory.setAttribute怎么用?Java SAXTransformerFactory.setAttribute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.sax.SAXTransformerFactory
的用法示例。
在下文中一共展示了SAXTransformerFactory.setAttribute方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setTraxFactoryAttributes
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Set to the SAX TrAX Factory attributes by inspecting the given parameters
* for those which are from TrAX namespace
*/
protected void setTraxFactoryAttributes (final SAXTransformerFactory saxtf, final Hashtable <String, Object> params)
{
// loop over all parameters
final Enumeration <String> e = params.keys ();
while (e.hasMoreElements ())
{
final String key = e.nextElement ();
// is this one from TrAX namespace?
if (key.startsWith (tmp_TRAX_ATTR_NS))
{
// it is, remove the namespace prefix and set it to the factory
final String name = key.substring (tmp_TRAX_ATTR_NS.length ()).toLowerCase ();
saxtf.setAttribute (name, params.get (key));
if (CSTX.DEBUG)
log.debug ("newTHOutOfTrAX(): set factory attribute " + name + "=" + params.get (key));
}
}
}
示例2: createTransformerHandler
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Creates a {@link TransformerHandler}.
*
* @param commentHeader the comment header
* @param rootTag the root tag
* @param streamResult stream result
*/
public static TransformerHandler createTransformerHandler(String commentHeader, String rootTag,
StreamResult streamResult, Charset charset)
throws TransformerFactoryConfigurationError,
TransformerConfigurationException, SAXException {
SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
try {
tf.setAttribute("indent-number", new Integer(2));
} catch (Exception e) {
// ignore, workaround for JDK 1.5 bug, see http://forum.java.sun.com/thread.jspa?threadID=562510
}
TransformerHandler transformerHandler = tf.newTransformerHandler();
Transformer serializer = transformerHandler.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, charset.name());
serializer.setOutputProperty(OutputKeys.METHOD, "xml");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
transformerHandler.setResult(streamResult);
transformerHandler.startDocument();
String newline = System.getProperty("line.separator");
if (newline == null) {
newline = "\n";
}
commentHeader = (newline + commentHeader).replaceAll("\\n--", newline + " ");
transformerHandler.characters("\n".toCharArray(), 0, 1);
transformerHandler.comment(commentHeader.toCharArray(), 0, commentHeader.toCharArray().length);
transformerHandler.characters("\n".toCharArray(), 0, 1);
if (rootTag.length() > 0) {
transformerHandler.startElement("", "", rootTag, null);
}
return transformerHandler;
}
示例3: createTransformerHandler
import javax.xml.transform.sax.SAXTransformerFactory; //导入方法依赖的package包/类
/**
* Creates a {@link TransformerHandler}.
*
* @param commentHeader the comment header
* @param rootTag the root tag
* @param streamResult stream result
*/
public static TransformerHandler createTransformerHandler(String commentHeader, String rootTag,
StreamResult streamResult)
throws TransformerFactoryConfigurationError,
TransformerConfigurationException, SAXException {
SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance();
try {
tf.setAttribute("indent-number", new Integer(2));
} catch (Exception e) {
// ignore, workaround for JDK 1.5 bug, see http://forum.java.sun.com/thread.jspa?threadID=562510
}
TransformerHandler transformerHandler = tf.newTransformerHandler();
Transformer serializer = transformerHandler.getTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, Charset.defaultCharset().name());
serializer.setOutputProperty(OutputKeys.METHOD, "xml");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
transformerHandler.setResult(streamResult);
transformerHandler.startDocument();
String newline = System.getProperty("line.separator");
if (newline == null) {
newline = "\n";
}
commentHeader = (newline + commentHeader).replaceAll("\\n--", newline + " ");
transformerHandler.comment(commentHeader.toCharArray(), 0, commentHeader.toCharArray().length);
if (rootTag.length() > 0) {
transformerHandler.startElement("", "", rootTag, null);
}
return transformerHandler;
}