[jdom-interest] Problem with XMLOutputter or Element

Chandra Sekhar chandra_sekhar at centegy.com
Sat Aug 5 17:14:48 PDT 2000


Hi,

 I have checked out the latest sources from CVS and tested the same on this.
This was done on 05th August 2000:16:30

 My input java file & the output are included.

===============================================================
import java.io.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.Map;

public class first{
	public static void output(Document doc)
	{
		XMLOutputter out = new XMLOutputter();
		try  {  out.output(doc,System.out);}
		catch(IOException e)
		{  e.printStackTrace();}
	}
	public static void main(String []args)
	{
		Element root = new Element("Root");
		Document doc = new Document(root);

		root.setText("Inside the Root element");
		Element firstChild = new Element("Firstchild");
		root.addContent(firstChild);
		firstChild.setText(" Inside the first child element");
		output(doc);

		Element newElem = new Element("newRootElement");
		newElem.setText("Inside the New RootElement");
		doc.setRootElement(newElem);
		Element secChild = new Element("Secondchild");
		newElem.addContent(secChild).addContent(new
Element("Thirdelement"));
		secChild.setText("Inside the second child");
		try{
			newElem.getChild("Thirdelement").setText("Inside the
Third element");}
		catch(Exception e)
		   {e.printStackTrace();}
		output(doc);

	}
}
===============================================================

the output is 
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<Root>
	Inside the Root element
        <Firstchild> 
          Inside the first child element
        </Firstchild>
</Root>

<?xml version="1.0" encoding="UTF-8"?>

<newRootElement>
	Inside the New RootElement
 	    <Secondchild>
		   Inside the second child 
                </Secondchild>
                <Thirdelement>
		   Inside the Third element 
	    </Thirdelement>
</newRootElement>
<newRootElement>
	Inside the New RootElement
	    <Secondchild>
		   Inside the second child 
	    </Secondchild>
	    <Thirdelement>
		   Inside the Third element
	    </Thirdelement>
</newRootElement>
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

My question is why is the second root element coming twice?

I have seen the code in XMLOutputter ,

 It goes through each objects of the Document.
 if the object is of type Element it always prints the contents of
RootElement.
 in the present scenario, it finds two elements at the top most place it
prints the RootElement twice.

 My question at this point is, can a document have more than one Elements
which are at the same level especially at the root level. because we can do
a setRootElement on the Document object. In this function the Document sets
the IsRootElement of the current root Element false and the makes the new
Element's  IsRootElement to true.

   the solution to this is either provide another function to delete the old
RootElement or do this internally in the setRootElement() function or change
the functionality of the XMLOutputter so that it prints the Element contents
only when it is a RootElement.

Thanks
Sekhar



More information about the jdom-interest mailing list