本文整理汇总了Java中javax.xml.transform.OutputKeys类的典型用法代码示例。如果您正苦于以下问题:Java OutputKeys类的具体用法?Java OutputKeys怎么用?Java OutputKeys使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
OutputKeys类属于javax.xml.transform包,在下文中一共展示了OutputKeys类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: saveDocumentTo
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
private void saveDocumentTo() throws XMLException {
try {
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transf = tfactory.newTransformer();
transf.setOutputProperty( OutputKeys.INDENT, "yes" );
transf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transf.setOutputProperty( OutputKeys.OMIT_XML_DECLARATION, "no" );
transf.setOutputProperty( OutputKeys.METHOD, "xml" );
DOMImplementation impl = doc.getImplementation();
DocumentType doctype = impl.createDocumentType( "doctype",
"-//Recordare//DTD MusicXML 3.0 Partwise//EN", "http://www.musicxml.org/dtds/partwise.dtd" );
transf.setOutputProperty( OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId() );
transf.setOutputProperty( OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId() );
DOMSource src = new DOMSource( doc );
StreamResult resultS = new StreamResult( file );
transf.transform( src, resultS );
} catch ( TransformerException e ) {
throw new XMLException( "Could not save the File" );
}
}
示例2: writeOut
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
private static void writeOut(Document doc) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
DOMSource source = new DOMSource(doc);
File f = new File("splFile.xml");
f.delete();
StreamResult result = new StreamResult(f);
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
System.out.println("File saved!");
}
示例3: xmlFormat
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
/**
* xml format
*
* @param xml The xml string to be formatted
* @return formatted after the new string
*/
public static String xmlFormat(String xml) {
if (TextUtils.isEmpty(xml)) {
return "Empty/Null xml content";
}
String message;
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);
message = xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
} catch (TransformerException e) {
message = xml;
}
return message;
}
示例4: buildTransformer
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
protected Transformer buildTransformer(ToolParams params, ProcessorConfig procConfig)
throws TransformerConfigurationException {
Transformer result = params.getFactory().newTransformer(); // identity transform.
if (procConfig.getTransformerPath() != null) {
Path scriptPath = Paths.get(procConfig.getTransformerPath());
if (!scriptPath.isAbsolute()) {
scriptPath = params.getProcessorConfPath().getParent().resolve(scriptPath);
}
if (!Files.exists(scriptPath)) {
throw new IllegalArgumentException("Unable to find transformation script "+scriptPath);
}
result = params.getFactory().newTransformer(new StreamSource(scriptPath.toFile()));
}
result.setOutputProperty(OutputKeys.INDENT, "yes");
result.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
return result;
}
示例5: makeTransformer
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
/**
* Helper to make an XML Transformer.
*
* @param declaration If true, include the XML declaration.
* @param indent If true, set up the transformer to indent.
* @return A suitable {@code Transformer}.
*/
public static Transformer makeTransformer(boolean declaration,
boolean indent) {
Transformer tf = null;
try {
TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute("indent-number", new Integer(2));
tf = factory.newTransformer();
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.METHOD, "xml");
if (!declaration) {
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
}
if (indent) {
tf.setOutputProperty(OutputKeys.INDENT, "yes");
tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
}
} catch (TransformerException e) {
logger.log(Level.WARNING, "Failed to install transformer!", e);
}
return tf;
}
示例6: saveWithCustomIndetation
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
/**
* Saves the xml, contained by the specified input with the custom indentation.
* If the input is the result of jaxb marshalling, make sure to set
* Marshaller.JAXB_FORMATTED_OUTPUT to false in order for this method to work
* properly.
*
* @param input
* @param fos
* @param indentation
*/
public static void saveWithCustomIndetation(ByteArrayInputStream input, FileOutputStream fos, int indentation) {
try {
Transformer transformer = SAXTransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentation));
Source xmlSource = new SAXSource(new org.xml.sax.InputSource(input));
StreamResult res = new StreamResult(fos);
transformer.transform(xmlSource, res);
fos.flush();
fos.close();
} catch (TransformerFactoryConfigurationError | TransformerException | IOException e) {
log.log(Level.SEVERE, e.getMessage(), e);
}
}
示例7: write
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
public static void write(Document doc, OutputStream out) throws IOException {
// XXX note that this may fail to write out namespaces correctly if the document
// is created with namespaces and no explicit prefixes; however no code in
// this package is likely to be doing so
try {
Transformer t = TransformerFactory.newInstance().newTransformer(
new StreamSource(new StringReader(IDENTITY_XSLT_WITH_INDENT)));
DocumentType dt = doc.getDoctype();
if (dt != null) {
String pub = dt.getPublicId();
if (pub != null) {
t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, pub);
}
t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dt.getSystemId());
}
t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // NOI18N
Source source = new DOMSource(doc);
Result result = new StreamResult(out);
t.transform(source, result);
} catch (Exception | TransformerFactoryConfigurationError e) {
throw new IOException(e);
}
}
示例8: nodeToString
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
/**
* Converts the Node/Document/Element instance to XML payload.
*
* @param node the node/document/element instance to convert
* @return the XML payload representing the node/document/element
* @throws AlipayApiException problem converting XML to string
*/
public static String nodeToString(Node node) throws AlipayApiException {
String payload = null;
try {
Transformer tf = TransformerFactory.newInstance().newTransformer();
Properties props = tf.getOutputProperties();
props.setProperty(OutputKeys.INDENT, LOGIC_YES);
props.setProperty(OutputKeys.ENCODING, DEFAULT_ENCODE);
tf.setOutputProperties(props);
StringWriter writer = new StringWriter();
tf.transform(new DOMSource(node), new StreamResult(writer));
payload = writer.toString();
payload = payload.replaceAll(REG_INVALID_CHARS, " ");
} catch (TransformerException e) {
throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
}
return payload;
}
示例9: export
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
/**
* Exports an Object as xml.
*
* @param source Object to export
* @return The output xml is written to this stream
* @throws ExportException Exception while exporting
*/
public ByteArrayOutputStream export(F source) throws ExportException {
Node xmlNode = exportToXmlNode(source);
StringWriter writer = new StringWriter();
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(xmlNode), new StreamResult(writer));
String xmlString = writer.toString();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
stream.write(xmlString.getBytes("utf-8"));
return stream;
} catch (TransformerException | IOException e) {
throw new ExportException(e);
}
}
示例10: element2String
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
private static String element2String(Element element)
{
StringWriter result = new StringWriter();
DOMSource source = new DOMSource(element);
StreamResult destination = new StreamResult(result);
TransformerFactory factory = TransformerFactory.newInstance();
try
{
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(source, destination);
}
catch( TransformerException ex )
{
throw new RuntimeException("Error transforming element to xml string", ex);
}
return result.toString();
}
示例11: getXmlFragment
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
/**
* Hulpmethode om een xml fragment uit een node te halen middels xpath.
* @param locatie de locatie van de node als xpath.
* @param xPath een XPath instantie
* @param node de basis node
* @return de text
*/
protected static String getXmlFragment(final String locatie, final XPath xPath, final Node node) {
try {
final Node xPathNode = (Node) xPath.evaluate(locatie, node, XPathConstants.NODE);
if (xPathNode != null) {
final StringWriter buf = new StringWriter();
final Transformer xform = TransformerFactory.newInstance().newTransformer();
xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
xform.transform(new DOMSource(xPathNode), new StreamResult(buf));
return buf.toString();
}
} catch (final XPathExpressionException | TransformerException e) {
LOGGER.error("XPath voor text content kon niet worden geëvalueerd voor locatie {}.", locatie);
throw new UnsupportedOperationException(e);
}
return null;
}
示例12: export
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
public void export(File file) {
try {
// Create the GraphML Document
docFactory = DocumentBuilderFactory.newInstance();
docBuilder = docFactory.newDocumentBuilder();
doc = docBuilder.newDocument();
// Export the data
exportData();
// Save data as GraphML file
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(file);
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(source, result);
} catch (TransformerException te) {
te.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
}
示例13: xmlFormat
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
/**
* xml 格式化
*
* @param xml
* @return
*/
public static String xmlFormat(String xml) {
if (TextUtils.isEmpty(xml)) {
return "Empty/Null xml content";
}
String message;
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);
message = xmlOutput.getWriter().toString().replaceFirst(">", ">\n");
} catch (TransformerException e) {
message = xml;
}
return message;
}
示例14: writeOut
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
private static void writeOut(Document doc, File f)
throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
DOMSource source = new DOMSource(doc);
f.delete();
StreamResult result = new StreamResult(f);
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
System.out.println("File saved!");
}
示例15: writeOut
import javax.xml.transform.OutputKeys; //导入依赖的package包/类
private static void writeOut(Document doc) throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.STANDALONE, "no");
DOMSource source = new DOMSource(doc);
File f = new File("file.xml");
f.delete();
StreamResult result = new StreamResult(f);
// Output to console for testing
// StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
System.out.println("File saved!");
}