1 16 package org.apache.myfaces.util.xml; 17 18 import org.w3c.dom.Element ; 19 import org.w3c.dom.Node ; 20 import org.w3c.dom.NodeList ; 21 22 import java.util.ArrayList ; 23 import java.util.Collections ; 24 import java.util.List ; 25 26 30 public class XmlUtils 31 { 32 private XmlUtils() 33 { 34 } 36 37 public static String getElementText(Element elem) 38 { 39 StringBuffer buf = new StringBuffer (); 40 NodeList nodeList = elem.getChildNodes(); 41 for (int i = 0, len = nodeList.getLength(); i < len; i++) 42 { 43 Node n = nodeList.item(i); 44 if (n.getNodeType() == Node.TEXT_NODE) 45 { 46 buf.append(n.getNodeValue()); 47 } 48 else 49 { 50 } 53 } 54 return buf.toString(); 55 } 56 57 58 59 68 public static String getChildText(Element elem, String childTagName) 69 { 70 NodeList nodeList = elem.getElementsByTagName(childTagName); 71 int len = nodeList.getLength(); 72 if (len == 0) 73 { 74 return null; 75 } 76 else 77 { 78 return getElementText((Element )nodeList.item(len - 1)); 79 } 80 } 81 82 83 89 public static List getChildTextList(Element elem, String childTagName) 90 { 91 NodeList nodeList = elem.getElementsByTagName(childTagName); 92 int len = nodeList.getLength(); 93 if (len == 0) 94 { 95 return Collections.EMPTY_LIST; 96 } 97 else 98 { 99 List list = new ArrayList (len); 100 for (int i = 0; i < len; i++) 101 { 102 list.add(getElementText((Element )nodeList.item(i))); 103 } 104 return list; 105 } 106 } 107 108 } 109 | Popular Tags |