本文整理汇总了Java中javax.xml.transform.sax.TransformerHandler.endDocument方法的典型用法代码示例。如果您正苦于以下问题:Java TransformerHandler.endDocument方法的具体用法?Java TransformerHandler.endDocument怎么用?Java TransformerHandler.endDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.sax.TransformerHandler
的用法示例。
在下文中一共展示了TransformerHandler.endDocument方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testXMLStackOverflowBug
import javax.xml.transform.sax.TransformerHandler; //导入方法依赖的package包/类
@Test
public final void testXMLStackOverflowBug() throws TransformerConfigurationException, IOException, SAXException {
try {
SAXTransformerFactory stf = (SAXTransformerFactory) TransformerFactory.newInstance();
TransformerHandler ser = stf.newTransformerHandler();
ser.setResult(new StreamResult(System.out));
StringBuilder sb = new StringBuilder(4096);
for (int x = 4096; x > 0; x--) {
sb.append((char) x);
}
ser.characters(sb.toString().toCharArray(), 0, sb.toString().toCharArray().length);
ser.endDocument();
} catch (StackOverflowError se) {
se.printStackTrace();
Assert.fail("StackOverflow");
}
}
示例2: 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());
}
示例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();
}
}