當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。