本文整理汇总了Java中javax.xml.transform.Transformer.setOutputProperty方法的典型用法代码示例。如果您正苦于以下问题:Java Transformer.setOutputProperty方法的具体用法?Java Transformer.setOutputProperty怎么用?Java Transformer.setOutputProperty使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.Transformer
的用法示例。
在下文中一共展示了Transformer.setOutputProperty方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: format
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
@Override
public String format(String xml) {
String formattedString;
if (xml == null || xml.trim().length() == 0) {
throw new FormatException("XML empty.");
}
try {
Source xmlInput = new StreamSource(new StringReader(xml));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",
String.valueOf(XML_INDENT));
transformer.transform(xmlInput, xmlOutput);
formattedString = xmlOutput.getWriter().toString().replaceFirst(">", ">"
+ SystemCompat.lineSeparator);
} catch (Exception e) {
throw new FormatException("Parse XML error. XML string:" + xml, e);
}
return formattedString;
}
示例2: writeDocument
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Write an XML document to a Writer
*/
public static void writeDocument(Document doc, Writer writer)
throws IOException {
final Source source = new DOMSource(doc);
// Prepare the output file
final Result result = new StreamResult(writer);
// Write the DOM document to the file
try {
final Transformer xformer =
TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$
xformer.transform(source, result);
}
catch (TransformerException e) {
// FIXME: switch to IOException(Throwable) ctor in Java 1.6
throw (IOException) new IOException().initCause(e);
}
}
示例3: synchroGraphicalToXml
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public void synchroGraphicalToXml(){
Document doc=this.buildDocument();
if(doc==null)return;
TransformerFactory factory=TransformerFactory.newInstance();
try{
Transformer transformer=factory.newTransformer();
transformer.setOutputProperty("encoding","utf-8");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
ByteArrayOutputStream out = new ByteArrayOutputStream();
transformer.transform(new DOMSource(doc),new StreamResult(out));
xmlEditor.getDocumentProvider().getDocument(xmlEditor.getEditorInput()).set(out.toString("utf-8"));
out.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
示例4: toString
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
@Override
public String toString() {
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
return result.getWriter().toString();
} catch (TransformerException e) {
throw new RuntimeException(e);
}
}
示例5: printDocument
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public static void printDocument(Node node, OutputStream out) {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(
new DOMSource(node),
new StreamResult(new OutputStreamWriter(out, "UTF-8")));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例6: printDocument
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
private static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
StreamResult sr = new StreamResult(osw);
transformer.transform(new DOMSource(doc),
sr);
}
示例7: formatXML
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
private static String formatXML(String inputXML) {
try {
Source xmlInput = new StreamSource(new StringReader(inputXML));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
return xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
} catch (Exception e) {
e.printStackTrace();
return inputXML;
}
}
示例8: saveEmitter
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* Save a single emitter to the XML file
*
* @param out
* The location to which we should save
* @param emitter
* The emitter to store to the XML file
* @throws IOException
* Indicates a failure to write or encode the XML
*/
public static void saveEmitter(OutputStream out, ConfigurableEmitter emitter)
throws IOException {
try {
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document document = builder.newDocument();
document.appendChild(emitterToElement(document, emitter));
Result result = new StreamResult(new OutputStreamWriter(out,
"utf-8"));
DOMSource source = new DOMSource(document);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer xformer = factory.newTransformer();
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.transform(source, result);
} catch (Exception e) {
Log.error(e);
throw new IOException("Failed to save emitter");
}
}
示例9: setCommonOutputProperties
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public static void setCommonOutputProperties(final Transformer transformer, final boolean indentOutput)
throws TransformerConfigurationException {
transformer.setOutputProperty(OutputKeys.METHOD, XML);
transformer.setOutputProperty(OutputKeys.ENCODING, UTF_8);
transformer.setOutputProperty(OutputKeys.VERSION, VERSION);
if (indentOutput) {
transformer.setOutputProperty(OutputKeys.INDENT, YES);
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");
} else {
transformer.setOutputProperty(OutputKeys.INDENT, NO);
}
}
示例10: writeXml
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
/**
* @source http://stackoverflow.com/a/7373596
*/
public static void writeXml(Document dom, File xmlFile) throws Exception {
Transformer t = tf.newTransformer();
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.setOutputProperty(OutputKeys.METHOD, "xml");
t.setOutputProperty(OutputKeys.VERSION, "1.0");
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
t.transform(new DOMSource(dom), new StreamResult(new FileOutputStream(xmlFile)));
}
示例11: writeDocument
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public static void writeDocument(Document doc, OutputStream stream) throws TransformerException, IOException {
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer serializer;
serializer = tfactory.newTransformer();
//Setup indenting to "pretty print"
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
serializer.transform(new DOMSource(doc), new StreamResult(stream));
stream.close();
}
示例12: nodeToString
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
private static String nodeToString(Node node, StringWriter xmlString) throws Exception, RuntimeException {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.setOutputProperty(OutputKeys.INDENT, "no");
t.transform(new DOMSource(node), new StreamResult(xmlString));
return xmlString.toString();
}
示例13: nodeToText
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
private String nodeToText(Node node) throws TransformerException {
Transformer trans = TransformerFactory.newInstance().newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
trans.transform(new DOMSource(node), result);
String bodyContent = writer.toString();
System.out.println("SOAP body content read by SAAJ:"+bodyContent);
return bodyContent;
}
示例14: printXml
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
private static void printXml(String tag, String xml, String headString) {
if (TextUtils.isEmpty(tag)) {
tag = TAG;
}
if (xml != null) {
try {
Source xmlInput = new StreamSource(new StringReader(xml));
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer.transform(xmlInput, xmlOutput);
xml = xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
} catch (Exception e) {
e.printStackTrace();
}
xml = headString + "\n" + xml;
} else {
xml = headString + "Log with null object";
}
printLine(tag, true);
String[] lines = xml.split(LINE_SEPARATOR);
for (String line : lines) {
if (!TextUtils.isEmpty(line)) {
Log.d(tag, "|" + line);
}
}
printLine(tag, false);
}
示例15: writeXml
import javax.xml.transform.Transformer; //导入方法依赖的package包/类
public static void writeXml( Node n, OutputStream os )
throws TransformerException
{
TransformerFactory tf=TransformerFactory.newInstance();
//identity
Transformer t=tf.newTransformer();
t.setOutputProperty(OutputKeys.INDENT, "yes");
t.transform(new DOMSource( n ), new StreamResult( os ));
}