本文整理汇总了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();
}
}