2

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

4

1 回答 1

2

不知道为什么你有 DOCTYPE 问题。我稍后会尝试重现它。在此期间,当您更改线路时,它可能会消失:

        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);              

        SAXBuilder saxBuilder = new SAXBuilder();
        Document xIssuesMappingXMLInternal = saxBuilder.build(
               new File("C://Folder/Template.xml"));              

使用 JDOM 时无需使用 DOM 中间文档

编辑:(根据上面的评论)真正的问题不是问题,它只是“正常工作”。您看到的是对 Document 调用 toString() 方法的结果:您只是没有复制/粘贴 [] 大括号,这会使这一点更加明显,我不会感到困惑。

一切正常,并且,如果您希望将 XML 文档的输出作为字符串值(而不仅仅是文档的 toString() 值),请使用XMLOutputter.outputString(doc)

于 2013-05-23T19:26:05.373 回答