org.dom4j.io.OutputFormat.createCompactFormat()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(10.5k)|赞(0)|评价(0)|浏览(203)

本文整理了Java中org.dom4j.io.OutputFormat.createCompactFormat()方法的一些代码示例,展示了OutputFormat.createCompactFormat()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。OutputFormat.createCompactFormat()方法的具体详情如下:
包路径:org.dom4j.io.OutputFormat
类名称:OutputFormat
方法名:createCompactFormat

OutputFormat.createCompactFormat介绍

[英]A static helper method to create the default compact format. This format does not have any indentation or newlines after an alement and all other whitespace trimmed
[中]创建默认压缩格式的静态助手方法。这种格式在删除和所有其他空白后没有任何缩进或换行

代码示例

代码示例来源:origin: org.craftercms/crafter-search-batch-indexer

protected String documentToString(Document document) {
  StringWriter stringWriter = new StringWriter();
  OutputFormat format = OutputFormat.createCompactFormat();
  XMLWriter xmlWriter = new XMLWriter(stringWriter, format);
  try {
    xmlWriter.write(document);
  } catch (IOException e) {
    // Ignore, shouldn't happen.
  }
  return stringWriter.toString();
}

代码示例来源:origin: net.anwiba.commons/anwiba-commons-xml

private static OutputFormat createTrimedOutputFormat(final String encoding) {
 final OutputFormat format = OutputFormat.createCompactFormat();
 format.setEncoding(encoding);
 return format;
}

代码示例来源:origin: org.jbpm.jbpm3/jbpm-jpdl

private XMLWriter getXmlWriter() {
 if (xmlWriter == null) {
  try {
   // configuration is saved to database, write in a compact format
   xmlWriter = new XMLWriter(OutputFormat.createCompactFormat());
  }
  catch (UnsupportedEncodingException e) {
   // WTF?
   throw new JbpmException("failed to create xml writer", e);
  }
 }
 return xmlWriter;
}

代码示例来源:origin: pentaho/modeler

/**
 *
 * @param xml
 * @return
 * @throws Exception
 */
public static String compactPrint( String xml ) throws Exception {
 final OutputFormat format = OutputFormat.createCompactFormat();
 return print( xml, format );
}

代码示例来源:origin: com.github.albfernandez/jbpm-jpdl

private XMLWriter getXmlWriter() {
 if (xmlWriter == null) {
  try {
   // configuration is saved to database, write in a compact format
   xmlWriter = new XMLWriter(OutputFormat.createCompactFormat());
  }
  catch (UnsupportedEncodingException e) {
   // WTF?
   throw new JbpmException("failed to create xml writer", e);
  }
 }
 return xmlWriter;
}

代码示例来源:origin: zhoulychn/mybatis-generator

/**
   * 将一个XML文档保存至文件中.
   *
   * @param filePathName 要保存到的文档路径.
   * @param doc      要保存的XML文档对象.
   * @return true代表保存成功,否则代表不成功.
   */
  public static boolean writeToXml(String filePathName, Document doc) {
    OutputFormat format = OutputFormat.createCompactFormat();
    format.setEncoding("GBK");
    return savaToFile(doc, filePathName, format);
  }
}

代码示例来源:origin: pentaho/pentaho-platform

public static void saveDom( final Document doc, final OutputStream outputStream, String encoding,
  boolean suppressDeclaration, boolean prettyPrint ) throws IOException {
 OutputFormat format = prettyPrint ? OutputFormat.createPrettyPrint() : OutputFormat.createCompactFormat();
 format.setSuppressDeclaration( suppressDeclaration );
 if ( encoding != null ) {
  format.setEncoding( encoding.toLowerCase() );
  if ( !suppressDeclaration ) {
   doc.setXMLEncoding( encoding.toUpperCase() );
  }
 }
 XMLWriter writer = new XMLWriter( outputStream, format );
 writer.write( doc );
 writer.flush();
}

代码示例来源:origin: org.alfresco/alfresco-repository

