KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > tools > migration > XMLUtility


1 package org.jahia.tools.migration;
2
3 import org.w3c.dom.Element JavaDoc;
4 import org.w3c.dom.NodeList JavaDoc;
5 import org.w3c.dom.Node JavaDoc;
6
7 /**
8  * This class should keep all XML related method.
9  * It could be used for the main Jahia core, because these methods came from it.
10  *
11  * @author PELTIER Olivier
12  * @version 1.0
13  */

14 public class XMLUtility {
15
16     /**
17      * Extract the value of an xml Element.
18      *
19      * @param tagName String, the name of the Tag to extract.
20      * @param parentElement Element, the Element that should contains the tag.
21      * @return String.
22      *
23      * @throws Exception, when a multi valued tag is found.
24      */

25     public static String JavaDoc getElementValue (String JavaDoc tagName, Element JavaDoc parentElement)
26             throws Exception JavaDoc {
27         NodeList JavaDoc foundElements = parentElement.getElementsByTagName (tagName);
28         if (foundElements.getLength () == 1) {
29             return getElementTextValue ((Element JavaDoc) foundElements.item (0));
30         } else {
31                 throw new Exception JavaDoc ("ERROR : only one element is allowed for " + parentElement.getTagName());
32         }
33     }
34
35     /**
36      * Get the text value of an Element.
37      *
38      * @param rootElement Element, the Element to convert into text.
39      * @return String, the concatenation of all values found into rootElement.
40      *
41      * @throws Exception, when non text values are stored into the rootElement.
42      */

43     public static String JavaDoc getElementTextValue (Element JavaDoc rootElement)
44             throws Exception JavaDoc {
45         NodeList JavaDoc childNodes = rootElement.getChildNodes ();
46         StringBuffer JavaDoc result = new StringBuffer JavaDoc ();
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 JavaDoc("ERROR : only text values are allowed for " + rootElement.getTagName());
52             }
53         }
54         return result.toString ();
55     }
56 }
Popular Tags