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

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

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

OutputFormat.setSuppressDeclaration介绍

[英]This will set whether the XML declaration (<?xml version="1.0" encoding="UTF-8"?>) is included or not. It is common to suppress this in protocols such as WML and SOAP.
[中]这将设置是否包含XML声明(<?xml version="1.0" encoding="UTF-8"?>)。在WML和SOAP等协议中,抑制这种情况很常见。

代码示例

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

for (int size = args.length; i < size; i++) {
  if (args[i].equals("-suppressDeclaration")) {
    setSuppressDeclaration(true);
  } else if (args[i].equals("-omitEncoding")) {
    setOmitEncoding(true);

代码示例来源:origin: org.mule.modules/mule-module-xml

/**
 * @see OutputFormat#getEncoding()
 */
public synchronized void setSuppressDeclaration(boolean suppressDeclaration)
{
  outputFormat.setSuppressDeclaration(suppressDeclaration);
}

代码示例来源:origin: stackoverflow.com

public static String prettyPrintXml(String xml) {
   final StringWriter sw;
   try {
     final OutputFormat format = OutputFormat.createPrettyPrint();
     format.setSuppressDeclaration(true);
     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);
   }
   return sw.toString();
 }

代码示例来源:origin: org.nuxeo.ecm.core/nuxeo-core-io

protected XMLWriter initWriter() {
  if (writer == null) {
    try {
      OutputFormat format = AbstractDocumentWriter.createCompactFormat();
      format.setSuppressDeclaration(true);
      writer = new XMLWriter(out, format);
    } catch (UnsupportedEncodingException e) {
      // XXX
    }
  }
  return writer;
}

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

/**
 *
 * @param xml
 * @param format
 * @return
 * @throws Exception
 */
private static String print( final String xml, final OutputFormat format ) throws Exception {
 format.setSuppressDeclaration( true );
 final org.dom4j.Document document = DocumentHelper.parseText( xml );
 StringWriter sw = new StringWriter();
 final XMLWriter writer = new XMLWriter( sw, format );
 writer.write( document );
 return sw.toString();
}

代码示例来源: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: com.github.developframework/kite-core

@Override
public void outputXml(Writer writer, DataModel dataModel, String namespace, String templateId, boolean isPretty) {
  Document document = constructDocument(dataModel, namespace, templateId);
  try {
    OutputFormat format = new OutputFormat();
    format.setIndent(isPretty);
    format.setNewlines(isPretty);
    format.setSuppressDeclaration(kiteConfiguration.isXmlSuppressDeclaration());
    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.write(document);
    xmlWriter.close();
  } catch (IOException e) {
    throw new KiteException("produce xml string failed.");
  }
}

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

private void testGitHubIssue26(final Element element) throws IOException {
  final OutputFormat format = new OutputFormat("    ", true);
  format.setSuppressDeclaration(false);
  format.setTrimText(true);
  format.setPadText(true);
  format.setNewlines(true);
  new XMLWriter(new CharArrayWriter(128), format).write(element);
}

代码示例来源:origin: org.springframework.extensions.surf/spring-webscripts

public void createDocuments(List<Pair<String, Document>> pathContents) throws IOException
{
  for (Pair<String, Document> pathContent : pathContents)
  {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setSuppressDeclaration(false);
    
    StringBuilderWriter writer = new StringBuilderWriter(1024);
    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.write(pathContent.getSecond());
    xmlWriter.flush();
    writer.close();
    createDocument(pathContent.getFirst(), writer.toString());
  }
}

代码示例来源:origin: org.alfresco.surf/spring-webscripts

public void createDocuments(List<Pair<String, Document>> pathContents) throws IOException
{
  for (Pair<String, Document> pathContent : pathContents)
  {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setSuppressDeclaration(false);
    
    StringBuilderWriter writer = new StringBuilderWriter(1024);
    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.write(pathContent.getSecond());
    xmlWriter.flush();
    writer.close();
    createDocument(pathContent.getFirst(), writer.toString());
  }
}

代码示例来源:origin: deas/alfresco

public void createDocuments(List<Pair<String, Document>> pathContents) throws IOException
{
  for (Pair<String, Document> pathContent : pathContents)
  {
    OutputFormat format = OutputFormat.createPrettyPrint();
    format.setSuppressDeclaration(false);
    
    StringBuilderWriter writer = new StringBuilderWriter(1024);
    XMLWriter xmlWriter = new XMLWriter(writer, format);
    xmlWriter.write(pathContent.getSecond());
    xmlWriter.flush();
    writer.close();
    createDocument(pathContent.getFirst(), writer.toString());
  }
}

代码示例来源: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 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 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 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 testEscapeXML() throws Exception {
  ByteArrayOutputStream os = new ByteArrayOutputStream();
  OutputFormat format = new OutputFormat(null, false, "ISO-8859-2");
  format.setSuppressDeclaration(true);
  XMLWriter writer = new XMLWriter(os, format);
  Document document = DocumentFactory.getInstance().createDocument();
  Element root = document.addElement("root");
  root.setText("bla &#c bla");
  writer.write(document);
  String result = os.toString();
  System.out.println(result);
  Document doc2 = DocumentHelper.parseText(result);
  doc2.normalize(); // merges adjacant Text nodes
  System.out.println(doc2.getRootElement().getText());
  assertNodesEqual(document, doc2);
}

代码示例来源: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 testGitHubIssue26_case7() throws IOException {
  final Element element = new DOMElement("foo");
  element.add(new DOMText(""));
  element.add(new DOMElement("elem"));
  final OutputFormat format = new OutputFormat("    ", true);
  format.setSuppressDeclaration(false);
  format.setTrimText(false);
  format.setPadText(true);
  format.setNewlines(true);
  new XMLWriter(new CharArrayWriter(128), format).write(element);
}

代码示例来源: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\">");
}

相关文章