[jdom-interest] XMLOutputter

Rolf Lear rlear at algorithmics.com
Thu Feb 12 05:00:35 PST 2004


You have a bunch of problems ....

Firstly, white-space in XML has no meaning unless specifically requested
(xml:space="preserve"). Thus, the following two XML documents are
equivalent:
<root><element>text</element></root>
and
<root>
  <element>text</element>
</root>

Secondly, the two documents you have printed in your mail are not
equivalent. The <gui3> element in the first document has no content, but has
<window-manager> in the second. So, your code is not actually adding the
<window-manager> element to the <gui3> element, but to the <linux> element.

As for pretty-printing the XML, try using
  new XMLOutputter("  ",true).outputString(doc)
if you are beta 9 or earlier, and 
  new XMLOutputter(Format.getPrettyFormat()).outputString(doc)
if you are using Beta 10.

Finally, the <?xml version="1.0" encoding="UTF-8"?> line is just fine. Leave
it there!

Rolf

-----Original Message-----
From: jdom-interest-admin at jdom.org
[mailto:jdom-interest-admin at jdom.org]On Behalf Of J. Albers
Sent: Thursday, February 12, 2004 7:46 AM
To: jdom-interest at jdom.org
Subject: [jdom-interest] XMLOutputter


Hi,

I got a bit of a problem, i'm using XMLOutputter to let my application put
some manipulated XML elements in the output in an XML format. But the
problem is i just get it in 1 line like:

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

<linux><gui3></gui3><window-manager></window
manager><type>java</type><name>Enlightenment</name><version3>0.16.2</version
3><gui></gui><window-manager></window
manager><name>Enlightenment</name><version1>0.16.2</version1></linux>


All info i need is there, but not formatted right... it should be :
<linux>
 <gui3>
    <window-manager>
      <type>java</type>
      <name>Enlightenment</name>
      <version3>0.16.2</version3>
    </window-manager>
  </gui3>
  <gui>
    <window-manager bla="2">
      <name>Enlightenment</name>
      <version1>0.16.2</version1>
    </window-manager>
  </gui>
</linux>


Anyone here know what's wrong?

Thanks, Joachim.

----------------------------------------------------------------------------
------

private static void compareChildren(Element element1, Element element2,
Document doc, Element root)
 {
 List children1 = element1.getChildren();
 List children2 = element2.getChildren();
 if(children1.size() > children2.size())// lijst 2 moet de grootste zijn.
 {
   children1 = element2.getChildren();
   children2 = element1.getChildren();
 }
 if(children1.size() > 0 &&  children2.size() > 0)
 System.out.println("Elements are "+ getSamePercentage(children1,
children2)+" % the same.");

 //Element child = new Element("shit");

 for (int i=0; (i < (children1.size())); i++)
 {
    if((children1.get(i).toString()).equals(children2.get(i).toString()))
    {
     System.out.println(children1.get(i)+" and1 "+children2.get(i)+" are the
same.");
  //printStuff((Element)children1.get(i), doc, root);
  Element childname = (Element)children1.get(i);
  Element child = new Element(""+childname.getName());
  String text = childname.getTextNormalize();
   //if((text != null) && (!text.equals(""))){
   child.addContent(text);
  //}
  root.addContent(child);
  compareChildren(((Element) children1.get(i)), ((Element)
children2.get(i)), doc, root);
    }
    else
    {
   for (int j=1; (j < (children2.size()));j++)
   {
    if( (children1.get(i).toString()).equals((children2.get(j).toString())))
    {
     System.out.println(children1.get(i)+" and2 "+children2.get(j)+" are the
same.");
     //printStuff((Element)children1.get(i), doc, root);
     Element childname = (Element)children1.get(i);
     Element child = new Element(""+childname.getName());
     String text = childname.getTextNormalize();
     // if((text != null) && (!text.equals(""))){
      child.addContent(text);
     //}
     root.addContent(child);
     compareChildren(((Element) children1.get(i)), ((Element)
children2.get(j)),doc, root);
    }
      }
    }

 }
 try{
  result.output.setText(new XMLOutputter().outputString(doc));
 }
 catch (Exception e) {e.printStackTrace();}
   }

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com



More information about the jdom-interest mailing list