Java - XML

I need to wrote a function that will be called when someone clicks a Jbutton.  Within the function I need to create a XML file.  The XML needs to be in a string object.  Right now I am only looking for the basics, once I have that in place then I will start pulling data to populate the XML values.
jkteaterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
CEHJCommented:
Create a Document and add Node instances to it. You should add the content as you go along or your app will gain in overhead and complexity
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

// Create the builder and the doc

Document doc = factory.newDocumentBuilder().newDocument();

Open in new window

jkteaterAuthor Commented:
CEHJ

Good Morning,

Should a function be created that creates the XML?  Can you give me a example on how to code it?  Lets assume we are going to use hard code instead of getting the values.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE EdiRequest SYSTEM "test.dtd">

<EdiRequest version="1.0">
   <User login="jteater" />
   <ItemRevision itemId="LEX-0006561" revId="002" prl="001">
      <MasterDataset name="LEX-0006561-002" type="PART" />
      <ExtraDataset name="LEX-0006561-002" type="DRAWING" />
   </ItemRevision>

Lets say when I click the Send Button - I want it to create the above in a string Object
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

jkteaterAuthor Commented:
I am sorry I was out of the office for a few days.  I understand how to use the document builder and I am pretty sure how to convert it to a string.  I am still confused on how to write the root and children to make the XML.  

<EdiRequest version="1.0">
   <User login="jteater" />
   <ItemRevision itemId="LEX-0006561" revId="002" prl="001">
      <MasterDataset name="LEX-0006561-002" type="PART" />
      <ExtraDataset name="LEX-0006561-002" type="DRAWING" />
   </ItemRevision>
</EdiRequest>

This is not from a file or a document.  I have to basically write it out.  I am including the function so far.  Please show me how to put the above in.  
private void createXML(){
      try{
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder docBuilder = factory.newDocumentBuilder();
      Document doc = docBuilder.newDocument();
      Element root = doc.createElement("root");
      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("Child");
      //Add the atribute to the child
      childElement.setAttribute("attribute1","The value of Attribute 1");
      root.appendChild(childElement);
    

      TransformerFactory tranFactory = TransformerFactory.newInstance(); 
      Transformer aTransformer = tranFactory.newTransformer(); 

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

Open in new window

CEHJCommented:
Well you've got the idea - you just need to use the real values ;) Here's a start
private void createXML() {
        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");
            //Add the atribute to the child
            childElement.setAttribute("login", "jteater");
            root.appendChild(childElement);

            TransformerFactory tranFactory = TransformerFactory.newInstance();
            Transformer aTransformer = tranFactory.newTransformer();

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

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkteaterAuthor Commented:
First let me say thanks for staying with me on this question.

I understand how you created the childElement User.  But I do not understand how to create a element like
itemRevision?  How do you address the MasterDataset?  Is it a child of a child?

<EdiRequest version="1.0">
   <User login="jteater" />
   <ItemRevision itemId="LEX-0006561" revId="002" prl="001">
      <MasterDataset name="LEX-0006561-002" type="PART" />
      <ExtraDataset name="LEX-0006561-002" type="DRAWING" />
   </ItemRevision>
</EdiRequest>

I think I have the child element for ItemRevision created, but not sure how to include MasterDataset and Extradataset.  Also how do you close the element?  </itemRevison>?
Element childElement = doc.createElement("User");
          childElement.setAttribute("login", "jteater");
          root.appendChild(childElement);
          
          Element childElementItemRev = doc.createElement("ItemRevision");
          childElementItemRev.setAttribute("itemId", "LEX-0006561");
          childElementItemRev.setAttribute("revId", "002");
          childElementItemRev.setAttribute("prl", "001");
          root.appendChild(childElementItemRev);

Open in new window

CEHJCommented:
Yes, you'd append a new child, called 'MasterDataset' to ItemRevision
jkteaterAuthor Commented:
Does this look right
Element childElement = doc.createElement("User");
          childElement.setAttribute("login", "jteater");
          root.appendChild(childElement);
          
          Element childElementItemRev = doc.createElement("ItemRevision");
          childElementItemRev.setAttribute("itemId", "LEX-0006561");
          childElementItemRev.setAttribute("revId", "002");
          childElementItemRev.setAttribute("prl", "001");
             
             Element childMasterDataSet = doc.createElement("MasterDataset");
             childMasterDataSet.setAttribute("name", "LEX-0006561-002");
             childMasterDataSet.setAttribute("type", "PART");
             childElementItemRev.appendChild(childMasterDataSet);
             
             Element childExtraDataSet = doc.createElement("ExtraDataset");
             childExtraDataSet.setAttribute("name", "LEX-0006561-002");
             childExtraDataSet.setAttribute("type", "DRAWING");
             childElementItemRev.appendChild(childExtraDataSet);
          
          root.appendChild(childElementItemRev);

Open in new window

CEHJCommented:
Yes. Do your serialization - that will be the best way to test it
jkteaterAuthor Commented:
Keep your eyes posted they will be another question coming :)  It deals with the same type of thing but I feel this question has been answered.

Thanks
CEHJCommented:
:)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.