My code is below
Document convertDataToXML(ArrayList<MyData> oMyDataCollection) {
try {
//get the Input template file location
String sExportXMLTemplate = "";
XPathFactory xpfacFile = XPathFactory.instance();
XPathExpression<Element> xElementsLoc = xpfacFile.compile(IssueExchangerXPathConstants.EXPORT_XML_TEMPLATE, Filters.element());
List<Element> elementsLoc = xElementsLoc.evaluate(xConfigurationDocument);
for (Element xElem : elementsLoc) {
logger.trace(xElem.getText());
logger.trace("getXSLTFileName ends");
sExportXMLTemplate =xElem.getText();
break;
}
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
dBuilder = dbFactory.newDocumentBuilder();
org.w3c.dom.Document doc = dBuilder.parse(new File("C://Folder/Template.xml"));
DOMBuilder domBuilder = new DOMBuilder();
//Document xIssuesMappingXMLInternal=null;
Document xIssuesMappingXMLInternal;
xIssuesMappingXMLInternal = domBuilder.build(doc);
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Element> xElements = xpfac.compile(ExportXMLConstants.ISSUES_EXPORT,Filters.element(),null);
List<Element> elements = xElements.evaluate(xIssuesMappingXMLInternal);
for (Element xIssuesParent : elements) {
System.out.println(xIssuesParent.getName());
Element xCloneIssue = null ;
int iCounter=1;
for (Element xIssueChild : xIssuesParent.getChildren())
{
xCloneIssue = xIssueChild.clone();
xCloneIssue.detach();
xIssueChild.detach(); //remove from parent
break; //expecting only one node here..
}
//LOOP THE COLLECTION
Element xClonedCopy = xCloneIssue.clone();
for (RTCData oMyDataCollection : oMyDataCollectionCollection) {
xCloneIssue.getAttribute(ExportXMLConstants.ISSUE_ID_ATTR_EXPORT).setValue("ISSUE-"+iCounter);
//fill other nodes
Element xNewID = xCloneIssue.getChild(ExportXMLConstants.ISSUE_NEW_ID_EXPORT);
xNewID.setText(oMyDataCollection.getsExchangePartnerID());
Element xID = xCloneIssue.getChild(ExportXMLConstants.ISSUE_ID_EXPORT);
xID.setText(oMyDataCollection.getsID_Issue());
Element xSummary = xCloneIssue.getChild(ExportXMLConstants.ISSUE_SUMMARY_EXPORT);
xSummary.setText(oMyDataCollection.getsSummary_ExchangeInfo());
Element xStatus = xCloneIssue.getChild(ExportXMLConstants.ISSUE_STAUTS_EXPORT);
xStatus.setText(oMyDataCollection.getsStatus_Issue());
Element xComments = xCloneIssue.getChild(ExportXMLConstants.ISSUE_COMMENTS_EXPORT);
if (xComments != null)
{
Element xComment = xComments.getChild(ExportXMLConstants.ISSUE_COMMENT_EXPORT);
xComment.setText(oMyDataCollection.getsFeedback());
}
xIssuesParent.addContent(xCloneIssue);
xCloneIssue=xClonedCopy.clone();
iCounter++;
}
}
XMLOutputter xmlOutput = new XMLOutputter();
// display nice nice
xmlOutput.setFormat(Format.getPrettyFormat());
xmlOutput.output(xIssuesMappingXMLInternal, new FileWriter("c:\\temp\\OutputFile.xml"));
//logger.info(xmlOutput.outputString(xIssuesMappingXMLInternal));
//logger.info(xIssuesMappingXMLInternal.toString());
return xIssuesMappingXMLInternal;
} catch (Exception e) {
logger.fatal(e);
}
return null;
}
The purpose of this function is to convert data from object to XML and return the XML Document. To verify this "Document" I am saving the generated XML to a file c:\temp\OutputFile.xml
& I get the XML as I need
But when i try to return the Document which is xIssuesMappingXMLInternal
I always get the following Document: No DOCTYPE declaration, Root is element <ISSUES/>
Why is this c
The XML looks like below
<ISSUES>
<ISSUE ID="123">
<NEWID>123</NEWID>
<ID>233</ID>
<SUMMARY>THIS IS A TEST</SUMMARY>
<STATUS>NEW</STATUS>
<COMMENTS>
<COMMENT>
PLAIN TEXT DATA HERE
</COMMENT>
</COMMENTS>
</ISSUE>
<ISSUE>
.....
</ISSUE>
</ISSUES>
Here I will have multiple nodes
My expectation is that the Document would return the whole ISSUES node.
Also I don't want to use any "doctype" here. it's a simple XML
I am using Java SE 1.6 with JDOM 2.0.4
Any help would be really appreciated
Many thanks