本文整理匯總了Java中com.sun.org.apache.xml.internal.serialize.OutputFormat.setIndenting方法的典型用法代碼示例。如果您正苦於以下問題:Java OutputFormat.setIndenting方法的具體用法?Java OutputFormat.setIndenting怎麽用?Java OutputFormat.setIndenting使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.org.apache.xml.internal.serialize.OutputFormat
的用法示例。
在下文中一共展示了OutputFormat.setIndenting方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: prettyPrint
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
private static String prettyPrint(String xml, Boolean omitXmlDeclaration) throws IOException, SAXException, ParserConfigurationException {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setIndent(2);
format.setOmitXMLDeclaration(omitXmlDeclaration);
format.setLineWidth(Integer.MAX_VALUE);
Writer outxml = new StringWriter();
XMLSerializer serializer = new XMLSerializer(outxml, format);
serializer.serialize(doc);
return outxml.toString();
}
示例2: formatXml
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
/**
* Format xml string.
*
* @param uglyXml the ugly xml
* @return the string
*/
public static String formatXml(String uglyXml) {
try {
String xmlHeader= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(uglyXml));
final Document document = db.parse(is);
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().replace(xmlHeader,"");
} catch (Exception e) {
return "";
}
}
示例3: pretify
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
public static String pretify(String unformattedXml) {
try {
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();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例4: formatXml
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
private String formatXml(String sourceXml) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(sourceXml));
Document d = db.parse(is);
// -- Formating XML ----------------------------------------------
OutputFormat format = new OutputFormat(d);
format.setIndenting(true);
format.setIndent(4);
Writer out = new StringWriter();
XMLSerializer s = new XMLSerializer(out, format);
s.serialize(d);
return out.toString();
} catch (ParserConfigurationException | SAXException | IOException e) {
logger.severe(e.getMessage());
}
return sourceXml;
}
示例5: printTo
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
@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();
}
示例6: XmlEditsVisitor
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
/**
* Create a processor that writes to the file named and may or may not
* also output to the screen, as specified.
*
* @param filename Name of file to write output to
* @param printToScreen Mirror output to screen?
*/
public XmlEditsVisitor(OutputStream out)
throws IOException {
this.out = out;
OutputFormat outFormat = new OutputFormat("XML", "UTF-8", true);
outFormat.setIndenting(true);
outFormat.setIndent(2);
outFormat.setDoctype(null, null);
XMLSerializer serializer = new XMLSerializer(out, outFormat);
contentHandler = serializer.asContentHandler();
try {
contentHandler.startDocument();
contentHandler.startElement("", "", "EDITS", new AttributesImpl());
} catch (SAXException e) {
throw new IOException("SAX error: " + e.getMessage());
}
}
示例7: openResultFile
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
private void openResultFile(File outputFile) throws SAXException
{
try
{
out = new FileOutputStream(outputFile);
OutputFormat of = new OutputFormat("XML", FILE_ENCODING, true);
of.setIndent(1);
of.setIndenting(true);
XMLSerializer serializer = new XMLSerializer(out, of);
writer = serializer.asContentHandler();
writer.startDocument();
startElement(XML_ROOT, NO_ATTRIBS);
}
catch (IOException io)
{
logger.error(ERROR_WRITE_RESULTS, io);
close();
}
}
示例8: getPrettyXml
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
public static String getPrettyXml(byte[] xml) {
try {
String unformattedXml = new String(xml);
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();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例9: formatXml
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
/**
* Format the given string as xml content.
* @param xml
* @return
*/
public static String formatXml(String xml) {
try {
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document doc = domBuilder.parse(is);
OutputFormat format = new OutputFormat(doc);
format.setLineWidth(80);
format.setIndent(2);
format.setIndenting(true);
StringWriter out = new StringWriter();
XMLSerializer xmls = new XMLSerializer(out, format);
xmls.serialize(doc);
return out.toString();
} catch (Exception e) {
e.printStackTrace();
}
return xml;
}
示例10: printTo
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
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();
}
示例11: printToFile
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
/**
* This method uses Xerces specific classes prints the XML document to file.
*/
private static void printToFile(Document dom)
{
try
{
// print
OutputFormat format = new OutputFormat(dom);
format.setIndenting(true);
// to generate output to console use this serializer
// XMLSerializer serializer = new XMLSerializer(System.out, format);
// to generate a file output use fileoutputstream instead of
// system.out
XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File("/Users/parambir.singh/Downloads/Test15/" + TEST_NAME + ".xml")), format);
serializer.serialize(dom);
}
catch (IOException ie)
{
ie.printStackTrace();
}
}
示例12: getXMLSerializer
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
/**
* 設置屬性
*
* @param os
* @return
*/
private XMLSerializer getXMLSerializer(OutputStream os) {
OutputFormat of = new OutputFormat();
formatCDataTag();
of.setCDataElements(cdataNode);
of.setPreserveSpace(true);
of.setIndenting(true);
of.setOmitXMLDeclaration(true);
XMLSerializer serializer = new XMLSerializer(of);
serializer.setOutputByteStream(os);
return serializer;
}
示例13: getPrettyPrintFormat
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
/**
* @return a format object for XML formatting.
*/
@NonNull
private static OutputFormat getPrettyPrintFormat() {
OutputFormat format = new OutputFormat();
format.setLineWidth(120);
format.setIndenting(true);
format.setIndent(4);
format.setEncoding("UTF-8");
format.setOmitComments(true);
return format;
}
示例14: getString
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
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");
}
示例15: saveToFile
import com.sun.org.apache.xml.internal.serialize.OutputFormat; //導入方法依賴的package包/類
/**
*
*/
private void saveToFile() throws FileNotFoundException, IOException{
FileOutputStream fos = new FileOutputStream(FAVOURITE_FILE);
OutputFormat of = new OutputFormat("XML","UTF-8",true);
of.setIndent(1);
of.setIndenting(true);
XMLSerializer serializer = new XMLSerializer(fos,of);
serializer.asDOMSerializer();
serializer.serialize(document.getDocumentElement());
fos.close();
}