[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Newbie: How to create XML in Java?

Posted on 2004-11-02
9
Medium Priority
?
204 Views
Last Modified: 2008-03-10
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
0
Comment
Question by:BrianMc1958
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12476478
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12476548
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
 
LVL 13

Expert Comment

by:petmagdy
ID: 12476557
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 8

Expert Comment

by:kiranhk
ID: 12476619
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12476644
follow step by step procedure as suggested in above links
0
 
LVL 5

Accepted Solution

by:
Naeemg earned 1000 total points
ID: 12480395
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
 

Author Comment

by:BrianMc1958
ID: 12483263
You Experts are incredible.  Thank you to everyone, especially to Naeemg!  It's just what I was looking for.

--BrianMc1958
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

830 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