[jdom-interest] Using Xpath and Element GetDocument

Mike Brenner mikeb at mitre.org
Fri May 30 05:27:28 PDT 2003


mark wrote:
> in what context do you use doc.getRootElement().getChildren()?

Bradley S. Huffman wrote:
> The now deprecated getChildren/getChild methods where always a bit confusing
> since DOM uses "children" to mean all mixed content, i.e. elements,
> comments, text, pi, and so forth, and JDOM had getChildren return a list of only
> elements.


What? Things are moving too fast. 

I use doc.getRootElement().getChildren() whenever I use JDOM, 
which means to create a HashMap of HashMaps of HashMaps
of ... representing the xml in a quick-lookup tree of HashMaps.

For example, in a simple two-level xml file containing an associative list,
the 3 methods given below in Java source code make that simple HashMap. 

The key lines of code are:

	Document doc = builder.build(f); // in parse
	root=doc.getRootElement(); // in parse returning to XML2Hash_1Level
	List children = current.getChildren(); // in listChildren_1Level

which reduces down exactly to 

	doc.getRootElement().getChildren()

Since it compiles in JDOM-b9, I take it that this means
that it will be deprecated in JDOM-b10, which we will
have time to play with before going to JDOM-1.0?

Since interfaces are still being changed, I take it that
we are NOT ready to go to JDOM-1.0 yet?


MY QUESTION: Why is getChildren, the very center of JDOM, being deprecated? 

Does that not guarantee that every JDOM program that was ever
written now has to be changed to something else?


Instead of deprecating it, why don't we use it to 
create some one-liner methods that create
a tree of HashMaps of HashMaps ... that corresponds to
the XML file as a method in JDOM that does it all at once?

There would need to be more than one of these do-it-all
methods, because there are several style of converting 
xml to HashMaps, based on which set of attributes and 
elements constitutes the relational key, if any (that 
is, whether it is randomly or sequentially accessed).



///////////////////////////////////////////////////////////////////////
	boolean showTime=false;

	/** Only needed until JDOM version b9 comes out, fixing the memory leakage.
	    Historical note: this method did not actually fix the memory leakage,
	    which turned out to be in StringBuffer, and it was the buffer
	    in the String in the StringBuffer that needed to be deallocated,
	    not the children of each temporary JDOM tree. But when the server
	    goes down with memory errors, you wantonly try desperate and
	    insane tactical and strategic deallocations! */
	public static void nullifyJDOM(Element root){ 
		try{
			List children = root.getChildren();
			Iterator itr=children.iterator();
			while (itr.hasNext()){
				Element child=(Element)itr.next();
				itr.remove();
			}
		} catch (NullPointerException ne){}
	}

  	/** Moves a one-level xml file into a HashMap. */
  	public static Object XML2Hash_1Level(Reader f) throws JDOMException {
		Element root=parse(f);
		Object value= listChildren_1Level(root);
		nullifyJDOM(root);
		return value;
  	}

  	// listChildren: Loops through all the children
  	private static Object listChildren_1Level(Element current) {
		Element child;
		List children = current.getChildren();
		Iterator iterator = children.iterator();
      		int lenOfAll = children.size();
		HashMap h = new HashMap(hashFactor*lenOfAll);
		while (iterator.hasNext()) {
			child = (Element) iterator.next();
			h.put(child.getName(), child.getTextTrim());
		}
		return h;
	}

  	private static Element parse(Reader f) throws JDOMException{
	Element root = null;
    	try {
			long startTime=0;
			long finishTime=0;
			if (showTime) {
				startTime = System.currentTimeMillis();
			}
			Document doc = builder.build(f);
			root = doc.getRootElement();
			if (showTime) {
				finishTime=System.currentTimeMillis();
				System.err.println("JDOM Start "+startTime+
								   " Finish "+finishTime+
								   " Duration "+(finishTime-startTime));
			}
		} catch (JDOMException e) {
			System.err.println(f + " is not well-formed.");
			System.err.println(e.getMessage());
			throw new JDOMException();
		} catch (IOException e) { /* New in b9 -- you need to catch 2 exceptions instead of one */
			System.err.println("IOException in ReadXML.java!");
		return root;
	}




More information about the jdom-interest mailing list