1 package org.jahia.tools.migration; 2 3 import org.w3c.dom.Element ; 4 import org.w3c.dom.NodeList ; 5 import org.w3c.dom.Node ; 6 7 14 public class XMLUtility { 15 16 25 public static String getElementValue (String tagName, Element parentElement) 26 throws Exception { 27 NodeList foundElements = parentElement.getElementsByTagName (tagName); 28 if (foundElements.getLength () == 1) { 29 return getElementTextValue ((Element ) foundElements.item (0)); 30 } else { 31 throw new Exception ("ERROR : only one element is allowed for " + parentElement.getTagName()); 32 } 33 } 34 35 43 public static String getElementTextValue (Element rootElement) 44 throws Exception { 45 NodeList childNodes = rootElement.getChildNodes (); 46 StringBuffer result = new StringBuffer (); 47 for (int i = 0; i < childNodes.getLength (); i++) { 48 if (childNodes.item (i).getNodeType () == Node.TEXT_NODE) { 49 result.append (childNodes.item (i).getNodeValue ()); 50 } else { 51 throw new Exception ("ERROR : only text values are allowed for " + rootElement.getTagName()); 52 } 53 } 54 return result.toString (); 55 } 56 } | Popular Tags |