Java & XML, 2nd Edition private void loadFromElements(List (Hp web site)
Tuesday, September 25th, 2007Java & XML, 2nd Edition private void loadFromElements(List elements, StringBuffer baseName) { // Iterate through each elementfor (Iterator i = elements.iterator(); i.hasNext( ); ) { Element current = (Element)i.next( ); String name = current.getName( ); String text = current.getTextTrim( ); // Don’t add “.” if no baseName if (baseName.length( ) > 0) { baseName.append(”.”); } baseName.append(name); // See if we have an element value if ((text == null) || (text.equals(”"))) { // If no text, recurse on childrenloadFromElements(current.getChildren( ), baseName); } else { // If text, this is a property setProperty(baseName.toString( ), text); } // On unwind from recursion, remove last name if (baseName.length() == name.length( )) { baseName.setLength(0); } else { baseName.setLength(baseName.length( ) - (name.length( ) + 1)); } } } The implementation of the load( ) method (which all overloaded versions delegate to) uses SAXBuilder to read in the supplied XML document. I discussed this earlier in the chapter, and I’ll look at it in even more detail in the next; for now, it’s enough to realize that it simply reads XML into a JDOM Document object. The name for a property consists of the names of all the elements leading to the property value, with a period separating each name. Here’s a sample property in XML:
Visit our web design programs services for an affordable and reliable webhost to suit all your needs.