當前位置: 首頁>>代碼示例>>Java>>正文


Java TransformerHandler.endElement方法代碼示例

本文整理匯總了Java中javax.xml.transform.sax.TransformerHandler.endElement方法的典型用法代碼示例。如果您正苦於以下問題:Java TransformerHandler.endElement方法的具體用法?Java TransformerHandler.endElement怎麽用?Java TransformerHandler.endElement使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在javax.xml.transform.sax.TransformerHandler的用法示例。


在下文中一共展示了TransformerHandler.endElement方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:17,代碼來源:Bug6451633.java

示例2: 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();
}
 
開發者ID:xuxueli,項目名稱:xxl-incubator,代碼行數:45,代碼來源:XmlSaxImpl.java

示例3: 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();
    }
}
 
開發者ID:succlz123,項目名稱:AndroidPixelDimenGenerator,代碼行數:44,代碼來源:SAXGenerator.java


注:本文中的javax.xml.transform.sax.TransformerHandler.endElement方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。