当前位置: 首页>>代码示例>>Java>>正文


Java OutputKeys类代码示例

本文整理汇总了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" );
	}
}
 
开发者ID:Paulpanther,项目名称:Random-Music-Generator,代码行数:26,代码来源:XMLGenerator.java

示例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!");

}
 
开发者ID:NeoRoy,项目名称:KeYExperienceReport,代码行数:21,代码来源:SPL2OpltionGenerator.java

示例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;
}
 
开发者ID:goutham106,项目名称:GmArchMvvm,代码行数:25,代码来源:CharacterHandler.java

示例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;
}
 
开发者ID:hgadre,项目名称:solr-upgrade-tool,代码行数:20,代码来源:ConfigTransformer.java

示例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;
}
 
开发者ID:FreeCol,项目名称:freecol,代码行数:29,代码来源:Utils.java

示例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);
  }
}
 
开发者ID:gurkenlabs,项目名称:litiengine,代码行数:26,代码来源:XmlUtilities.java

示例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);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:24,代码来源:XMLUtil.java

示例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;
}
 
开发者ID:1991wangliang,项目名称:pay,代码行数:29,代码来源:XmlUtils.java

示例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);
    }
}
 
开发者ID:VerifAPS,项目名称:stvs,代码行数:23,代码来源:XmlExporter.java

示例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();
}
 
开发者ID:equella,项目名称:Equella,代码行数:22,代码来源:RequestParameter.java

示例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;
}
 
开发者ID:MinBZK,项目名称:OperatieBRP,代码行数:24,代码来源:AbstractDienstVerzoekParser.java

示例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();
	}
}
 
开发者ID:dev-cuttlefish,项目名称:cuttlefish,代码行数:26,代码来源:GraphMLExporter.java

示例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;
}
 
开发者ID:Zweihui,项目名称:Aurora,代码行数:25,代码来源:CharacterHandler.java

示例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!");

}
 
开发者ID:NeoRoy,项目名称:KeYExperienceReport,代码行数:21,代码来源:ResultToSPL.java

示例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!");

}
 
开发者ID:NeoRoy,项目名称:KeYExperienceReport,代码行数:21,代码来源:FeatureIdeOpltionGenerator.java


注:本文中的javax.xml.transform.OutputKeys类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。