[jdom-interest] Element.hasMixedContent() method

Brett McLaughlin brett.mclaughlin at lutris.com
Fri Jun 2 16:23:06 PDT 2000


Kevin Regan wrote:
> 
> Could this be implemented a good deal more efficiently
> and simply:
> 
> Class prevClass = null;
> Iterator i = content.iterator();
> while (i.hasNext()) {
>     Object obj = i.next();
>     Class newClass = obj.getClass();
> 
>     if (newClass != prevClass) {
>        if (prevClass != null) {
>           return true;
>        }
>        prevClass = newClass;
>     }
> }
> 
> return false;

yup. Done.

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

-- 
Brett McLaughlin, Enhydra Strategist
Lutris Technologies, Inc. 
1200 Pacific Avenue, Suite 300 
Santa Cruz, CA 95060 USA 
http://www.lutris.com
http://www.enhydra.org

Received: from web1405.mail.yahoo.com (web1405.mail.yahoo.com [128.11.23.169])
	by dorothy.denveronline.net (8.9.3/8.9.3) with SMTP id NAA15860
	for <jdom-interest at jdom.org>; Fri, 2 Jun 2000 13:51:23 -0600 (MDT)
Received: (qmail 21360 invoked by uid 60001); 2 Jun 2000 19:51:22 -0000
Message-ID: <20000602195122.21359.qmail at web1405.mail.yahoo.com>
Received: from [63.96.133.5] by web1405.mail.yahoo.com; Fri, 02 Jun 2000 12:51:22 PDT
Date: Fri, 2 Jun 2000 12:51:22 -0700 (PDT)
From: Jihua Ye <ji_hua_ye at yahoo.com>
To: jdom-interest at jdom.org
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="0-1957747793-959975482=:20208"
Subject: [jdom-interest] getChildren()
Sender: jdom-interest-admin at jdom.org
Errors-To: jdom-interest-admin at jdom.org
X-BeenThere: jdom-interest at jdom.org
X-Mailman-Version: 2.0beta2
Precedence: bulk
List-Id: JDOM Mailing List for General Issues and Updates <jdom-interest.jdom.org>

--0-1957747793-959975482=:20208
Content-Type: text/plain; charset=us-ascii


I tried to get a list of children for a given element. When I print the list returned by getChildren(), I can see every child element appears twice in the list. Why? Can anyone tell me what's wrong in the following code? Thanks.


test.xml


<?xml version="1.0"?>
<person><firstName>Smith</firstName><lastName>Johnson</lastName><age>14</age></person>


ChildrenTest.java


import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;


