Java Code Examples for javax.xml.transform.Transformer.setOutputProperties()

The following are Jave code examples for showing how to use setOutputProperties() of the javax.xml.transform.Transformer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: framework   File: XmlUtils.java   View Source Code Vote up 6 votes
/**
 * Converts the Node/Element instance to XML payload.
 *
 * @param node the node/element instance to convert
 * @return the XML payload representing the node/element
 * @throws AlipayApiException problem converting XML to string
 */
public static String childNodeToString(Node node) throws AlipayApiException {
	String payload = null;

	try {
		Transformer tf = TransformerFactory.newInstance().newTransformer();

		Properties props = tf.getOutputProperties();
		props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
		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;
}
 
Example 2
Project: framework   File: XmlUtils.java   View Source Code Vote up 6 votes
/**
 * 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;
}
 
Example 3
Project: framework   File: XmlUtils.java   View Source Code Vote up 6 votes
/**
 * Transforms the XML content to XHTML/HTML format string with the XSL.
 *
 * @param payload  the XML payload to convert
 * @param xsltFile the XML stylesheet file
 * @return the transformed XHTML/HTML format string
 * @throws AlipayApiException problem converting XML to HTML
 */
public static String xmlToHtml(String payload, File xsltFile) throws AlipayApiException {
	String result = null;

	try {
		Source template = new StreamSource(xsltFile);
		Transformer transformer = TransformerFactory.newInstance().newTransformer(template);

		Properties props = transformer.getOutputProperties();
		props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
		transformer.setOutputProperties(props);

		StreamSource source = new StreamSource(new StringReader(payload));
		StreamResult sr = new StreamResult(new StringWriter());
		transformer.transform(source, sr);

		result = sr.getWriter().toString();
	} catch (TransformerException e) {
		throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
	}

	return result;
}
 
Example 4
Project: bohemia   File: XmlUtils.java   View Source Code Vote up 6 votes
/** 
 * Converts the Node/Element instance to XML payload. 
 * 
 * @param node the node/element instance to convert 
 * @return the XML payload representing the node/element 
 * @throws XmlException problem converting XML to string 
 */  
public static String childNodeToString(Node node) throws XmlException {  
    String payload = null;  
  
    try {  
        Transformer tf = TransformerFactory.newInstance().newTransformer();  
  
        Properties props = tf.getOutputProperties();  
        props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);  
        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;  
}
 
Example 5
Project: bohemia   File: XmlUtils.java   View Source Code Vote up 6 votes
/** 
 * 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;  
}
 
Example 6
Project: bohemia   File: XmlUtils.java   View Source Code Vote up 6 votes
/** 
 * Transforms the XML content to XHTML/HTML format string with the XSL. 
 * 
 * @param payload the XML payload to convert 
 * @param xsltFile the XML stylesheet file 
 * @return the transformed XHTML/HTML format string 
 * @throws XmlException problem converting XML to HTML 
 */  
public static String xmlToHtml(String payload, File xsltFile)  
        throws XmlException {  
    String result = null;  
  
    try {  
        Source template = new StreamSource(xsltFile);  
        Transformer transformer = TransformerFactory.newInstance()  
                .newTransformer(template);  
  
        Properties props = transformer.getOutputProperties();  
        props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);  
        transformer.setOutputProperties(props);  
  
        StreamSource source = new StreamSource(new StringReader(payload));  
        StreamResult sr = new StreamResult(new StringWriter());  
        transformer.transform(source, sr);  
  
        result = sr.getWriter().toString();  
    } catch (TransformerException e) {  
        throw new XmlException(XmlException.XML_TRANSFORM_ERROR, e);  
    }  
  
    return result;  
}
 
Example 7
Project: DAFramework   File: DomXmlUtils.java   View Source Code Vote up 6 votes
/**
 * 获取一个Transformer对象,由于使用时都做相同的初始化,所以提取出来作为公共方法。
 *
 * @return a Transformer encoding gb2312
 */

public static Transformer newTransformer() {
	try {
		Transformer transformer = TransformerFactory.newInstance()
				.newTransformer();
		Properties properties = transformer.getOutputProperties();
		properties.setProperty(OutputKeys.ENCODING, "gb2312");
		properties.setProperty(OutputKeys.METHOD, "xml");
		properties.setProperty(OutputKeys.VERSION, "1.0");
		properties.setProperty(OutputKeys.INDENT, "no");
		transformer.setOutputProperties(properties);
		return transformer;
	} catch (TransformerConfigurationException tce) {
		throw new RuntimeException(tce.getMessage());
	}
}
 
Example 8
Project: LibraSock   File: XmlUtils.java   View Source Code Vote up 6 votes
public static void save(String paramString, Document paramDocument) throws Exception {
	DOMSource localDOMSource = new DOMSource(paramDocument);
	File localFile1 = new File(paramString);
	File localFile2 = localFile1.getParentFile();
	localFile2.mkdirs();
	StreamResult localStreamResult = new StreamResult(localFile1);
	try {
		TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
		Transformer localTransformer = localTransformerFactory.newTransformer();
		Properties localProperties = localTransformer.getOutputProperties();
		localProperties.setProperty("encoding", "UTF-8");
		localProperties.setProperty("indent", "yes");
		localTransformer.setOutputProperties(localProperties);
		localTransformer.transform(localDOMSource, localStreamResult);
	} catch (TransformerConfigurationException localTransformerConfigurationException) {
		localTransformerConfigurationException.printStackTrace();
	} catch (TransformerException localTransformerException) {
		localTransformerException.printStackTrace();
	}
}
 
