Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

convert xml into string

843789May 16 2010 — edited May 16 2010
Hello,
iam doing program , that need to convert xml file into string ,


this program just read the xml file , but doesn't convert it to string, coz i don't know how to do it!, some one could teach me how?
import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class example
{
    public static void main( String[] args )
    {

    	SAXBuilder builder = new SAXBuilder();
    	File xmlFile = new File("file.xml");

        try{

    	   Document document = (Document) builder.build(xmlFile);
           Element rootNode = document.getRootElement();
           List list = rootNode.getChildren("info");

           for (int i=0; i< list.size(); i++)
           {
             Element node = (Element) list.get(i);

             System.out.println("name : "  + node.getChildText("name"));
       	     System.out.println("city :  "  + node.getChildText("city"));
      	     System.out.println("age : "  + node.getChildText("age"));

           }

    	 }catch(IOException io){
    		System.out.println(io.getMessage());
    	 }catch(JDOMException jdomex){
    		System.out.println(jdomex.getMessage());
    	}
    }
}
and the xml file
<?xml version="1.0"?>
<data>
   <info>
          <name>Tom</name>
          <city>London</city>
	  <age>40</age>
    </info>
     <info>
          <name>Michael</name>
          <city>Berlin</city>
	  <age>50</age>
    </info>
   <info>
        <name>Raphael</name>
        <city>Paris</city>
	<age>54</age>
   </info>
</data>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 13 2010
Added on May 16 2010
9 comments
344 views