public class ChildrenTest {
   public static void main(String[] args) {
        
        String filename = "test.xml";
        try {
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(new File(filename));
            Element root = doc.getRootElement();
            file://System.out.println("root=" + root.getName());
            
            ArrayList aList = new ArrayList(root.getChildren());
            System.out.println("root has children: " + aList.size());
            for (int i=0; i<aList.size(); i++) {
               Element elem = (Element) aList.get(i);
               System.out.println(i + elem.getName() + " = " + elem.getContent());
               
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



result:


Copyright (C) 1996-99 Symantec Corporation


root=person
list=[org.jdom.Element at cc6e897d, org.jdom.Element at ccd2897d, org.jdom.Element at cf4
e897d, org.jdom.Element at cc6e897d, org.jdom.Element at ccd2897d, org.jdom.Element at cf
4e897d]
root has children: 6
0firstName = Smith
1lastName = Johnson
2age = 14
3firstName = Smith
4lastName = Johnson
5age = 14





---------------------------------
Do You Yahoo!?
Yahoo! Photos -- now, 100 FREE prints!
--0-1957747793-959975482=:20208
Content-Type: text/html; charset=us-ascii

<P>I tried to get a list of children for a given element. When I print the list returned by getChildren(), I can see every child element appears twice in the list. Why? Can anyone tell me what's wrong in the following code? Thanks.</P><BR>
<P><STRONG>test.xml</STRONG></P><BR>
<P>&lt;?xml version="1.0"?&gt;<BR>&lt;person&gt;&lt;firstName&gt;Smith&lt;/firstName&gt;&lt;lastName&gt;Johnson&lt;/lastName&gt;&lt;age&gt;14&lt;/age&gt;&lt;/person&gt;</P><BR>
<P><STRONG>ChildrenTest.java</STRONG></P><BR>
<P>import java.io.*;<BR>import java.util.*;<BR>import org.jdom.*;<BR>import org.jdom.input.*;</P><BR>
<P>public class ChildrenTest {<BR>&nbsp;&nbsp; public static void main(String[] args) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String filename = "test.xml";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SAXBuilder builder = new SAXBuilder();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Document doc = builder.build(new File(filename));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Element root = doc.getRootElement();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="file://System.out.println/">file://System.out.println</A>("root=" + root.getName());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList aList = new ArrayList(root.getChildren());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("root has children: " + aList.size());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;aList.size(); i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Element elem = (Element) aList.get(i);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(i + elem.getName() + " = " + elem.getContent());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR></P><BR>
<P><STRONG>result:</STRONG></P><BR>
<P>Copyright (C) 1996-99 Symantec Corporation</P><BR>
<P>root=person<BR>list=[org.jdom.Element at cc6e897d, org.jdom.Element at ccd2897d, org.jdom.Element at cf4<BR>e897d, org.jdom.Element at cc6e897d, org.jdom.Element at ccd2897d, org.jdom.Element at cf<BR>4e897d]<BR>root has children: 6<BR>0firstName = Smith<BR>1lastName = Johnson<BR>2age = 14<BR>3firstName = Smith<BR>4lastName = Johnson<BR>5age = 14<BR><BR></P><p><br><hr size=1><b>Do You Yahoo!?</b><br>
<a href="http://photos.yahoo.com/">Yahoo! Photos</a> -- now, 100 FREE prints!
--0-1957747793-959975482=:20208--

Received: from web1405.mail.yahoo.com (web1405.mail.yahoo.com [128.11.23.169])
	by dorothy.denveronline.net (8.9.3/8.9.3) with SMTP id NAA15860
	for <jdom-interest at jdom.org>; Fri, 2 Jun 2000 13:51:23 -0600 (MDT)
Received: (qmail 21360 invoked by uid 60001); 2 Jun 2000 19:51:22 -0000
Message-ID: <20000602195122.21359.qmail at web1405.mail.yahoo.com>
Received: from [63.96.133.5] by web1405.mail.yahoo.com; Fri, 02 Jun 2000 12:51:22 PDT
Date: Fri, 2 Jun 2000 12:51:22 -0700 (PDT)
From: Jihua Ye <ji_hua_ye at yahoo.com>
To: jdom-interest at jdom.org
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary="0-1957747793-959975482=:20208"
Subject: [jdom-interest] getChildren()
Sender: jdom-interest-admin at jdom.org
Errors-To: jdom-interest-admin at jdom.org
X-BeenThere: jdom-interest at jdom.org
X-Mailman-Version: 2.0beta2
Precedence: bulk
List-Id: JDOM Mailing List for General Issues and Updates <jdom-interest.jdom.org>

--0-1957747793-959975482=:20208
Content-Type: text/plain; charset=us-ascii


I tried to get a list of children for a given element. When I print the list returned by getChildren(), I can see every child element appears twice in the list. Why? Can anyone tell me what's wrong in the following code? Thanks.


test.xml


<?xml version="1.0"?>
<person><firstName>Smith</firstName><lastName>Johnson</lastName><age>14</age></person>


ChildrenTest.java


import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;


public class ChildrenTest {
   public static void main(String[] args) {
        
        String filename = "test.xml";
        try {
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(new File(filename));
            Element root = doc.getRootElement();
            file://System.out.println("root=" + root.getName());
            
            ArrayList aList = new ArrayList(root.getChildren());
            System.out.println("root has children: " + aList.size());
            for (int i=0; i<aList.size(); i++) {
               Element elem = (Element) aList.get(i);
               System.out.println(i + elem.getName() + " = " + elem.getContent());
               
            }
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}



result:


Copyright (C) 1996-99 Symantec Corporation


root=person
list=[org.jdom.Element at cc6e897d, org.jdom.Element at ccd2897d, org.jdom.Element at cf4
e897d, org.jdom.Element at cc6e897d, org.jdom.Element at ccd2897d, org.jdom.Element at cf
4e897d]
root has children: 6
0firstName = Smith
1lastName = Johnson
2age = 14
3firstName = Smith
4lastName = Johnson
5age = 14





---------------------------------
Do You Yahoo!?
Yahoo! Photos -- now, 100 FREE prints!
--0-1957747793-959975482=:20208
Content-Type: text/html; charset=us-ascii

<P>I tried to get a list of children for a given element. When I print the list returned by getChildren(), I can see every child element appears twice in the list. Why? Can anyone tell me what's wrong in the following code? Thanks.</P><BR>
<P><STRONG>test.xml</STRONG></P><BR>
<P>&lt;?xml version="1.0"?&gt;<BR>&lt;person&gt;&lt;firstName&gt;Smith&lt;/firstName&gt;&lt;lastName&gt;Johnson&lt;/lastName&gt;&lt;age&gt;14&lt;/age&gt;&lt;/person&gt;</P><BR>
<P><STRONG>ChildrenTest.java</STRONG></P><BR>
<P>import java.io.*;<BR>import java.util.*;<BR>import org.jdom.*;<BR>import org.jdom.input.*;</P><BR>
<P>public class ChildrenTest {<BR>&nbsp;&nbsp; public static void main(String[] args) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String filename = "test.xml";<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; try {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SAXBuilder builder = new SAXBuilder();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Document doc = builder.build(new File(filename));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Element root = doc.getRootElement();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="file://System.out.println/">file://System.out.println</A>("root=" + root.getName());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ArrayList aList = new ArrayList(root.getChildren());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("root has children: " + aList.size());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=0; i&lt;aList.size(); i++) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Element elem = (Element) aList.get(i);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(i + elem.getName() + " = " + elem.getContent());<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } catch (Exception e) {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; e.printStackTrace();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp; }<BR>}<BR></P><BR>
<P><STRONG>result:</STRONG></P><BR>
<P>Copyright (C) 1996-99 Symantec Corporation</P><BR>
<P>root=person<BR>list=[org.jdom.Element at cc6e897d, org.jdom.Element at ccd2897d, org.jdom.Element at cf4<BR>e897d, org.jdom.Element at cc6e897d, org.jdom.Element at ccd2897d, org.jdom.Element at cf<BR>4e897d]<BR>root has children: 6<BR>0firstName = Smith<BR>1lastName = Johnson<BR>2age = 14<BR>3firstName = Smith<BR>4lastName = Johnson<BR>5age = 14<BR><BR></P><p><br><hr size=1><b>Do You Yahoo!?</b><br>
<a href="http://photos.yahoo.com/">Yahoo! Photos</a> -- now, 100 FREE prints!
--0-1957747793-959975482=:20208--



More information about the jdom-interest mailing list