1 36 37 package jnlp.sample.servlet; 38 39 import javax.xml.parsers.*; 40 import java.util.ArrayList ; 41 import java.util.List ; 42 import org.xml.sax.*; 43 import org.w3c.dom.*; 44 45 48 public class XMLParsing { 49 50 public static XMLNode convert(Node n) { 51 if (n == null) { 52 return null; 53 } else if (n instanceof Text) { 54 Text tn = (Text)n; 55 return new XMLNode(tn.getNodeValue()); 56 } else if (n instanceof Element) { 57 Element en = (Element)n; 58 59 XMLAttribute xmlatts = null; 60 NamedNodeMap attributes = en.getAttributes(); 61 for(int i = attributes.getLength() - 1; i >= 0; i--) { 62 Attr ar = (Attr)attributes.item(i); 63 xmlatts = new XMLAttribute(ar.getName(), ar.getValue(), xmlatts); 64 } 65 66 XMLNode thisNode = new XMLNode(en.getNodeName(), xmlatts, null, null);; 68 XMLNode last = null; 69 Node nn = en.getFirstChild(); 70 while(nn != null) { 71 if (thisNode.getNested() == null) { 72 last = convert(nn); 73 thisNode.setNested(last); 74 } else { 75 XMLNode nnode = convert(nn); 76 last.setNext(nnode); 77 last = nnode; 78 } 79 last.setParent(thisNode); 80 nn = nn.getNextSibling(); 81 } 82 83 return thisNode; 84 } 85 return null; 86 } 87 88 89 static public boolean isElementPath(XMLNode root, String path) { 90 return findElementPath(root, path) != null; 91 } 92 93 94 95 static public String getPathString(XMLNode e) { 96 return (e == null || !(e.isElement())) ? "" : getPathString(e.getParent()) + "<" + e.getName() + ">"; 97 } 98 99 100 101 static public String getElementContent(XMLNode root, String path) { 102 return getElementContent(root, path, null); 103 } 104 105 106 static public String [] getMultiElementContent(XMLNode root, String path) { 107 final List list = new ArrayList (); 108 visitElements(root, path, new ElementVisitor() { 109 public void visitElement(XMLNode n) { 110 String value = getElementContent(n, ""); 111 if (value != null) list.add(value); 112 } 113 }); 114 if (list.size() == 0) return null; 115 return (String [])list.toArray(new String [list.size()]); 116 } 117 118 121 static public String getElementContent(XMLNode root, String path, String defaultvalue) { 122 XMLNode e = findElementPath(root, path); 123 if (e == null) return defaultvalue; 124 XMLNode n = e.getNested(); 125 if (n != null && !n.isElement()) return n.getName(); 126 return defaultvalue; 127 } 128 129 133 static public XMLNode findElementPath(XMLNode elem, String path) { 134 if (elem == null) return null; 136 if (path == null || path.length() == 0) return elem; 138 139 int idx = path.indexOf('>'); 141 String head = path.substring(1, idx); 142 String tail = path.substring(idx + 1); 143 return findElementPath(findChildElement(elem, head), tail); 144 } 145 146 147 static public XMLNode findChildElement(XMLNode elem, String tag) { 148 XMLNode n = elem.getNested(); 149 while(n != null) { 150 if (n.isElement() && n.getName().equals(tag)) return n; 151 n = n.getNext(); 152 } 153 return null; 154 } 155 156 157 public abstract static class ElementVisitor { 158 abstract public void visitElement(XMLNode e); 159 } 160 161 164 static public void visitElements(XMLNode root, String path, ElementVisitor ev) { 165 int idx = path.lastIndexOf('<'); 167 String head = path.substring(0, idx); 168 String tag = path.substring(idx + 1, path.length() - 1); 169 170 XMLNode elem = findElementPath(root, head); 171 if (elem == null) return; 172 173 XMLNode n = elem.getNested(); 175 while(n != null) { 176 if (n.isElement() && n.getName().equals(tag)) { 177 ev.visitElement(n); 178 } 179 n = n.getNext(); 180 } 181 } 182 183 static public void visitChildrenElements(XMLNode elem, ElementVisitor ev) { 184 XMLNode n = elem.getNested(); 186 while(n != null) { 187 if (n.isElement()) ev.visitElement(n); 188 n = n.getNext(); 189 } 190 } 191 } 192 193 | Popular Tags |