本文整理汇总了Java中javax.xml.transform.TransformerFactory.newTransformer方法的典型用法代码示例。如果您正苦于以下问题:Java TransformerFactory.newTransformer方法的具体用法?Java TransformerFactory.newTransformer怎么用?Java TransformerFactory.newTransformer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.xml.transform.TransformerFactory
的用法示例。
在下文中一共展示了TransformerFactory.newTransformer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testTransformer
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
@Test
public void testTransformer() throws TransformerException {
String xml = "<?xml version='1.0'?><root/>";
ReaderStub.used = false;
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
InputSource in = new InputSource(new StringReader(xml));
SAXSource source = new SAXSource(in);
StreamResult result = new StreamResult(new StringWriter());
transformer.transform(source, result);
assertTrue(ReaderStub.used);
}
示例2: writeXml
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* Write out the non-default properties in this configuration to the given
* {@link Writer}.
*
* @param out the writer to write to.
*/
public void writeXml(Writer out) throws IOException {
Document doc = asXmlDocument();
try {
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(out);
TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = transFactory.newTransformer();
// Important to not hold Configuration log while writing result, since
// 'out' may be an HDFS stream which needs to lock this configuration
// from another thread.
transformer.transform(source, result);
} catch (TransformerException te) {
throw new IOException(te);
}
}
示例3: test
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
@Test
public void test() {
try {
String xml = "<html><body><a href='http://example.com/bla?x&y'>" + "</a></body></html>";
Source src = new StreamSource(new StringReader(xml));
StringWriter stringWriter = new StringWriter();
Result res = new StreamResult(stringWriter);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setOutputProperty(OutputKeys.METHOD, "html");
t.transform(src, res);
// System.out.println("s = " + stringWriter.toString());
Assert.assertTrue(stringWriter.toString().contains("&"));
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
示例4: testCheckElementContentWhitespace
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* Test for the isIgnoringElementContentWhitespace and the
* setIgnoringElementContentWhitespace. The xml file has all kinds of
* whitespace,tab and newline characters, it uses the MyNSContentHandler
* which does not invoke the characters callback when this
* setIgnoringElementContentWhitespace is set to true.
* @throws Exception If any errors occur.
*/
@Test
public void testCheckElementContentWhitespace() throws Exception {
String goldFile = GOLDEN_DIR + "dbfactory02GF.out";
String outputFile = USER_DIR + "dbfactory02.out";
MyErrorHandler eh = MyErrorHandler.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(true);
assertFalse(dbf.isIgnoringElementContentWhitespace());
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setErrorHandler(eh);
Document doc = db.parse(new File(XML_DIR, "DocumentBuilderFactory06.xml"));
assertFalse(eh.isErrorOccured());
DOMSource domSource = new DOMSource(doc);
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
SAXResult saxResult = new SAXResult();
try(MyCHandler handler = MyCHandler.newInstance(new File(outputFile))) {
saxResult.setHandler(handler);
transformer.transform(domSource, saxResult);
}
assertTrue(compareWithGold(goldFile, outputFile));
}
示例5: getStringFromDocument
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
private static String getStringFromDocument(Document document) throws TransformerException {
DOMSource domSource = new DOMSource(document);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
}
示例6: write
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* XML書き込み
*
* @param document xml document
* @throws IOException IOエラー
* @throws TransformerException XML書き込みエラー
*/
private void write(final Document document) throws IOException, TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
transformer
.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://cobertura.sourceforge.net/xml/coverage-04.dtd");
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(this.reportPath)) {
transformer.transform(new DOMSource(document), new StreamResult(bufferedWriter));
}
}
示例7: process
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
@Override
public void process(ProcessingContext<Corpus> ctx, Corpus corpus) throws ModuleException {
try {
Logger logger = getLogger(ctx);
TransformerFactory factory = TransformerFactory.newInstance();
InputStream is = xslTransform.getInputStream();
Source transformerSource = new StreamSource(is);
Transformer transformer = factory.newTransformer(transformerSource);
is.close();
transformer.setErrorListener(new ListingErrorHandler());
libraryResolver = getLibraryResolver(ctx);
Evaluator roots = libraryResolver.resolveNullable(this.roots);
Evaluator fileName = this.fileName.resolveExpressions(libraryResolver);
EvaluationContext evalCtx = new EvaluationContext(logger);
for (Element root : Iterators.loop(getRoots(roots, evalCtx, corpus))) {
transformer.reset();
Document doc = XMLUtils.docBuilder.newDocument();
doc.setUserData(ELEMENT_USER_DATA, root, null);
// org.w3c.dom.Element elt = getElementProxy(doc, root);
// doc.appendChild(elt);
Source source = new DOMSource(doc);
String fileNameString = fileName.evaluateString(evalCtx, root);
// System.out.println("creating file: " + fileNameString);
File outFile = new File(outDir, fileNameString);
Result result = new StreamResult(outFile);
transformer.transform(source, result);
}
} catch (DOMException | TransformerException | IOException e) {
rethrow(e);
}
}
示例8: writeFile
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
private void writeFile(String title, ArrayList<String> list) throws IOException, ParserConfigurationException, TransformerException {
System.out.println("GetEmoticons.writeFile");
System.out.println("title = " + title);
System.out.println("list = " + list);
if (title == null || list == null || list.isEmpty()) return;
String fileName = title.replaceAll("\\W", "") + ".xml";
File file = new File("C:\\github\\AsciiGenerator\\app\\src\\main\\assets\\emoticons\\" + fileName);
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document document = documentBuilder.newDocument();
org.w3c.dom.Element root = document.createElement("root");
document.appendChild(root);
org.w3c.dom.Element name = document.createElement("name");
name.appendChild(document.createTextNode(title));
root.appendChild(name);
org.w3c.dom.Element data = document.createElement("data");
for (String s : list) {
org.w3c.dom.Element item = document.createElement("item");
item.appendChild(document.createTextNode(s));
data.appendChild(item);
}
root.appendChild(data);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(file);
transformer.transform(domSource, streamResult);
}
示例9: testQueueConfigurationParser
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* test xml generation
* @throws ParserConfigurationException
* @throws Exception
*/
@Test (timeout=5000)
public void testQueueConfigurationParser()
throws ParserConfigurationException, Exception {
JobQueueInfo info = new JobQueueInfo("root", "rootInfo");
JobQueueInfo infoChild1 = new JobQueueInfo("child1", "child1Info");
JobQueueInfo infoChild2 = new JobQueueInfo("child2", "child1Info");
info.addChild(infoChild1);
info.addChild(infoChild2);
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
Document document = builder.newDocument();
// test QueueConfigurationParser.getQueueElement
Element e = QueueConfigurationParser.getQueueElement(document, info);
// transform result to string for check
DOMSource domSource = new DOMSource(e);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
String str= writer.toString();
assertTrue(str
.endsWith("<queue><name>root</name><properties/><state>running</state><queue><name>child1</name><properties/><state>running</state></queue><queue><name>child2</name><properties/><state>running</state></queue></queue>"));
}
示例10: transformer03
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* This tests if newTransformer(DOMSource) method returns Transformer.
*
* @throws Exception If any errors occur.
*/
@Test
public void transformer03() throws Exception {
TransformerFactory tfactory = TransformerFactory.newInstance();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new File(TEST_XSL));
DOMSource domSource = new DOMSource(document);
Transformer transformer = tfactory.newTransformer(domSource);
assertNotNull(transformer);
}
示例11: saveXml
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
public void saveXml(OutputStream outputStream, Document doc) throws Exception {
TransformerFactory transFactory = TransformerFactory.newInstance();
transFactory.setAttribute("indent-number", new Integer(3));
Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.VERSION, "1.0 ");
PrintWriter printWriter = new PrintWriter(outputStream);
transformer.transform(new DOMSource(doc), new StreamResult(printWriter));
}
示例12: marshalSamlXmlObject
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* Marshal the saml xml object to raw xml.
*
* @param object the object
* @param writer the writer
* @return the xml string
*/
public String marshalSamlXmlObject(final XMLObject object, final StringWriter writer) {
try {
final MarshallerFactory marshallerFactory = XMLObjectProviderRegistrySupport.getMarshallerFactory();
final Marshaller marshaller = marshallerFactory.getMarshaller(object);
if (marshaller == null) {
throw new IllegalArgumentException("Cannot obtain marshaller for object " + object.getElementQName());
}
final Element element = marshaller.marshall(object);
element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns", SAMLConstants.SAML20_NS);
element.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xenc", "http://www.w3.org/2001/04/xmlenc#");
final TransformerFactory transFactory = TransformerFactory.newInstance();
final Transformer transformer = transFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(element), new StreamResult(writer));
return writer.toString();
} catch (final Exception e) {
throw new IllegalStateException("An error has occurred while marshalling SAML object to xml", e);
}
}
示例13: newFormatingTransformer
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* Creates a XML transformer with the following properties:<br />
* <br />
* character encoding: UTF-8<br />
* output method: xml<br />
* output version: 1.0<br />
* indention size: 2
*/
public static Transformer newFormatingTransformer()
throws TransformerConfigurationException {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xalan}indent-amount", "2");
return transformer;
}
示例14: getIdentityTransformer
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
/**
* Gets the shared instance of the identity transformer.
*/
public Transformer getIdentityTransformer() {
try {
if(identityTransformer==null) {
TransformerFactory tf = XmlFactory.createTransformerFactory(model.options.disableXmlSecurity);
identityTransformer = tf.newTransformer();
}
return identityTransformer;
} catch (TransformerConfigurationException e) {
throw new Error(e); // impossible
}
}
示例15: ignoreAttTest
import javax.xml.transform.TransformerFactory; //导入方法依赖的package包/类
@Test
public void ignoreAttTest() throws TransformerConfigurationException {
/* Create a TransformFactory instance */
TransformerFactory transformerFactory = TransformerFactory.newInstance();
/* Create and init a StreamSource instance */
StreamSource source = new StreamSource(new StringReader(XSL));
transformerFactory.newTransformer(source);
}