private static OutputFormat getConsoleWithDocType() { OutputFormat fmt = new OutputFormat("XML", "UTF-8", true); fmt.setIndent(2); fmt.setLineWidth(80); fmt.setPreserveSpace(false); // default is false fmt.setOmitXMLDeclaration(false); // default is false fmt.setOmitDocumentType(false); return fmt; }
private static OutputFormat getPrettyPrint() { OutputFormat fmt = new OutputFormat("XML", "UTF-8", true); fmt.setEncoding("UTF-8"); fmt.setIndenting(true); fmt.setIndent(2); fmt.setOmitXMLDeclaration(true); return fmt; }
private static OutputFormat getXmlNoSpace(String encoding) { OutputFormat fmt = new OutputFormat("XML", encoding, false); // indent == 0 means add no indenting fmt.setIndent(0); // default line width is 72, but only applies when indenting fmt.setLineWidth(0); fmt.setPreserveSpace(false); return fmt; }
/** * Constructs a new serializer. The serializer cannot be used without * calling {@link #setOutputCharStream} or {@link #setOutputByteStream} * first. */ public XHTMLSerializer( OutputFormat format ) { super( true, format != null ? format : new OutputFormat( Method.XHTML, null, false ) ); }
public String format(String unformattedXml) throws IOException, ParserConfigurationException, SAXException { final Document document = parseXmlFile(unformattedXml); OutputFormat format = new OutputFormat(document); format.setLineWidth(65); format.setIndenting(true); format.setIndent(2); Writer out = new StringWriter(); XMLSerializer serializer = new XMLSerializer(out, format); serializer.serialize(document); return out.toString(); }
public String getString(Document document, boolean indenting, int indent) throws IOException{ OutputFormat format = new OutputFormat(document); format.setLineWidth(200); format.setIndenting(indenting); format.setIndent(indent); format.setPreserveEmptyAttributes(true); format.setEncoding("UTF-8"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLSerializer serializer = new XMLSerializer(baos, format); serializer.asDOMSerializer(); serializer.serialize(document); return baos.toString("UTF-8"); }
/** * Constructs a new serializer. The serializer cannot be used without * calling {@link #setOutputCharStream} or {@link #setOutputByteStream} * first. */ public XMLSerializer( OutputFormat format ) { super( format != null ? format : new OutputFormat( Method.XML, null, false ) ); _format.setMethod( Method.XML ); }
BufferedReader in = new BufferedReader(new FileReader(fromFile)); FileOutputStream fos = new FileOutputStream(toFile); OutputFormat of = new OutputFormat("XML","windows-1250",true); of.setIndent(1); of.setIndenting(true); XMLSerializer serializer = new XMLSerializer(fos,of); ContentHandler hd = serializer.asContentHandler();
@Override public void printTo(Writer out) throws IOException { if (dom == null) { return; } StringWriter sw = new StringWriter(); OutputFormat format = new OutputFormat(dom); format.setIndenting(true); XMLSerializer serializer = new XMLSerializer(sw, format); serializer.serialize(dom); out.write(sw.toString()); out.flush(); }
/** * Constructs a new serializer that writes to the specified output * stream using the specified output format. If <tt>format</tt> * is null, will use a default output format. * * @param output The output stream to use * @param format The output format to use, null for the default */ public XML11Serializer( OutputStream output, OutputFormat format ) { super( output, format != null ? format : new OutputFormat( Method.XML, null, false ) ); _format.setVersion("1.1"); }
@Override public String generateOutput(DocumentRecord documentRecord) { if (Format.XML.equals(documentRecord.getFormat())) { DOMHandle handle = documentRecord.getContent(new DOMHandle()); Document document = handle.get(); OutputFormat format = new OutputFormat(handle.get()); format.setOmitXMLDeclaration(omitXmlDeclaration); StringWriter writer = new StringWriter(); try { new XMLSerializer(writer, format).serialize(document); return writer.toString(); } catch (IOException e) { throw new RuntimeException("Unable to serialize XML document to string: " + e.getMessage(), e); } } else if (logger.isDebugEnabled()) { logger.debug(String.format("Document '%s' has a format of '%s', so will not attempt to remove the XML declaration from it", documentRecord.getUri(), documentRecord.getFormat().name())); } return documentRecord.getContent(new StringHandle()).get(); }
/** * Constructs a new output format with the default values for * the specified method and encoding. If <tt>indent</tt> * is true, the document will be pretty printed with the default * indentation level and default line wrapping. * * @param method The specified output method * @param encoding The specified encoding * @param indenting True for pretty printing * @see #setEncoding * @see #setIndenting * @see #setMethod */ public OutputFormat( String method, String encoding, boolean indenting ) { setMethod( method ); setEncoding( encoding ); setIndenting( indenting ); }
ser.fNamespaces = (features & NAMESPACES) != 0; ser.fNamespacePrefixes = (features & NSDECL) != 0; ser._format.setOmitComments((features & COMMENTS)==0); ser._format.setOmitXMLDeclaration((features & XMLDECL) == 0); ser._format.setIndenting((features & FORMAT_PRETTY_PRINT) != 0);
OutputFormat format = new OutputFormat (); format.setPreserveSpace (true); StringWriter sw = new StringWriter(); org.w3c.dom.Element rootElement = doc.getDocumentElement();
/** * Constructs a new output format with the proper method, * document type identifiers and media type for the specified * document, and with the specified encoding. If <tt>indent</tt> * is true, the document will be pretty printed with the default * indentation level and default line wrapping. * * @param doc The document to output * @param encoding The specified encoding * @param indenting True for pretty printing * @see #setEncoding * @see #setIndenting * @see #whichMethod */ public OutputFormat( Document doc, String encoding, boolean indenting ) { this( doc ); setEncoding( encoding ); setIndenting( indenting ); }
private void copySettings(XMLSerializer src, XMLSerializer dest) { dest.fDOMErrorHandler = fErrorHandler; dest._format.setEncoding(src._format.getEncoding()); dest._format.setLineSeparator(src._format.getLineSeparator()); dest.fDOMFilter = src.fDOMFilter; }//copysettings
ser._format.setEncoding(encoding);
/** * Constructs a new serializer. The serializer cannot be used without * calling {@link #setOutputCharStream} or {@link #setOutputByteStream} * first. */ public HTMLSerializer() { this( false, new OutputFormat( Method.HTML, "ISO-8859-1", false ) ); }
/** * Dumps the SOAPMessage to String * * @param msg the SOAP message * @return the String representation of the message * @throws IOException IO issue * @throws SOAPException SOAP issue */ public String dumpSoapMessage(SOAPMessage msg) throws IOException, SOAPException { Document doc = msg.getSOAPBody().getOwnerDocument(); OutputFormat format = new OutputFormat(doc); format.setLineWidth(65); format.setIndenting(true); format.setIndent(2); StringWriter s = new StringWriter(); XMLSerializer serializer = new XMLSerializer(s, format); serializer.serialize(doc); return s.toString(); }
private static OutputFormat getXmlNoSpace(String encoding) { OutputFormat fmt = new OutputFormat("XML", encoding, false); // indent == 0 means add no indenting fmt.setIndent(0); // default line width is 72, but only applies when indenting fmt.setLineWidth(0); fmt.setPreserveSpace(false); return fmt; }