/** * getChildText purpose. * * <p>Used to help with XML manipulations. Returns the text value of the specified element name. * * @param elem The root element to look for children in. * @return The value if the text was found, the null otherwise. */ public static String getElementText(Element elem) { try { return getElementText(elem, false); } catch (Exception ex) { return null; } }
/** * getKeyWords purpose. * * <p>Used to help with XML manipulations. Returns a list of keywords that were found. * * @param keywordsElem The root element to look for children in. * @return The list of keywords that were found. */ public static List getKeyWords(Element keywordsElem) { NodeList klist = keywordsElem.getElementsByTagName("keyword"); int kCount = klist.getLength(); List keywords = new ArrayList(kCount); String kword; Element kelem; for (int i = 0; i < kCount; i++) { kelem = (Element) klist.item(i); kword = getElementText(kelem); if (kword != null) { keywords.add(kword); } } Object[] s = (Object[]) keywords.toArray(); if (s == null) { return new ArrayList(); } ArrayList ss = new ArrayList(s.length); for (int i = 0; i < s.length; i++) ss.add(s[i]); return ss; }
/** * getChildText purpose. * * <p>Used to help with XML manipulations. Returns the first child text value of the specified * element name. An exception occurs when the node is required and not found. * * @param root The root element to look for children in. * @param childName The name of the attribute to look for. * @param mandatory true when an exception should be thrown if the text does not exist. * @return The value if the child was found, the null otherwise. * @throws Exception When a child attribute is required and not found. */ public static String getChildText(Element root, String childName, boolean mandatory) throws Exception { Element elem = getChildElement(root, childName, mandatory); if (elem != null) { return getElementText(elem, mandatory); } else { if (mandatory) { String msg = "Mandatory child " + childName + "not found in " + " element: " + root; throw new Exception(msg); } return null; } }