01: package org.geotools.demo.xml;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05: import java.io.StringBufferInputStream;
06: import java.io.StringReader;
07:
08: import javax.xml.parsers.DocumentBuilder;
09: import javax.xml.parsers.DocumentBuilderFactory;
10:
11: import org.apache.xerces.util.DOMUtil;
12: import org.geotools.filter.FilterDOMParser;
13: import org.geotools.filter.FilterFilter;
14: import org.geotools.filter.FilterHandler;
15: import org.geotools.gml.GMLFilterDocument;
16: import org.geotools.gml.GMLFilterGeometry;
17: import org.opengis.filter.Filter;
18: import org.w3c.dom.Document;
19: import org.w3c.dom.Element;
20: import org.w3c.dom.Node;
21: import org.w3c.dom.NodeList;
22: import org.xml.sax.InputSource;
23: import org.xml.sax.SAXException;
24: import org.xml.sax.XMLReader;
25: import org.xml.sax.helpers.DefaultHandler;
26: import org.xml.sax.helpers.XMLReaderFactory;
27:
28: public class DOMExample {
29: static String xml = "<Filter xmlns:gml=\"http://www.opengis.net/gml\">"
30: + " <Overlaps>"
31: + " <PropertyName>testGeometry</PropertyName>"
32: + "<gml:Polygon srsName=\"http://www.opengis.net/gml/srs/EPSG#4326\">"
33: + "<gml:outerBoundaryIs>"
34: + "<gml:LinearRing>"
35: + "<gml:coordinates>0,0 0,10 10,10 10,0 0,0</gml:coordinates>"
36: + "</gml:LinearRing>"
37: + "</gml:outerBoundaryIs>"
38: + "</gml:Polygon>" + " </Overlaps>" + "</Filter>";
39:
40: public static void main(String args[]) throws Exception {
41: System.out.println("Parsing:" + xml);
42: StringReader reader = new StringReader(xml);
43: InputSource input = new InputSource(reader);
44:
45: DocumentBuilderFactory dbf = DocumentBuilderFactory
46: .newInstance();
47: DocumentBuilder db = dbf.newDocumentBuilder();
48: Document dom = db.parse(input);
49:
50: Filter filter = null;
51:
52: // first grab a filter node
53: NodeList nodes = dom.getElementsByTagName("Filter");
54:
55: for (int j = 0; j < nodes.getLength(); j++) {
56: Element filterNode = (Element) nodes.item(j);
57: NodeList list = filterNode.getChildNodes();
58: Node child = null;
59:
60: for (int i = 0; i < list.getLength(); i++) {
61: child = list.item(i);
62:
63: if ((child == null)
64: || (child.getNodeType() != Node.ELEMENT_NODE)) {
65: continue;
66: }
67:
68: filter = FilterDOMParser.parseFilter(child);
69: }
70: }
71: System.out.println("got:" + filter);
72: }
73:
74: }
|