本文整理汇总了Java中javax.xml.transform.sax.TransformerHandler.startElement方法的典型用法代码示例。如果您正苦于以下问题:Java TransformerHandler.startElement方法的具体用法?Java TransformerHandler.startElement怎么用?Java TransformerHandler.startElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.sax.TransformerHandler
的用法示例。
在下文中一共展示了TransformerHandler.startElement方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import javax.xml.transform.sax.TransformerHandler; //导入方法依赖的package包/类
@Test
public void test() throws Exception {
TransformerHandler th = ((SAXTransformerFactory) TransformerFactory.newInstance()).newTransformerHandler();
DOMResult result = new DOMResult();
th.setResult(result);
th.startDocument();
th.startElement("", "root", "root", new AttributesImpl());
th.characters(new char[0], 0, 0);
th.endElement("", "root", "root");
th.endDocument();
// there's no point in having empty text --- we should remove it
Assert.assertEquals(0, ((Document) result.getNode()).getDocumentElement().getChildNodes().getLength());
}
示例2: createTransformerHandler
import javax.xml.transform.sax.TransformerHandler; //导入方法依赖的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: createXml
import javax.xml.transform.sax.TransformerHandler; //导入方法依赖的package包/类
@Override
public void createXml(List<User> list, String fileName) throws Exception {
Result resultXml = new StreamResult(new FileOutputStream(fileName)); // 输出到person.xml
SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory
.newInstance();
TransformerHandler th = sff.newTransformerHandler();
th.setResult(resultXml);
Transformer transformer = th.getTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // 编码格式是UTF-8
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // 换行
// 开始xml文档
th.startDocument();
th.startElement("", "", "users", null); // 定义users节点
for (User user : list) {
AttributesImpl attrs = new AttributesImpl();
attrs.addAttribute("", "", "id", "", String.valueOf(user.getId()));
th.startElement("", "", "user", attrs); // 定义user节点
// 定义name节点
th.startElement("", "", "name", null);
th.characters(user.getName().toCharArray(), 0, user.getName()
.length());
th.endElement("", "", "name");
// 定义age节点
th.startElement("", "", "age", null);
th.characters(String.valueOf(user.getAge()).toCharArray(), 0,
String.valueOf(user.getAge()).toCharArray().length);
th.endElement("", "", "age");
// 定义sex节点
th.startElement("", "", "sex", null);
th.characters(user.getSex().toCharArray(), 0, user.getSex()
.length());
th.endElement("", "", "sex");
th.endElement("", "", "user"); // 结束user节点
}
th.endElement("", "", "users"); // 结束users节点
// 结束xml文档
th.endDocument();
}
示例4: generate
import javax.xml.transform.sax.TransformerHandler; //导入方法依赖的package包/类
public void generate(Values values) throws IOException {
File dimenFile = values.dimenFile;
FileOutputStream fos = new FileOutputStream(dimenFile);
StreamResult result = new StreamResult(fos);
SAXTransformerFactory sff = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler th = null;
try {
th = sff.newTransformerHandler();
Transformer transformer = th.getTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
th.setResult(result);
th.startDocument();
AttributesImpl attr = new AttributesImpl();
char[] space = "\n".toCharArray();
th.characters(space, 0, space.length);
th.startElement("", "", "resources", attr);
List<Dimen> dimens = values.dimens;
char[] spaceChar = "\n ".toCharArray();
for (Dimen dimen : dimens) {
//white space
th.characters(spaceChar, 0, spaceChar.length);
//name attr
attr.addAttribute("", "", "name", String.class.getName(), dimen.name);
th.startElement("", "", "dimen", attr);
char[] valueChars = String.format("%spx", dimen.value).toCharArray();
th.characters(valueChars, 0, valueChars.length);
th.endElement("", "", "dimen");
}
th.endElement("", "", "resources");
th.endDocument();
} catch (TransformerConfigurationException | SAXException e) {
e.printStackTrace();
} finally {
fos.close();
}
}