本文整理匯總了Java中org.dom4j.io.OutputFormat.createPrettyPrint方法的典型用法代碼示例。如果您正苦於以下問題:Java OutputFormat.createPrettyPrint方法的具體用法?Java OutputFormat.createPrettyPrint怎麽用?Java OutputFormat.createPrettyPrint使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.dom4j.io.OutputFormat
的用法示例。
在下文中一共展示了OutputFormat.createPrettyPrint方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getWriter
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* Get the writer for the import file
*
* @param destination the destination XML import file
* @return the XML writer
*/
private XMLWriter getWriter(String destination)
{
try
{
// Define output format
OutputFormat format = OutputFormat.createPrettyPrint();
format.setNewLineAfterDeclaration(false);
format.setIndentSize(INDENT_SIZE);
format.setEncoding(this.fileEncoding);
return new XMLWriter(new FileOutputStream(destination), format);
}
catch (Exception exception)
{
throw new AlfrescoRuntimeException("Unable to create XML writer.", exception);
}
}
示例2: printXML
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* 打印XML
*
* @param document
*/
protected void printXML(Document document) {
if (log.isInfoEnabled()) {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setExpandEmptyElements(true);
format.setSuppressDeclaration(true);
StringWriter stringWriter = new StringWriter();
XMLWriter writer = new XMLWriter(stringWriter, format);
try {
writer.write(document);
log.info(stringWriter.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例3: startTransferReport
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* Start the transfer report
*/
public void startTransferReport(String encoding, Writer writer) throws SAXException
{
OutputFormat format = OutputFormat.createPrettyPrint();
format.setNewLineAfterDeclaration(false);
format.setIndentSize(3);
format.setEncoding(encoding);
this.writer = new XMLWriter(writer, format);
this.writer.startDocument();
this.writer.startPrefixMapping(PREFIX, TransferReportModel2.TRANSFER_REPORT_MODEL_2_0_URI);
// Start Transfer Manifest // uri, name, prefix
this.writer.startElement(TransferReportModel2.TRANSFER_REPORT_MODEL_2_0_URI, TransferReportModel.LOCALNAME_TRANSFER_REPORT, PREFIX + ":" + TransferReportModel.LOCALNAME_TRANSFER_REPORT, EMPTY_ATTRIBUTES);
}
示例4: convertXML
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* Performs XML conversion from ADN to oai_dc format. Characters are encoded as UTF-8.
*
* @param xml XML input in the 'adn' format.
* @param docReader A lucene doc reader for this record.
* @param context The servlet context where this is running.
* @return XML in the converted 'oai_dc' format.
*/
public String convertXML(String xml, XMLDocReader docReader, ServletContext context) {
getXFormFilesAndIndex(context);
try {
Transformer transformer = XSLTransformer.getTransformer(transform_file.getAbsolutePath());
String transformed_content = XSLTransformer.transformString(xml, transformer);
SAXReader reader = new SAXReader();
Document document = DocumentHelper.parseText(transformed_content);
// Dom4j automatically writes using UTF-8, unless otherwise specified.
OutputFormat format = OutputFormat.createPrettyPrint();
StringWriter outputWriter = new StringWriter();
XMLWriter writer = new XMLWriter(outputWriter, format);
writer.write(document);
outputWriter.close();
writer.close();
return outputWriter.toString();
} catch (Throwable e) {
System.err.println("NCS_ITEMToNSDL_DCFormatConverter was unable to produce transformed file: " + e);
e.printStackTrace();
return "";
}
}
示例5: prettyPrint
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* Formats an {@link org.dom4j.Node} as a printable string
*
* @param node the Node to display
* @return a nicley-formatted String representation of the Node.
*/
public static String prettyPrint(Node node) {
StringWriter sw = new StringWriter();
OutputFormat format = OutputFormat.createPrettyPrint();
format.setXHTML(true);
//Default is false, this produces XHTML
HTMLWriter ppWriter = new HTMLWriter(sw, format);
try {
ppWriter.write(node);
ppWriter.flush();
} catch (Exception e) {
return ("Pretty Print Failed");
}
return sw.toString();
}
示例6: parseXMLToString
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* xml 2 string
*
* @param document xml document
* @return
*/
public static String parseXMLToString(Document document) {
Assert.notNull(document);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
StringWriter writer = new StringWriter();
XMLWriter xmlWriter = new XMLWriter(writer, format);
try {
xmlWriter.write(document);
xmlWriter.close();
} catch (IOException e) {
throw new RuntimeException("XML解析發生錯誤");
}
return writer.toString();
}
示例7: documentToString
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* Devuelve la representaci�n de un Document XML en String bien formateado
* y con codificaci�n UTF-8.
* @param doc Documento.
* @return string representando el documento formateado y en UTF-8.
*/
private String documentToString(Document doc) {
String result = null;
StringWriter writer = new StringWriter();
OutputFormat of = OutputFormat.createPrettyPrint();
of.setEncoding("UTF-8");
XMLWriter xmlWriter = new XMLWriter(writer, of);
try {
xmlWriter.write(doc);
xmlWriter.close();
result = writer.toString();
} catch (IOException e) {
log.error("Error escribiendo xml", e);
}
return result;
}
示例8: doc2XmlFile
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* doc2XmlFile
* 將Document對象保存為一個xml文件到本地
* @return true:保存成功 flase:失敗
* @param filename 保存的文件名
* @param document 需要保存的document對象
*/
public static boolean doc2XmlFile(Document document,String filename)
{
boolean flag = true;
try{
/* 將document中的內容寫入文件中 */
//默認為UTF-8格式,指定為"GB2312"
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GB2312");
XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)),format);
writer.write(document);
writer.close();
}catch(Exception ex){
flag = false;
ex.printStackTrace();
}
return flag;
}
示例9: writeToXML
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
public static boolean writeToXML(Document document, String tempPath) {
try {
// 使用XMLWriter寫入,可以控製格式,經過調試,發現這種方式會出現亂碼,主要是因為Eclipse中xml文件和JFrame中文件編碼不一致造成的
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding(EncodingUtil.CHARSET_UTF8);
// format.setSuppressDeclaration(true);//這句話會壓製xml文件的聲明,如果為true,就不打印出聲明語句
format.setIndent(true);// 設置縮進
format.setIndent(" ");// 空行方式縮進
format.setNewlines(true);// 設置換行
XMLWriter writer = new XMLWriter(new FileWriterWithEncoding(new File(tempPath), EncodingUtil.CHARSET_UTF8), format);
writer.write(document);
writer.close();
} catch (IOException e) {
e.printStackTrace();
MyLogger.logger.error("寫入xml文件出錯!");
return false;
}
return true;
}
示例10: perform
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
public void perform(TaskRequest req, TaskResponse res) {
File f = new File((String) file.evaluate(req, res));
if (f.getParentFile() != null) {
// Make sure the necessary directories are in place...
f.getParentFile().mkdirs();
}
try {
XMLWriter writer = new XMLWriter(new FileOutputStream(f), OutputFormat.createPrettyPrint());
writer.write((Node) node.evaluate(req, res));
} catch (Throwable t) {
String msg = "Unable to write the specified file: " + f.getPath();
throw new RuntimeException(msg, t);
}
}
示例11: formatXml
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* Returns the given xml document as nicely formated string.
*
* @param node
* The xml document.
* @return the formated xml as string.
*/
private static String formatXml(Node node) {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setIndentSize(4);
format.setTrimText(true);
format.setExpandEmptyElements(true);
StringWriter stringWriter = new StringWriter();
XMLWriter xmlWriter = new XMLWriter(stringWriter, format);
try {
xmlWriter.write(node);
xmlWriter.flush();
} catch (IOException e) {
// this should never happen
throw new RuntimeException(e);
}
return stringWriter.getBuffer().toString();
}
示例12: exportAndImportToQTIFormat
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
private static QTIDocument exportAndImportToQTIFormat(QTIDocument qtiDocOrig) throws IOException {
Document qtiXmlDoc = qtiDocOrig.getDocument();
OutputFormat outformat = OutputFormat.createPrettyPrint();
String fileName = qtiDocOrig.getAssessment().getTitle() + "QTIFormat.xml";
OutputStreamWriter qtiXmlOutput = new OutputStreamWriter(new FileOutputStream(new File(TEMP_DIR, fileName)), Charset.forName("UTF-8"));
XMLWriter writer = new XMLWriter(qtiXmlOutput, outformat);
writer.write(qtiXmlDoc);
writer.flush();
writer.close();
XMLParser xmlParser = new XMLParser(new IMSEntityResolver());
Document doc = xmlParser.parse(new FileInputStream(new File(TEMP_DIR, fileName)), true);
ParserManager parser = new ParserManager();
QTIDocument qtiDocRestored = (QTIDocument) parser.parse(doc);
return qtiDocRestored;
}
示例13: writeToFile
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* writes the manifest.xml
*/
void writeToFile() {
final String filename = "imsmanifest.xml";
final OutputFormat format = OutputFormat.createPrettyPrint();
try {
VFSLeaf outFile;
// file may exist
outFile = (VFSLeaf) cpcore.getRootDir().resolve("/" + filename);
if (outFile == null) {
// if not, create it
outFile = cpcore.getRootDir().createChildLeaf("/" + filename);
}
final DefaultDocument manifestDocument = cpcore.buildDocument();
final XMLWriter writer = new XMLWriter(outFile.getOutputStream(false), format);
writer.write(manifestDocument);
} catch (final Exception e) {
log.error("imsmanifest for ores " + ores.getResourceableId() + "couldn't be written to file.", e);
throw new OLATRuntimeException(CPOrganizations.class, "Error writing imsmanifest-file", new IOException());
}
}
示例14: prettyPrint
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
public static String prettyPrint(final String xml) {
if (StringUtils.isBlank(xml)) {
throw new RuntimeException("xml was null or blank in prettyPrint()");
}
final StringWriter sw;
try {
final OutputFormat format = OutputFormat.createPrettyPrint();
final org.dom4j.Document document = DocumentHelper.parseText(xml);
sw = new StringWriter();
final XMLWriter writer = new XMLWriter(sw, format);
writer.write(document);
} catch (Exception e) {
throw new RuntimeException("Error pretty printing xml:\n" + xml, e);
}
String[] xmlArray = StringUtils.split(sw.toString(), '\n');
Object[] xmlContent = ArrayUtils.subarray(xmlArray, 1, xmlArray.length);
StringBuilder xmlStr = new StringBuilder();
for (Object object : xmlContent) {
xmlStr.append(object.toString());
xmlStr.append('\n');
}
return xmlStr.toString();
}
示例15: startTransferManifest
import org.dom4j.io.OutputFormat; //導入方法依賴的package包/類
/**
* Start the transfer manifest
*/
public void startTransferManifest(Writer writer) throws SAXException
{
format = OutputFormat.createPrettyPrint();
format.setNewLineAfterDeclaration(false);
format.setIndentSize(3);
format.setEncoding("UTF-8");
this.writer = new XMLWriter(writer, format);
this.writer.startDocument();
this.writer.startPrefixMapping(PREFIX, TransferModel.TRANSFER_MODEL_1_0_URI);
this.writer.startPrefixMapping("cm", NamespaceService.CONTENT_MODEL_1_0_URI);
// Start Transfer Manifest // uri, name, prefix
this.writer.startElement(TransferModel.TRANSFER_MODEL_1_0_URI,
ManifestModel.LOCALNAME_TRANSFER_MAINIFEST, PREFIX + ":"
+ ManifestModel.LOCALNAME_TRANSFER_MAINIFEST, EMPTY_ATTRIBUTES);
}