本文整理匯總了Java中javax.xml.transform.sax.TransformerHandler.characters方法的典型用法代碼示例。如果您正苦於以下問題:Java TransformerHandler.characters方法的具體用法?Java TransformerHandler.characters怎麽用?Java TransformerHandler.characters使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.transform.sax.TransformerHandler
的用法示例。
在下文中一共展示了TransformerHandler.characters方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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: 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;
}
示例4: 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();
}
示例5: 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();
}
}