?
Solved

Tags overwriting while trying to create tags inside a for loop (Creating XML filr using Java)

Posted on 2006-05-24
3
Medium Priority
?
278 Views
Last Modified: 2013-11-19
Hello experts,
                   m trying to create an XML file using Java. The XML file m expecting is as follows

                  <states>
                         <state>
                                  <name>Alabama</name>
                                  <url>url1</url>
                        </state>

                         <state>
                                  <name>Alaska</name>
                                  <url>url2</url>
                        </state>

                          <state>
                                  <name>Arizona</name>
                                  <url>url3</url>
                        </state>
 
                  </states>


In the Java Program m using a for loop for writing the nodes to the root. But m getting only one node outputted, namely the last one as the nodes are overwritten. My Java code is as follows :


public class CreateXml {
      public static void main(String[] args) throws TransformerException {
          DocumentBuilderFactory domFactory = null;
          DocumentBuilder domBuilder = null;
          String xmlFileName = "states";

          
          try
           {
            domFactory = DocumentBuilderFactory.newInstance();
            domBuilder = domFactory.newDocumentBuilder();
            }
          catch(FactoryConfigurationError exp)
           {
            System.err.println(exp.toString());
           }
        catch(ParserConfigurationException exp)
        {
            System.err.println(exp.toString());
        }
        catch(Exception exp)
        {
            System.err.println(exp.toString());
        }
       
              String[] states = {"Alabama","Alaska","Arizona"};
      
            Document newDoc = domBuilder.newDocument();

        // Root element
        Element rootElement = newDoc.createElement("states");
       
        Element state  = newDoc.createElement("state");
        Element stateName = newDoc.createElement("name");
        Element stateUrl =  newDoc.createElement("url");
       
        newDoc.appendChild(rootElement);

       //  Problem Occuring Here..
        for(int i=0;i<states.length;i++)
        {
              
              rootElement.appendChild(state);
              
              System.out.println(i);
              stateName.setTextContent(states[i]);
              
              
              stateUrl.setTextContent("url"+i);
              state.appendChild(stateName);
              state.appendChild(stateUrl);
            
              
            
          }
       

        //Save Document
        TransformerFactory tranFactory = TransformerFactory.newInstance();
        Transformer aTransformer = tranFactory.newTransformer();

        Source src = new DOMSource(newDoc);
        Result dest = new StreamResult(new File(xmlFileName));
        aTransformer.transform(src, dest);
}
 
The output m getting is :
<?xml version="1.0" encoding="UTF-8"?>
<states>
      <state>
            <name>Arizona</name>
            <url>url3</url>
      </state>
</states>

instead of the original.. Please help..
0
Comment
Question by:royalcyber
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 1500 total points
ID: 16751598
>>Element state  = newDoc.createElement("state");

should be *inside* the loop
0
 

Author Comment

by:royalcyber
ID: 16751657
Thanx CEHJ.. real thanx :-)..
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16751736
:-) Then why a 'B'? ;-)
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

621 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