当前位置: 首页>>代码示例>>Java>>正文


Java Document.PROCESSING_INSTRUCTION_NODE属性代码示例

本文整理汇总了Java中org.w3c.dom.Document.PROCESSING_INSTRUCTION_NODE属性的典型用法代码示例。如果您正苦于以下问题:Java Document.PROCESSING_INSTRUCTION_NODE属性的具体用法?Java Document.PROCESSING_INSTRUCTION_NODE怎么用?Java Document.PROCESSING_INSTRUCTION_NODE使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在org.w3c.dom.Document的用法示例。


在下文中一共展示了Document.PROCESSING_INSTRUCTION_NODE属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: prettyPrintDOMWithEncoding

public static void prettyPrintDOMWithEncoding(Document doc, String defaultEncoding, Result result) {
	Node firstChild = doc.getFirstChild();
	boolean omitXMLDeclaration = false;
	String encoding = defaultEncoding; // default Encoding char set if non
										// is found in the PI

	if ((firstChild.getNodeType() == Document.PROCESSING_INSTRUCTION_NODE)
			&& (firstChild.getNodeName().equals("xml"))) {
		omitXMLDeclaration = true;
		String piValue = firstChild.getNodeValue();
		// extract from PI the encoding Char Set
		int encodingOffset = piValue.indexOf("encoding=\"");
		if (encodingOffset != -1) {
			encoding = piValue.substring(encodingOffset + 10);
			// remove the last "
			encoding = encoding.substring(0, encoding.length() - 1);
		}
	}

	try {
		Transformer t = getNewTransformer();
		t.setOutputProperty(OutputKeys.ENCODING, encoding);
		t.setOutputProperty(OutputKeys.INDENT, "yes");
		t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html, text
		t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
		t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXMLDeclaration ? "yes" : "no");
		t.transform(new DOMSource(doc), result);
	} catch (Exception e) {
		Engine.logEngine.error("Unexpected exception while pretty print DOM", e);
	}
}
 
开发者ID:convertigo,项目名称:convertigo-engine,代码行数:31,代码来源:XMLUtils.java

示例2: prettyPrintElement

public static String prettyPrintElement(Element elt, boolean omitXMLDeclaration, boolean bIndent) {
	Node firstChild = elt;
	String encoding = "ISO-8859-1"; // default Encoding char set if non is
									// found in the PI

	if (omitXMLDeclaration && (firstChild.getNodeType() == Document.PROCESSING_INSTRUCTION_NODE)
			&& (firstChild.getNodeName().equals("xml"))) {
		String piValue = firstChild.getNodeValue();
		// extract from PI the encoding Char Set
		int encodingOffset = piValue.indexOf("encoding=\"");
		if (encodingOffset != -1) {
			encoding = piValue.substring(encodingOffset + 10);
			// remove the last "
			encoding = encoding.substring(0, encoding.length() - 1);
		}
	}
	StringWriter strWtr = new StringWriter();
	try {
		Transformer t = getNewTransformer();
		t.setOutputProperty(OutputKeys.ENCODING, encoding);
		t.setOutputProperty(OutputKeys.INDENT, bIndent ? "yes" : "no");
		t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html, text
		t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
		t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXMLDeclaration ? "yes" : "no");
		t.transform(new DOMSource(elt), new StreamResult(strWtr));
		return strWtr.getBuffer().toString();
	} catch (Exception e) {
		System.err.println("XML.toString(Document): " + e);
		Engine.logEngine.error("Unexpected exception", e);
		return e.getMessage();
	}
}
 
开发者ID:convertigo,项目名称:convertigo-engine,代码行数:32,代码来源:XMLUtils.java


注:本文中的org.w3c.dom.Document.PROCESSING_INSTRUCTION_NODE属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。