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


Java Transformer类代码示例

本文整理汇总了Java中javax.xml.transform.Transformer的典型用法代码示例。如果您正苦于以下问题:Java Transformer类的具体用法?Java Transformer怎么用?Java Transformer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Transformer类属于javax.xml.transform包,在下文中一共展示了Transformer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: saveDocumentTo

import javax.xml.transform.Transformer; //导入依赖的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: doTransform

import javax.xml.transform.Transformer; //导入依赖的package包/类
/**
 * Performs the XSLT transformation.
 */
private String doTransform(final Transformer transformer, final Source input, final URIResolver resolver)
{
	final StringWriter writer = new StringWriter();
	final StreamResult output = new StreamResult(writer);

	if( resolver != null )
	{
		transformer.setURIResolver(resolver);
	}

	try
	{
		transformer.transform(input, output);
	}
	catch( final TransformerException ex )
	{
		throw new RuntimeException("Error transforming XSLT", ex);
	}

	return writer.toString();
}
 
开发者ID:equella,项目名称:Equella,代码行数:25,代码来源:XsltServiceImpl.java

示例3: export

import javax.xml.transform.Transformer; //导入依赖的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

示例4: printXmlFile

import javax.xml.transform.Transformer; //导入依赖的package包/类
/**
 * Outputs the given XML {@link Document} to the file {@code outFile}.
 *
 * TODO right now reformats the document. Needs to output as-is, respecting white-space.
 *
 * @param doc The document to output. Must not be null.
 * @param outFile The {@link File} where to write the document.
 * @param log A log in case of error.
 * @return True if the file was written, false in case of error.
 */
static boolean printXmlFile(
        @NonNull Document doc,
        @NonNull File outFile,
        @NonNull IMergerLog log) {
    // Quick thing based on comments from http://stackoverflow.com/questions/139076
    try {
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");         //$NON-NLS-1$
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");                   //$NON-NLS-1$
        tf.setOutputProperty(OutputKeys.INDENT, "yes");                       //$NON-NLS-1$
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount",     //$NON-NLS-1$
                             "4");                                            //$NON-NLS-1$
        tf.transform(new DOMSource(doc), new StreamResult(outFile));
        return true;
    } catch (TransformerException e) {
        log.error(Severity.ERROR,
                new FileAndLine(outFile.getName(), 0),
                "Failed to write XML file: %1$s",
                e.toString());
        return false;
    }
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:33,代码来源:MergerXmlUtils.java

示例5: docResolver01

import javax.xml.transform.Transformer; //导入依赖的package包/类
/**
 * This is to test the URIResolver.resolve() method when there is an error
 * in the file.
 *
 * @throws Exception If any errors occur.
 */
