Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

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!

JDom issue....!!!

843840Oct 15 2007 — edited Oct 15 2007
Hi Friends,


I made one small application to output the xml.

which gives output like
<?xml version="1.0" encoding="UTF-8"?>
<car vin="123fhg5869705iop90"><make>Toyota</make><model>Celica</model><color>green</color><license state="CA">1ABC234</license><!--Description of a car--></car>
all lines in one line.

I tried to use following for new line but since i found no method in
JDom v1.0
XMLOutputter outp = new XMLOutputter();
outp.output(doc, fileStream);
outp.setTextTrim(true);
outp.setTextTrim(true);
outp.setIndent(" ");
outp.setNewlines(true);
outp.output(doc, System.out);
I have included Jdom.jar and programme running successfully and making
XML but when I try to make above methods it is not in the list
of outp object.

Please Help me.
I am Using NetBeans 5.0

I want formatted file output for XML.


My Code is :
import java.io.FileWriter;
import org.jdom.Attribute;
import org.jdom.Comment;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Administrator
 */
public class JDomEx1
{
    
    /** Creates a new instance of JDomEx1 */
    public JDomEx1()
    {    }
    
    public static void main(String args[])
    {
        Element carElement = new Element("car");
        Document myDocument = new Document(carElement);
        carElement.setAttribute(new Attribute("vin", "123fhg5869705iop90"));
        Element make = new Element("make");
        make.addContent("Toyota");
        carElement.addContent(make);
        carElement.addContent(new Element("model").addContent("Celica"));
        carElement.addContent(new Element("year").addContent("1997"));
        carElement.addContent(new Element("color").addContent("green"));
        carElement.addContent(new Element("license").addContent("1ABC234").setAttribute("state", "CA"));
        carElement.addContent(new Comment("Description of a car"));
        Element yearElement = carElement.getChild("year");
        boolean removed = carElement.removeChild("year");
        try
        {
            XMLOutputter outputter = new XMLOutputter();
            FileWriter writer = new FileWriter("myFile.xml");
            outputter.output(myDocument, writer);
            
            outputter.output(myDocument, System.out);
            writer.close();
            
            //outputter.output(myDocument, System.out);
        }
        catch (java.io.IOException e)
        {
            e.printStackTrace();
        }
        
        
        
    }
    
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 12 2007
Added on Oct 15 2007
4 comments
285 views