XSLT Processing Update (#14693)
This commit is contained in:
parent
b7c65ace34
commit
29c9c5dba9
@ -0,0 +1,31 @@
|
||||
package com.baeldung.xsltProcessing;
|
||||
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import java.io.File;
|
||||
|
||||
public class XSLTProcessorWithParametersAndOption {
|
||||
public static void transformXMLWithParametersAndOption(
|
||||
String inputXMLPath,
|
||||
String xsltPath,
|
||||
String outputHTMLPath,
|
||||
String companyName,
|
||||
boolean enableIndentation
|
||||
) throws TransformerException {
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Source xsltSource = new StreamSource(new File(xsltPath));
|
||||
Transformer transformer = transformerFactory.newTransformer(xsltSource);
|
||||
|
||||
transformer.setParameter("companyName", companyName);
|
||||
|
||||
if (enableIndentation) {
|
||||
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
|
||||
}
|
||||
|
||||
Source xmlSource = new StreamSource(new File(inputXMLPath));
|
||||
Result outputResult = new StreamResult(new File(outputHTMLPath));
|
||||
|
||||
transformer.transform(xmlSource, outputResult);
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.xsltProcessing;
|
||||
|
||||
import javax.xml.transform.*;
|
||||
import javax.xml.transform.stream.StreamResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
import java.io.File;
|
||||
|
||||
public class XSLTProcessorWithTemplate {
|
||||
public static void transformXMLUsingTemplate(String inputXMLPath, String xsltPath, String outputHTMLPath) throws TransformerException {
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
Source xsltSource = new StreamSource(new File(xsltPath));
|
||||
Templates templates = transformerFactory.newTemplates(xsltSource);
|
||||
|
||||
Transformer transformer = templates.newTransformer();
|
||||
|
||||
Source xmlSource = new StreamSource(new File(inputXMLPath));
|
||||
Result outputResult = new StreamResult(new File(outputHTMLPath));
|
||||
|
||||
transformer.transform(xmlSource, outputResult);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user