Example 9
Project: DaUtil   File: DomXmlUtils.java   View Source Code Vote up 6 votes
/**
 * 获取一个Transformer对象,由于使用时都做相同的初始化,所以提取出来作为公共方法。
 *
 * @return a Transformer encoding gb2312
 */

public static Transformer newTransformer() {
	try {
		Transformer transformer = TransformerFactory.newInstance()
				.newTransformer();
		Properties properties = transformer.getOutputProperties();
		properties.setProperty(OutputKeys.ENCODING, "gb2312");
		properties.setProperty(OutputKeys.METHOD, "xml");
		properties.setProperty(OutputKeys.VERSION, "1.0");
		properties.setProperty(OutputKeys.INDENT, "no");
		transformer.setOutputProperties(properties);
		return transformer;
	} catch (TransformerConfigurationException tce) {
		throw new RuntimeException(tce.getMessage());
	}
}
 
Example 10
Project: alipay-sdk   File: XmlUtils.java   View Source Code Vote up 6 votes
/**
 * Converts the Node/Element instance to XML payload.
 *
 * @param node the node/element instance to convert
 * @return the XML payload representing the node/element
 * @throws ApiException problem converting XML to string
 */
public static String childNodeToString(Node node) throws AlipayApiException {
    String payload = null;

    try {
        Transformer tf = TransformerFactory.newInstance().newTransformer();

        Properties props = tf.getOutputProperties();
        props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
        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;
}
 
Example 11
Project: alipay-sdk   File: XmlUtils.java   View Source Code Vote up 6 votes
/**
 * 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 ApiException 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;
}
 
Example 12
Project: alipay-sdk   File: XmlUtils.java   View Source Code Vote up 6 votes
/**
    * Transforms the XML content to XHTML/HTML format string with the XSL.
    *
    * @param payload the XML payload to convert
    * @param xsltFile the XML stylesheet file
    * @return the transformed XHTML/HTML format string
    * @throws ApiException problem converting XML to HTML
    */
   public static String xmlToHtml(String payload, File xsltFile)
throws AlipayApiException {
       String result = null;

       try {
           Source template = new StreamSource(xsltFile);
           Transformer transformer = TransformerFactory.newInstance()
                   .newTransformer(template);

           Properties props = transformer.getOutputProperties();
           props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
           transformer.setOutputProperties(props);

           StreamSource source = new StreamSource(new StringReader(payload));
           StreamResult sr = new StreamResult(new StringWriter());
           transformer.transform(source, sr);

           result = sr.getWriter().toString();
       } catch (TransformerException e) {
           throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
       }

       return result;
   }
 
Example 13
Project: pay   File: XmlUtils.java   View Source Code Vote up 6 votes
/**
 * Converts the Node/Element instance to XML payload.
 *
 * @param node the node/element instance to convert
 * @return the XML payload representing the node/element
 * @throws AlipayApiException problem converting XML to string
 */
public static String childNodeToString(Node node) throws AlipayApiException {
    String payload = null;

    try {
        Transformer tf = TransformerFactory.newInstance().newTransformer();

        Properties props = tf.getOutputProperties();
        props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
        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;
}
 
Example 14
Project: pay   File: XmlUtils.java   View Source Code Vote up 6 votes
/**
 * 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;
}
 
Example 15
Project: pay   File: XmlUtils.java   View Source Code Vote up 6 votes
/**
    * Transforms the XML content to XHTML/HTML format string with the XSL.
    *
    * @param payload the XML payload to convert
    * @param xsltFile the XML stylesheet file
    * @return the transformed XHTML/HTML format string
    * @throws AlipayApiException problem converting XML to HTML
    */
   public static String xmlToHtml(String payload, File xsltFile)
throws AlipayApiException {
       String result = null;

       try {
           Source template = new StreamSource(xsltFile);
           Transformer transformer = TransformerFactory.newInstance()
                   .newTransformer(template);

           Properties props = transformer.getOutputProperties();
           props.setProperty(OutputKeys.OMIT_XML_DECLARATION, LOGIC_YES);
           transformer.setOutputProperties(props);

           StreamSource source = new StreamSource(new StringReader(payload));
           StreamResult sr = new StreamResult(new StringWriter());
           transformer.transform(source, sr);

           result = sr.getWriter().toString();
       } catch (TransformerException e) {
           throw new AlipayApiException("XML_TRANSFORM_ERROR", e);
       }

       return result;
   }
 
Example 16
Project: openjdk-jdk10   File: TransformerTest03.java   View Source Code Vote up 5 votes
/**
 * Test for Transformer.setOutputProperties method.
 *
 * @throws Exception If any errors occur.
 */
@Test
public void testcase01() throws Exception {
    String outputFile = USER_DIR + "transformer03.out";
    String goldFile = GOLDEN_DIR + "transformer03GF.out";
    String xsltFile = XML_DIR + "cities.xsl";
    String xmlFile = XML_DIR + "cities.xml";

    try (FileInputStream fis = new FileInputStream(xmlFile);
            FileOutputStream fos = new FileOutputStream(outputFile)) {
        Properties properties = new Properties();
        properties.put("method", "xml");
        properties.put("encoding", "UTF-8");
        properties.put("omit-xml-declaration", "yes");
        properties.put("{http://xml.apache.org/xslt}indent-amount", "0");
        properties.put("indent", "no");
        properties.put("standalone", "no");
        properties.put("version", "1.0");
        properties.put("media-type", "text/xml");

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DOMSource domSource = new DOMSource(dbf.newDocumentBuilder().
                parse(new File(xsltFile)));

        Transformer transformer = TransformerFactory.newInstance().
                newTransformer(domSource);
        transformer.setOutputProperties(properties);
        transformer.transform(new StreamSource(fis), new StreamResult(fos));
    }
    assertTrue(compareWithGold(goldFile, outputFile));
}