|
| 1 | +/* Daniel Shiffman */ |
| 2 | +/* Programming from A to Z */ |
| 3 | +/* Spring 2006 */ |
| 4 | +/* http://www.shiffman.net */ |
| 5 | +/* daniel.shiffman@nyu.edu */ |
| 6 | + |
| 7 | +/* Class to search / traverse an */ |
| 8 | +/* XML file */ |
| 9 | + |
| 10 | + |
| 11 | +package simpleML; |
| 12 | +import java.net.*; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.io.*; |
| 15 | + |
| 16 | +import javax.xml.parsers.DocumentBuilder; |
| 17 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 18 | +import javax.xml.parsers.ParserConfigurationException; |
| 19 | + |
| 20 | +import org.w3c.dom.Document; |
| 21 | +import org.w3c.dom.Element; |
| 22 | +import org.w3c.dom.NamedNodeMap; |
| 23 | +import org.w3c.dom.Node; |
| 24 | +import org.w3c.dom.NodeList; |
| 25 | +import org.xml.sax.SAXException; |
| 26 | + |
| 27 | +public class A2ZXmlReader |
| 28 | +{ |
| 29 | + private String urlPath; |
| 30 | + private Document doc; |
| 31 | + private Element root; |
| 32 | + |
| 33 | + public A2ZXmlReader(String name) throws IOException { |
| 34 | + urlPath = name; |
| 35 | + try { |
| 36 | + createDocument(); |
| 37 | + } catch (ParserConfigurationException e) { |
| 38 | + e.printStackTrace(); |
| 39 | + } catch (SAXException e) { |
| 40 | + e.printStackTrace(); |
| 41 | + } catch (IOException e) { |
| 42 | + e.printStackTrace(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + public void createDocument() throws ParserConfigurationException, SAXException, IOException { |
| 47 | + InputStream is = openStream(urlPath); |
| 48 | + DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); |
| 49 | + DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); |
| 50 | + doc = docBuilder.parse(is); |
| 51 | + doc.getDocumentElement().normalize(); // // strips out empty "text" nodes? |
| 52 | + |
| 53 | + root = doc.getDocumentElement(); |
| 54 | + } |
| 55 | + |
| 56 | + public Element getRoot() { |
| 57 | + return root; |
| 58 | + } |
| 59 | + |
| 60 | + // This method recursively traverses the XML Tree (starting from currElement) |
| 61 | + public void traverseXML (Node currNode) |
| 62 | + { |
| 63 | + // If it's an Element, spit out the Name |
| 64 | + if (currNode.getNodeType() == Node.ELEMENT_NODE) { |
| 65 | + System.out.print(currNode.getNodeName() + ": "); |
| 66 | + // If it's a "Text" node, take the value |
| 67 | + // These will come one after another and therefore appear in the right order |
| 68 | + } else if (currNode.getNodeType() == Node.TEXT_NODE) { |
| 69 | + System.out.println(currNode.getNodeValue().trim()); |
| 70 | + } |
| 71 | + |
| 72 | + // Display any attributes |
| 73 | + if (currNode.hasAttributes()) { |
| 74 | + NamedNodeMap attributes = currNode.getAttributes(); |
| 75 | + for (int i = 0; i < attributes.getLength(); i++) { |
| 76 | + Node attr = attributes.item(i); |
| 77 | + System.out.println(" " + attr.getNodeName() + ": " + attr.getNodeValue()); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Check any children |
| 82 | + if (currNode.hasChildNodes()) { |
| 83 | + // Get the list of children |
| 84 | + NodeList children = currNode.getChildNodes(); |
| 85 | + // Go through all the chilrden |
| 86 | + for (int i = 0; i < children.getLength(); i++) { |
| 87 | + // Search each Node |
| 88 | + Node n = children.item(i); |
| 89 | + traverseXML(n); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + // This method recursively searches for an Element (starting from currElement) with the name elementName |
| 95 | + public Element findElement(Element currElement, String elementName) |
| 96 | + { |
| 97 | + // So far we've found nothing, i.e. null |
| 98 | + Element found = null; |
| 99 | + // If the current element is what we want, hooray we are done! |
| 100 | + if (currElement.getTagName().equals(elementName)) { |
| 101 | + found = currElement; |
| 102 | + // Otherwise, if there are children, let's check those |
| 103 | + } else if (currElement.hasChildNodes()) { |
| 104 | + // Get the list of children |
| 105 | + NodeList children = currElement.getChildNodes(); |
| 106 | + // As long as we haven't found it (i.e. found is still null) |
| 107 | + for (int i = 0; i < children.getLength() && found == null; i++) { |
| 108 | + // Search each Element type node |
| 109 | + Node n = children.item(i); |
| 110 | + if (n.getNodeType() == Node.ELEMENT_NODE) { |
| 111 | + Element e = (Element) n; |
| 112 | + found = findElement(e, elementName); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + return found; |
| 117 | + } |
| 118 | + |
| 119 | + |
| 120 | + // Find all elements |
| 121 | + public void fillArrayList(Element currElement, String elementName, ArrayList a) { |
| 122 | + // So far we've found nothing, i.e. null |
| 123 | + Element found = null; |
| 124 | + // If the current element is what we want, hooray we are done! |
| 125 | + if (currElement.getTagName().equals(elementName)) { |
| 126 | + found = currElement; |
| 127 | + // Otherwise, if there are children, let's check those |
| 128 | + } else if (currElement.hasChildNodes()) { |
| 129 | + // Get the list of children |
| 130 | + NodeList children = currElement.getChildNodes(); |
| 131 | + // As long as we haven't found it (i.e. found is still null) |
| 132 | + for (int i = 0; i < children.getLength(); i++) { |
| 133 | + // Search each Element type node |
| 134 | + Node n = children.item(i); |
| 135 | + if (n.getNodeType() == Node.ELEMENT_NODE) { |
| 136 | + Element e = (Element) n; |
| 137 | + fillArrayList(e, elementName,a); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + if (found != null) a.add(found); |
| 142 | + } |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + private InputStream openStream(String urlpath) { |
| 150 | + InputStream stream = null; |
| 151 | + try { |
| 152 | + URL url = new URL(urlpath); |
| 153 | + stream = url.openStream(); |
| 154 | + return stream; |
| 155 | + } catch (MalformedURLException e) { |
| 156 | + System.out.println("Something's wrong with the URL: "+ urlpath); |
| 157 | + e.printStackTrace(); |
| 158 | + } catch (IOException e) { |
| 159 | + System.out.println("there's a problem downloading from: "+ urlpath); |
| 160 | + e.printStackTrace(); |
| 161 | + } |
| 162 | + return stream; |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | + |
0 commit comments