I need to save the newly created DOMSource object as new XML file into a folder inside the SFTP server (not transferring a file from local computer into SFTP).
Here is the code
public void save(String xmlFilePath, Document document) {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
Transformer transformer;
try {
transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File(xmlFilePath));
transformer.transform(domSource,streamResult);
} catch (TransformerException | NullPointerException e) {
e.printStackTrace();
}
}
Use JSch SSH/SFTP library.
Afaik, it's the most widely used SFTP library for Java
It supports uploading data from streams.
I assume that its ChannelSftp.put overload that returns OutputStream can be hooked to your StreamResult (instead of the File).
StreamResult streamResult =
new StreamResult(channelSftp.put("/sftp/path/file.zml"));
Related
I am trying to creating an XML file using java. I am explicitly passing the path for the new XML file to be created and it is getting created successfully. But now, how do I get the XML file in the project folder automatically without giving the path.
My CWD is - C:\Users\sit\eclipse-workspace\XMLProject\src
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("C:\Users\sit\eclipse-workspace\XMLProject\src\abc.xml"));
transformer.transform(source, result);
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}
}
StreamResult result = new StreamResult(new File("abc.xml"));
Simply just give it the file name without the absolute path and it will put it in the project folder. If you want it specifically in the src folder, then you need to:
StreamResult result = new StreamResult(new File("src\\abc.xml"));
Instead of an absolute path, we are giving it a relative path. Take a look here for better understanding how to use paths and differences between them.
How do I write this document to the local filesystem?
public void docToFile(org.w3c.dom.Document document, URI path) throws Exception {
File file = new File(path);
}
I need to iterate the document, or might there be a "to xml/html/string" method? I was looking at:
document.getXmlEncoding();
Not quite what I'm after -- but something like that. Looking for the String representation and then to write that to file like:
Path file = ...;
byte[] buf = ...;
Files.write(file, buf);
https://docs.oracle.com/javase/tutorial/essential/io/file.html
I would use a transformer class to convert the DOM content to an xml file, something like below:
Document doc =...
// write the content into xml file
DOMSource source = new DOMSource(doc);
FileWriter writer = new FileWriter(new File("/tmp/output.xml"));
StreamResult result = new StreamResult(writer);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.transform(source, result);
I hope this ends up working for you!
I want to print the syncml payload in a xml file before using it. Is there a method in java to print the syncml payload into a xml file or a way to check the payload?
Solved by converting it to a string. (able to find more details from --> http://www.theserverside.com/news/thread.tss?thread_id=26060)
public void printXML(Document request){
try
{
DOMSource domSource = new DOMSource(request);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
String checkpayload = writer.toString();
System.out.print(checkpayload);
}
catch(TransformerException ex)
{
ex.printStackTrace();
}
}
Good Afternoon. I want to use the StringWriter to write the new file to a network folder. Can anyone give me some examples using the code below on how to do this? It's my first time working with the StringWriter class.
public static final void newOutput(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter out = new StringWriter();
tf.transform(new DOMSource(xml), new StreamResult(out));
/*
* need to update to write to folder
*/
System.out.println(out.toString());
}
}
public static final void newOutput(Document xml) throws Exception {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tf.setOutputProperty(OutputKeys.INDENT, "yes");
DOMSource source = new DOMSource(xml);
// Use StreamResult to write to a file to current directory
StreamResult out = new StreamResult(new File("test.txt"));
// to print to console
// StreamResult out = new StreamResult(System.out);
tf.transform(source, out);
/*
* console output is redirected to SRC folder to check format
* need to update to write to folder
*/
System.out.println(out.toString());
}
i would like to append a new tag with some attributes with those values in to xml file and save that xml file through my application.i have written a method for append a new tag as child to xml file which is available in sdcard of android emulator.the following method for append a new tag as follows
public void appendTag(){
try{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("/sdcard/sample.xml"));
Node node = doc.getElementsByTagName("earth").item(0);
//append a new node to earth
Element newelmnt = doc.createElement("new");
newelmnt.appendChild(doc.createTextNode("this is a text"));
node.appendChild(newelmnt);
}
catch (Exception e) {
e.printStackTrace();
}
}
after execution of this method i can't able to find a new tag in xml file.
could please any one help on how to append new tag as child in xml file and how save the modification?
if i uses TransformerFactory i am getting error as
ERROR/AndroidRuntime(13479): java.lang.VerifyError: com.sample.xmlapp.DOMClass
i have used as follows
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/sdcard/sample.xml"));
transformer.transform(source, result);
Your in memory document will be changed, but you will need to write it to a file again.
Try adding this for writing the document to the file again:
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/sdcard/sample.xml"));
transformer.transform(source, result);