[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Dom Document to Object String

Posted on 2011-04-25
1
Medium Priority
?
247 Views
Last Modified: 2012-06-27
I have created a Dom Document and I want to make it a String Object.  I have read over this article
http://helpdesk.objects.com.au/java/how-to-create-xml-string-from-a-dom-document 
but I need more help.  Thanks for any help you can give me.


try {
          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          DocumentBuilder docBuilder = factory.newDocumentBuilder();
          Document doc = docBuilder.newDocument();
          Element root = doc.createElement("EdiRequest");
          doc.appendChild(root);

          //Comment comment = doc.createComment("This is comment");
          //add in the root element
          //root.appendChild(comment);

          //create child element
          Element childElement = doc.createElement("User");
          childElement.setAttribute("login", LoginUser);
          root.appendChild(childElement);
          
          //ItemRevision
          Element childElementItemRev = doc.createElement("ItemRevision");
          for (i = 0; i < selItems.size(); ++i) {
             Item item = (Item) selItems.get(i);
             childElementItemRev.setAttribute("itemId", item.getItemId());
             for (j = 0; j < item.getNumberRevisions(); ++j) {
                ItemRevision ir = item.getRev(j);
                childElementItemRev.setAttribute("revId", ir.getRevId());
                childElementItemRev.setAttribute("prl", ir.getPrl());
                if (ir.getNumberDatasets() > 0) {
                   for (k = 0; k < ir.getNumberDatasets(); ++k) {
                      Dataset d = ir.getDataset(k);
                      //Sub MasterDataset
                      Element childMasterDataSet = doc.createElement("MasterDataset");
                      childMasterDataSet.setAttribute("name", d.getName());
                      childMasterDataSet.setAttribute("type", d.getType());
                      childElementItemRev.appendChild(childMasterDataSet);
                      //END MasterDataset
                  }   
               }
             }   
          } 
             //Sub ExtraDataset
             //Element childExtraDataSet = doc.createElement("ExtraDataset");
             //childExtraDataSet.setAttribute("name", "LEX-0006561-002");
             //childExtraDataSet.setAttribute("type", "DRAWING");
             //childElementItemRev.appendChild(childExtraDataSet);
             //END ExtraDataset
          
          root.appendChild(childElementItemRev);
          //END ItemRevision
          
          //TwoDTranslators
          Element childTwoDTrans = doc.createElement("TwoDTranslators");
             Element childTwoDTranslators = doc.createElement("Translators");
             for (int ii = 0; ii < transOptionIds.size(); ++ii) {
                Integer id = (Integer) transOptionIds.get(ii);
                TranslatorOption tOpt = (twod ? EdiSystemData.getTwodTranslatorOption(id.intValue()) : EdiSystemData.getThreedTranslatorOption(id.intValue()));
                Translator trans = tOpt.getBaseTranslator();
                childTwoDTranslators.setAttribute("name", trans.getTransName());
                if (tOpt.getOptionName().length() > 0) {
                   Element childTwoDTransOption = doc.createElement("TranslatorOption");
                   childTwoDTransOption.setAttribute("name", tOpt.getOptionName());
                   childTwoDTranslators.appendChild(childTwoDTransOption);
                }
                childTwoDTrans.appendChild(childTwoDTranslators);
             }
          root.appendChild(childTwoDTrans); 
          //END TwoDTranslators
          
          //ThreeDTranslators
          Element childThreeDTrans = doc.createElement("ThreeDTranslators");
             Element childThreeDTranslators = doc.createElement("Translators");
             childThreeDTranslators.setAttribute("name", "STEP");
                Element childThreeDTransOption = doc.createElement("TranslatorOption");
                childThreeDTransOption.setAttribute("name", "STEP_VERSION");
                childThreeDTransOption.setAttribute("value", "203");
                childThreeDTranslators.appendChild(childThreeDTransOption);
             childThreeDTrans.appendChild(childThreeDTranslators);
          root.appendChild(childThreeDTrans); 
          //END ThreeDTranslators  
          
          //Destination
          Element childDestination = doc.createElement("Destination");
          childDestination.setAttribute("downloadURL", downloadURL);
          
             Element childreqtrans = doc.createElement("RequestTransmittal");
             //keyuser
                for (int l = 0; l < info.keyUserIds.size(); ++l) {
                   String Kid = (String) transOptionIds.get(i);
                   Element childkeyUser = doc.createElement("KeyUser");
                   childkeyUser.setAttribute("keyuser", Kid);
                   childreqtrans.appendChild(childkeyUser);
                }
             //amepme
                for (int l = 0; l < info.amePmeIds.size(); ++l) {
                   String APid = (String) transOptionIds.get(i);
                   Element childamepme = doc.createElement("AmePme");
                   childamepme.setAttribute("amepme", APid);
                   childreqtrans.appendChild(childamepme);
                }   
             //amealt
                for (int l = 0; l < info.amePmeIds.size(); ++l) {
                   String AltPid = (String) transOptionIds.get(i);
                   Element childaltame = doc.createElement("AmePmeAlternate");
                   childaltame.setAttribute("altamepme", AltPid);
                   childreqtrans.appendChild(childaltame);
                }             
             //projectname
                Element childProject = doc.createElement("ProjectName");
                childProject.setAttribute("pName", proj.getName());
                childreqtrans.appendChild(childProject);
                            
             //vendorname
                Element childVendor = doc.createElement("VendorName");
                childVendor.setAttribute("vName", proj.getVendorName());
                childreqtrans.appendChild(childVendor);
             
             //compressionformat
                Element childComPress = doc.createElement("CompressionFormat");
                childComPress.setAttribute("compres", EdiSystemData.getCompressionScheme(proj.getCompressionId()).getName());
                childreqtrans.appendChild(childComPress);
            
            //ftp    
                Element childftp = doc.createElement("FTP");
            //ftp host       
                   Element childftphost = doc.createElement("Host");
                   childftphost.setAttribute("host", url.getHost());
                   childftp.appendChild(childftphost);
            //ftp port        
                   Element childport = doc.createElement("Port");
                   childport.setAttribute("port", "21");
                   childftp.appendChild(childport);
            //ftp user       
                   Element childftpuser = doc.createElement("User");
                   childftpuser.setAttribute("ftpuser", "ftpuser");
                   childftp.appendChild(childftpuser);
            //ftp dir       
                   Element childDir = doc.createElement("Directory");
                   childDir.setAttribute("ftpdir", url.getPath());
                   childftp.appendChild(childDir);
            //ftp postUpload       
                   Element childPostUpload = doc.createElement("PostUploadOptions");
            //post chmod          
                      Element childchmod = doc.createElement("Chmod");
                      childchmod.setAttribute("chmod", "664");
                      childPostUpload.appendChild(childchmod);
                   childftp.appendChild(childPostUpload);
                childreqtrans.appendChild(childftp);  
             childDestination.appendChild(childreqtrans);
          root.appendChild(childDestination); 
        
        //Email Addresses
          
          Element childEmails = doc.createElement("EmailAddresses");  
          for (int z = 0; z < emails.length; ++z) {   
             Element childemailAdd = doc.createElement("EmailAddress");
             childemailAdd.setAttribute("email", emails[z]);
             childEmails.appendChild(childemailAdd);
          }
          root.appendChild(childEmails);
          
          
          TransformerFactory tranFactory = TransformerFactory.newInstance();
          Transformer aTransformer = tranFactory.newTransformer();

          Source src = new DOMSource(doc);
          Result dests = new StreamResult(System.out);
          aTransformer.transform(src, (Result) dest);
      }
      catch (Exception e) {
          System.out.println(e.getMessage());
      }

Open in new window

0
Comment
Question by:jkteater
1 Comment
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 35463604


>           Result dests = new StreamResult(System.out);
>          aTransformer.transform(src, (Result) dest);

change that to:

StringWriter writer = new StringWriter();
Result result = new StreamResult(writer);
aTransformer.transform(src, result);
writer.close();

you can then get the xml string using:

String xml = writer.toString();
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month19 days, 18 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question