public XMLWriter(OutputStream outputStream, boolean prettyPrint, String encoding)
    throws UnsupportedEncodingException
{
  OutputFormat format = prettyPrint ? OutputFormat.createPrettyPrint() : OutputFormat.createCompactFormat();
  format.setNewLineAfterDeclaration(false);
  format.setIndentSize(3);
  format.setEncoding(encoding);
  output = outputStream;
  this.dom4jWriter = new org.dom4j.io.XMLWriter(outputStream, format);
}

代码示例来源:origin: com.axway.ats.framework/ats-actionlibrary

@Override
@PublicAtsApi
public String toString() {
  try {
    return getFormattedString(OutputFormat.createCompactFormat());
  } catch (XMLException e) {
    log.error(e.getMessage(), e.getCause());
    return "";
  }
}

代码示例来源:origin: Alfresco/alfresco-repository

public XMLWriter(OutputStream outputStream, boolean prettyPrint, String encoding)
    throws UnsupportedEncodingException
{
  OutputFormat format = prettyPrint ? OutputFormat.createPrettyPrint() : OutputFormat.createCompactFormat();
  format.setNewLineAfterDeclaration(false);
  format.setIndentSize(3);
  format.setEncoding(encoding);
  output = outputStream;
  this.dom4jWriter = new org.dom4j.io.XMLWriter(outputStream, format);
}

代码示例来源:origin: dom4j/dom4j

public void testPenguin() throws IOException {
  // U+1F427 PENGUIN
  final String penguin = "\ud83d\udc27";
  Document document = DocumentHelper.createDocument();
  document.addElement("doc").setText(penguin);
  OutputFormat outputFormat = OutputFormat.createCompactFormat();
  outputFormat.setSuppressDeclaration(true);
  StringWriter stringWriter = new StringWriter();
  XMLWriter writer = new XMLWriter(stringWriter, outputFormat);
  writer.write(document);
  writer.close();
  Assert.assertEquals(stringWriter.toString(), "<doc>"+penguin+"</doc>");
}

代码示例来源:origin: dom4j/dom4j

public void testAttributeQuotes() throws Exception {
  Document doc = DocumentFactory.getInstance().createDocument();
  doc.addElement("root").addAttribute("test", "text with ' in it");
  StringWriter out = new StringWriter();
  XMLWriter writer = new XMLWriter(out, OutputFormat
      .createCompactFormat());
  writer.write(doc);
  String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
      + "<root test=\"text with ' in it\"/>";
  assertEquals(expected, out.toString());
}

代码示例来源:origin: dom4j/dom4j

public void testElementNamespaceWriteDocument() throws IOException {
  Document document = DocumentHelper.createDocument();
  Element root = document.addElement("rss")
      .addNamespace("g", "http://base.google.com/ns/1.0")
      .addNamespace("c", "http://base.google.com/cns/1.0");
  OutputFormat outputFormat = OutputFormat.createCompactFormat();
  outputFormat.setSuppressDeclaration(true);
  StringWriter stringWriter = new StringWriter();
  XMLWriter writer = new XMLWriter(stringWriter, outputFormat);
  writer.write(document);
  writer.close();
  Assert.assertEquals(stringWriter.toString(), "<rss xmlns:g=\"http://base.google.com/ns/1.0\" xmlns:c=\"http://base.google.com/cns/1.0\"></rss>");
}

代码示例来源:origin: dom4j/dom4j

public void testSurrogatePairAttribute() throws IOException {
  // U+1F427 PENGUIN
  final String penguin = "\ud83d\udc27";
  Document document = DocumentHelper.createDocument();
  document.addElement("doc").addAttribute("penguin", penguin);
  OutputFormat outputFormat = OutputFormat.createCompactFormat();
  outputFormat.setSuppressDeclaration(true);
  outputFormat.setEncoding("US-ASCII");
  StringWriter stringWriter = new StringWriter();
  XMLWriter writer = new XMLWriter(stringWriter, outputFormat);
  writer.write(document);
  writer.close();
  Assert.assertEquals(stringWriter.toString(), "<doc penguin=\"&#128039;\"/>");
}

代码示例来源:origin: dom4j/dom4j

