本文整理匯總了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);
}
}
示例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();
}
}