Java tutorial
/** * * * Copyright (C) 2007 Stephen Harding * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Please send inquiries to; steve@inverse2.com * * $Revision: 1.4 $ * * $Log: FindXML.java,v $ * Revision 1.4 2008/07/01 17:26:47 stevewdh * Changed logging so that user can specify the type they want (Java, Log4J or HTML). * * Revision 1.3 2008/04/17 13:22:25 stevewdh * Substitution variables can now be taken from the input XML - this is used by the XMLtoSQL class. * * Revision 1.2 2008/03/05 10:43:15 stevewdh * *** empty log message *** * * Revision 1.1 2007/10/04 11:06:27 stevewdh * *** empty log message *** * * Revision 1.2 2007/09/30 13:09:05 stephen harding * Added contact details to license header. * * Revision 1.1 2007/09/15 16:09:06 stephen harding * Added header. * * */ package com.init.octo.util; import java.util.Iterator; import java.util.StringTokenizer; import org.jdom2.Element; public class FindXML { private static Logger log = Logger.getLogger(FindXML.class.getName()); // logging object /** * This method locates an XML tag or attribute based on an input string. * The input locator string must be in the format root.element.element or root.element.[attribute] * * @param locator - the definition of the element you want to locate * @param root - the root element of the XML structure * * @returns String - the string we have found, or null of it wasn't found */ static public String findXML(String locator, Element root) { Element element = null; String retStr = null; StringTokenizer tokens = new StringTokenizer(locator, "."); String str = tokens.nextToken(); if (tokens.countTokens() == 0) { locator = "root." + locator; tokens = new StringTokenizer(locator, "."); str = tokens.nextToken(); } // follow the locator text element name definition down... element = root; while (tokens.hasMoreTokens()) { str = tokens.nextToken(); if (str.startsWith("[")) { // an attribute has been specified String attName = str.substring(1, str.indexOf("]")); retStr = element.getAttributeValue(attName); element = null; break; } else { String[] spec = str.split(":"); if (spec.length == 1) { element = element.getChild(str); } else { /** A specific member of a repeating group has been specified... **/ Iterator<?> it = element.getChildren(spec[0]).iterator(); int idx = 1; int num = 0; try { num = Integer.parseInt(spec[1]); } catch (Exception x) { log.warn("Bad element index [" + spec[1] + "]"); num = 1; } while (it.hasNext()) { element = (Element) it.next(); if (idx == num) { break; } } /** If we go past the end of the list we will return the last one... **/ /** this way the call can detect no change in the output... **/ } } if (element == null) { return (null); } } if (element != null) { // wasn't specified as an attribute... retStr = element.getTextTrim(); } return (retStr); } // end findXML() } // end class FindXML