public void testSurrogatePairElement() throws IOException {
  // U+1F427 PENGUIN
  final String penguin = "\ud83d\udc27";
  Document document = DocumentHelper.createDocument();
  document.addElement("doc").setText(penguin);
  OutputFormat outputFormat = OutputFormat.createCompactFormat();
  outputFormat.setSuppressDeclaration(true);
  outputFormat.setEncoding("US-ASCII");
  StringWriter stringWriter = new StringWriter();
  XMLWriter writer = new XMLWriter(stringWriter, outputFormat);
  writer.write(document);
  writer.close();
  Assert.assertEquals(stringWriter.toString(), "<doc>&#128039;</doc>");
}

代码示例来源:origin: dom4j/dom4j

public void testBug926752() throws Exception {
    org.dom4j.Document doc = getDocument("/xml/test/defaultNamespace.xml");
    DOMWriter writer = new DOMWriter(org.dom4j.dom.DOMDocument.class);
    org.w3c.dom.Document result = writer.write(doc);

    NamedNodeMap atts = result.getDocumentElement().getAttributes();
    assertEquals(1, atts.getLength());

    OutputFormat format = OutputFormat.createCompactFormat();
    format.setSuppressDeclaration(true);

    XMLWriter wr = new XMLWriter(format);
    StringWriter strWriter = new StringWriter();
    wr.setWriter(strWriter);
    wr.write((org.dom4j.Document) result);
    assertEquals("<a xmlns=\"dummyNamespace\"><b><c>Hello</c></b></a>",
        strWriter.toString());
  }
}

代码示例来源:origin: dom4j/dom4j

public void testElementNamespaceWriteOpen() throws IOException {
  Document document = DocumentHelper.createDocument();
  Element root = document.addElement("rss")
      .addNamespace("g", "http://base.google.com/ns/1.0")
      .addNamespace("c", "http://base.google.com/cns/1.0");
  OutputFormat outputFormat = OutputFormat.createCompactFormat();
  outputFormat.setSuppressDeclaration(true);
  StringWriter stringWriter = new StringWriter();
  XMLWriter writer = new XMLWriter(stringWriter, outputFormat);
  writer.writeOpen(root);
  writer.close();
  Assert.assertEquals(stringWriter.toString(), "<rss xmlns:g=\"http://base.google.com/ns/1.0\" xmlns:c=\"http://base.google.com/cns/1.0\">");
}

代码示例来源:origin: dom4j/dom4j

public void testWithCompactFormatMerged() throws Exception {
  String out = readwriteText(OutputFormat.createCompactFormat(), true);
  if (VERBOSE) {
    log("Text output is [");
    log(out);
    log("]. Done");
  }
  // should contain &amp; and &lt;
  assertTrue("Output text contains \"&amp;\"", out
      .lastIndexOf("&amp;") >= 0);
  assertTrue("Output text contains \"&lt;\"",
      out.lastIndexOf("&lt;") >= 0);
}

代码示例来源:origin: dom4j/dom4j

public void testWithCompactFormatNonMerged() throws Exception {
  String outText = readwriteText(OutputFormat.createCompactFormat(),
      false);
  if (VERBOSE) {
    log("Text output is [");
    log(outText);
    log("]. Done");
  }
  // should contain &amp; and &lt;
  assertTrue("Output text contains \"&amp;\"", outText
      .lastIndexOf("&amp;") >= 0);
  assertTrue("Output text contains \"&lt;\"",
      outText.lastIndexOf("&lt;") >= 0);
}

代码示例来源:origin: dom4j/dom4j

public void testElementNamespaceAttributesWriteOpen() throws IOException {
  Document document = DocumentHelper.createDocument();
  Element root = document.addElement("rss")
      .addNamespace("g", "http://base.google.com/ns/1.0")
      .addNamespace("c", "http://base.google.com/cns/1.0");
  root.addAttribute("nons", "value");
  root.addAttribute(QName.get("g:ns"), "value");
  OutputFormat outputFormat = OutputFormat.createCompactFormat();
  outputFormat.setSuppressDeclaration(true);
  StringWriter stringWriter = new StringWriter();
  XMLWriter writer = new XMLWriter(stringWriter, outputFormat);
  writer.writeOpen(root);
  writer.close();
  Assert.assertEquals(stringWriter.toString(), "<rss xmlns:g=\"http://base.google.com/ns/1.0\" xmlns:c=\"http://base.google.com/cns/1.0\" nons=\"value\" g:ns=\"value\">");
}

相关文章