@Test
public static void docResolver01() throws Exception {
    try (FileInputStream fis = new FileInputStream(XML_DIR + "doctest.xsl")) {
        URIResolverTest resolver = new URIResolverTest("temp/colors.xml", SYSTEM_ID);
        StreamSource streamSource = new StreamSource(fis);
        streamSource.setSystemId(SYSTEM_ID);

        Transformer transformer = TransformerFactory.newInstance().newTransformer(streamSource);
        transformer.setURIResolver(resolver);

        File f = new File(XML_DIR + "myFake.xml");
        Document document = DocumentBuilderFactory.newInstance().
                newDocumentBuilder().parse(f);

        // Use a Transformer for output
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(System.err);
        // No exception is expected because resolver resolve wrong URI.
        transformer.transform(source, result);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:28,代码来源:URIResolverTest.java

示例6: MexEntityResolver

import javax.xml.transform.Transformer; //导入依赖的package包/类
public MexEntityResolver(List<? extends Source> wsdls) throws IOException {
    Transformer transformer = XmlUtil.newTransformer();
    for (Source source : wsdls) {
        XMLStreamBufferResult xsbr = new XMLStreamBufferResult();
        try {
            transformer.transform(source, xsbr);
        } catch (TransformerException e) {
            throw new WebServiceException(e);
        }
        String systemId = source.getSystemId();

        //TODO: can we do anything if the given mex Source has no systemId?
        if(systemId != null){
            SDDocumentSource doc = SDDocumentSource.create(JAXWSUtils.getFileOrURL(systemId), xsbr.getXMLStreamBuffer());
            this.wsdls.put(systemId, doc);
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:MexEntityResolver.java

示例7: convertToString

import javax.xml.transform.Transformer; //导入依赖的package包/类
public static String convertToString(Source source,
        boolean includeXmlDeclaration) throws TransformerException {

    Transformer transformer = Transformers.newFormatingTransformer();
    if (!includeXmlDeclaration) {
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                "yes");
    }

    StringWriter stringWriter = new StringWriter();
    try {
        transformer.transform(source, new StreamResult(stringWriter));
        return stringWriter.toString();
    } finally {
        try {
            stringWriter.close();
        } catch (IOException e) {
            // ignore
        }
    }
}
 
开发者ID:servicecatalog,项目名称:oscm,代码行数:22,代码来源:XMLConverter.java

示例8: serializeList

import javax.xml.transform.Transformer; //导入依赖的package包/类
public <T> void serializeList(List<T> listToConvert, boolean noFormatting,
        Writer writer) throws TransformerException, IOException {
    Document xml;
    try {
        xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException ex) {
        throw new IOException(ex);
    }
    
    Element root = xml.createElement(rootTagName);
    xml.appendChild(root);
    
    for (T listElement : listToConvert) {
        Element newElement = xml.createElement(elementTagName);
        newElement.setTextContent(listElement.toString());
        root.appendChild(newElement);
    }
    
    Transformer tf =  TransformerFactory.newInstance().newTransformer();
    if (!noFormatting) {
        tf.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); //$NON-NLS-1$ //$NON-NLS-2$
    }
    tf.transform(new DOMSource(xml), new StreamResult(writer));
}
 
开发者ID:pgcodekeeper,项目名称:pgcodekeeper,代码行数:26,代码来源:XmlStringList.java

示例9: doTransform

import javax.xml.transform.Transformer; //导入依赖的package包/类
private void doTransform(String sXSL, OutputStream os) throws javax.xml.transform.TransformerConfigurationException, javax.xml.transform.TransformerException {
    // create the transformerfactory & transformer instance
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();

    if (null == sXSL) {
        t.transform(new DOMSource(doc), new StreamResult(os));
        return;
    }

    try {
        Transformer finalTransformer = tf.newTransformer(new StreamSource(sXSL));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(baos);
        t.transform(new DOMSource(doc), new StreamResult(bos));
        bos.flush();
        StreamSource xmlSource = new StreamSource(new BufferedInputStream(new ByteArrayInputStream(baos.toByteArray())));
        finalTransformer.transform(xmlSource, new StreamResult(os));
    } catch (IOException ioe) {
        throw new javax.xml.transform.TransformerException(ioe);
    }
}
 
开发者ID:dmitrykolesnikovich,项目名称:featurea,代码行数:23,代码来源:XMLOutputVisitor.java

示例10: colorizeXML

import javax.xml.transform.Transformer; //导入依赖的package包/类
String colorizeXML(String xml, String xsltFilename) throws TransformerConfigurationException, TransformerException {

       // Get the XSLT file as a resource
       InputStream xslt = getClass().getResourceAsStream(xsltFilename);
       
       // Create and configure XSLT Transformer 
       TransformerFactory factory = TransformerFactory.newInstance();
       Transformer transformer = factory.newTransformer(new StreamSource(xslt));
       transformer.setParameter("indent-elements", "yes");
       
       OutputStream outputStream = new ByteArrayOutputStream();
       InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
       
       // Convert the XML into HTML per the XSLT file
       transformer.transform(new StreamSource(inputStream), new StreamResult(outputStream));
       
       return new String(((ByteArrayOutputStream)outputStream).toByteArray());
   }
 
开发者ID:will-gilbert,项目名称:OSWf-OSWorkflow-fork,代码行数:19,代码来源:WfDefinitionServiceImpl.java

示例11: test

import javax.xml.transform.Transformer; //导入依赖的package包/类
@Test
public void test() throws Exception {
    SAXParserFactory fac = SAXParserFactory.newInstance();
    fac.setNamespaceAware(true);
    SAXParser saxParser = fac.newSAXParser();

    StreamSource sr = new StreamSource(this.getClass().getResourceAsStream("SAX2DOMTest.xml"));
    InputSource is = SAXSource.sourceToInputSource(sr);
    RejectDoctypeSaxFilter rf = new RejectDoctypeSaxFilter(saxParser);
    SAXSource src = new SAXSource(rf, is);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    DOMResult result = new DOMResult();
    transformer.transform(src, result);

    Document doc = (Document) result.getNode();
    System.out.println("Name" + doc.getDocumentElement().getLocalName());

    String id = "XWSSGID-11605791027261938254268";
    Element selement = doc.getElementById(id);
    if (selement == null) {
        System.out.println("getElementById returned null");
    }

}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:SAX2DOMTest.java

示例12: nodeToString

import javax.xml.transform.Transformer; //导入依赖的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 XmlException problem converting XML to string 
 */  
public static String nodeToString(Node node) throws XmlException {  
    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 XmlException(XmlException.XML_TRANSFORM_ERROR, e);  
    }  
  
    return payload;  
}
 
开发者ID:m18507308080,项目名称:bohemia,代码行数:29,代码来源:XmlUtils.java

示例13: getNcsItemStream

import javax.xml.transform.Transformer; //导入依赖的package包/类
/**
 *  Gets the ncsItemStream attribute of the CollectionAdopter object
 *
 * @param  nsdl_dc_stream  NOT YET DOCUMENTED
 * @return                 The ncsItemStream value
 * @exception  Exception   NOT YET DOCUMENTED
 */
public Element getNcsItemStream(Element nsdl_dc_stream) throws Exception {
	// return XSLTransformer.transformString (nsdl_dc, this.xslPath);
	String className = "net.sf.saxon.TransformerFactoryImpl";
	if (nsdl_dc_stream == null)
		return null;
	String nsdl_dc = nsdl_dc_stream.asXML();
	try {
		Transformer transformer =
			XSLTransformer.getTransformer(this.xslPath, className);
		String xml = XSLTransformer.transformString(nsdl_dc, transformer);
		if (xml != null && xml.trim().length() > 0)
			return DocumentHelper.parseText(xml).getRootElement();
	} catch (Throwable t) {
		prtln("transform problem: " + t.getMessage());
	}
	return null;
}
 
开发者ID:NCAR,项目名称:joai-project,代码行数:25,代码来源:CollectionAdopter.java

示例14: printXmlDoc

import javax.xml.transform.Transformer; //导入依赖的package包/类
public String printXmlDoc(Document doc) throws Exception {
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);

    TransformerFactory transformerFact = TransformerFactory.newInstance();
    transformerFact.setAttribute("indent-number", new Integer(4));
    Transformer transformer;

    transformer = transformerFact.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml");

    transformer.transform(new DOMSource(doc), result);
    return sw.toString();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:Bug6794483Test.java

示例15: writeOut

import javax.xml.transform.Transformer; //导入依赖的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,代码来源:SPLOpltionGenerator.java


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