I have the following program in which I am trying to generate a XML which is also generated through DOM parser. I want to store the generated XML into a string variable and it isn't working.
How can I store the generated XML into a string variable?
public class generatexml {
public static void main(String[] args) {
//************ wantt to store the generated xml in a string ********
String s = generatexml();
}
public static StreamResult generatexml()
{
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
// root elements
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("kermail");
doc.appendChild(rootElement);
for(int i =0 ;i<5 ;i++ )
{
Element invoiceReferenceNotificationMessage = doc.createElement("invoiceReferenceNotificationMessage");
rootElement.appendChild(invoiceReferenceNotificationMessage);
Element InvoiceReference = doc.createElement("abceReference");
InvoiceReference.appendChild(doc.createTextNode("7815"));
invoiceReferenceNotificationMessage.appendChild(abceReference);
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
return result;
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
}}