Newbie: How to create XML in Java?

Dear Experts,

How do you create XML from within Java, other than by hand-coding it?  Assuming you have:

FieldA, with value "A",
and
FieldB, with value "123",

is there some AUTOMATED technique for generating:

<FieldA>A</FieldA><FieldB>123</FieldB>

Could you point me towards documentation?

Thanks, Experts!

--BrianMc1958
BrianMc1958Asked:
Who is Participating?
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.

petmagdyCommented:
0
kiranhkCommented:
this has got the sample code to generate the xml from a java class

http://www.codegeneration.net/tiki-read_article.php?articleId=65
0
petmagdyCommented:
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

sudhakar_koundinyaCommented:
follow step by step procedure as suggested in above links
0
NaeemgCommented:
Try this code.


    Document doc = null;
    try
    {
      DOMImplementation domImpl = DocumentBuilderFactory.newInstance()
        .newDocumentBuilder()
        .getDOMImplementation();

      //-- create DOM document with default root node;
      doc = domImpl.createDocument(null, "RootNode", null);
      Node node = doc.createElement("FieldA"); //-- first element node
      Node text = doc.createTextNode("A"); //-- Text node
      node.appendChild(text); //-- append text node to element
      doc.getDocumentElement().appendChild(node); //-- append element node to Root node

      node = doc.createElement("FieldB"); //-- first element node
      text = doc.createTextNode("B"); //-- Text node
      node.appendChild(text); //-- append text node to element
      doc.getDocumentElement().appendChild(node); //-- append element node to Root node

    //-- now ur DOM document is ready. u can save its data to file by getting its contents as
    System.out.println(doc.getDocumentElement());
    }
    catch(ParserConfigurationException e)
    {
      System.err.println(e.getMessage());
      //e.printStackTrace(System.err);
    }
    catch(DOMException e)
    {
      System.err.println(e.getMessage());
      //e.printStackTrace(System.err);
    }
0

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
BrianMc1958Author Commented:
You Experts are incredible.  Thank you to everyone, especially to Naeemg!  It's just what I was looking for.

--BrianMc1958
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.