org.dom4j.io.DocumentSource Java Examples
The following examples show how to use
org.dom4j.io.DocumentSource.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Dom4jTransformer.java From tutorials with MIT License | 6 votes |
public String modifyAttribute(String attribute, String oldValue, String newValue) throws TransformerException { // 2- Locate the node(s) with xpath, we can use index and iterator too. String expr = String.format("//*[contains(@%s, '%s')]", attribute, oldValue); XPath xpath = DocumentHelper.createXPath(expr); List<Node> nodes = xpath.selectNodes(input); // 3- Make the change on the selected nodes for (int i = 0; i < nodes.size(); i++) { Element element = (Element) nodes.get(i); element.addAttribute(attribute, newValue); } // 4- Save the result to a new XML doc TransformerFactory factory = TransformerFactory.newInstance(); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); Transformer xformer = factory.newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer output = new StringWriter(); xformer.transform(new DocumentSource(input), new StreamResult(output)); return output.toString(); }
Example #2
Source File: Dom4jUtils.java From spring-boot-study with MIT License | 5 votes |
/** * 使用XSLT转换XML * */ public static Document styleDocument(Document document, String stylesheet) throws Exception { TransformerFactory factory = TransformerFactory.newInstance();//实例化转换器工厂 TransformerFactory Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));// 创建一个转化格式对象 DocumentSource source = new DocumentSource(document); // XML 源对象 DocumentResult result = new DocumentResult(); //转换结果对象 transformer.transform(source, result);//转换操作 Document transformedDoc = result.getDocument();//获取转换后的文档 return transformedDoc; }
Example #3
Source File: HTMLBugReporter.java From spotbugs with GNU Lesser General Public License v2.1 | 5 votes |
@Override public void finish() { try { BugCollection bugCollection = getBugCollection(); bugCollection.setWithMessages(true); // Decorate the XML with messages to display Document document = bugCollection.toDocument(); // new AddMessages(bugCollection, document).execute(); // Get the stylesheet as a StreamSource. // First, try to load the stylesheet from the filesystem. // If that fails, try loading it as a resource. InputStream xslInputStream = getStylesheetStream(stylesheet); StreamSource xsl = new StreamSource(xslInputStream); xsl.setSystemId(stylesheet); // Create a transformer using the stylesheet TransformerFactory factory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null); Transformer transformer = factory.newTransformer(xsl); // Source document is the XML generated from the BugCollection DocumentSource source = new DocumentSource(document); // Write result to output stream StreamResult result = new StreamResult(outputStream); // Do the transformation transformer.transform(source, result); } catch (Exception e) { logError("Could not generate HTML output", e); fatalException = e; if (FindBugs.DEBUG) { e.printStackTrace(); } } outputStream.close(); }
Example #4
Source File: ParserUtils.java From directory-ldap-api with Apache License 2.0 | 4 votes |
/** * XML Pretty Printer XSLT Transformation * * @param document the Dom4j Document * @return the transformed document */ public static Document styleDocument( Document document ) { // load the transformer using JAXP TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = null; try { factory.setFeature( javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE ); try { factory.setAttribute( javax.xml.XMLConstants.ACCESS_EXTERNAL_DTD, "" ); factory.setAttribute( javax.xml.XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "" ); } catch ( IllegalArgumentException ex ) { // ignore } transformer = factory.newTransformer( new StreamSource( ParserUtils.class .getResourceAsStream( "/org/apache/directory/shared/dsmlv2/DSMLv2.xslt" ) ) ); } catch ( TransformerConfigurationException e1 ) { if ( LOG.isWarnEnabled() ) { LOG.warn( I18n.msg( I18n.MSG_3000_FAILED_TO_CREATE_XSLT_TRANSFORMER ), e1 ); } // return original document return document; } // now lets style the given document DocumentSource source = new DocumentSource( document ); DocumentResult result = new DocumentResult(); try { transformer.transform( source, result ); } catch ( TransformerException e ) { // return original document return document; } // return the transformed document return result.getDocument(); }