| import static de.aja.lotus.LotusScriptEmulator.len; |
| import static de.aja.lotus.LotusScriptEmulator.redimPreserve; |
| import static de.aja.lotus.LotusScriptEmulator.ubound; |
| |
| import java.io.FileNotFoundException; |
| import java.io.IOException; |
| import java.io.StringReader; |
| |
| import org.xml.sax.Attributes; |
| import org.xml.sax.InputSource; |
| import org.xml.sax.SAXException; |
| import org.xml.sax.XMLReader; |
| import org.xml.sax.helpers.DefaultHandler; |
| import org.xml.sax.helpers.XMLReaderFactory; |
| |
| public class LocationXmlContentHandler extends DefaultHandler { |
| |
| boolean ok; |
| boolean insideLocation; |
| boolean insideData; |
| |
| String data = ""; |
| String tagName; |
| String newValue; |
| |
| String[] treffer = new String[0]; |
| |
| |
| |
| |
| public void startElement(String uri, String localName, String qName, |
| Attributes atts) throws SAXException { |
| tagName = localName; |
| if (insideLocation) { |
| if (len(data) > 0) { |
| data = data + ":"; |
| } |
| if (ok) { |
| data = data + localName + "="; |
| newValue = ""; |
| insideData = true; |
| } |
| } |
| |
| |
| if (localName.equals("Location")) { |
| ok = true; |
| |
| data = ""; |
| insideLocation = true; |
| } |
| |
| } |
| |
| |
| public void characters(char[] ch, int start, int length) |
| throws SAXException { |
| |
| if (insideData && insideLocation && ok) { |
| newValue = newValue + new String(ch, start, length); |
| |
| } |
| } |
| |
| |
| public void endElement(String uri, String localName, String qName) |
| throws SAXException { |
| if (insideData) { |
| |
| |
| if (localName.equals("ID1") && (!"AAA".equals(newValue))) { |
| ok = false; |
| } |
| if (localName.equals("ID2") && (!"111".equals(newValue))) { |
| ok = false; |
| } |
| |
| if (ok) { |
| data = data + newValue; |
| |
| } |
| } |
| |
| insideData = false; |
| |
| if (localName.equals("Location")) { |
| if (ok) { |
| |
| int ubound = ubound(treffer); |
| treffer = redimPreserve(treffer, ubound + 1); |
| treffer[ubound] = data; |
| } |
| |
| insideLocation = false; |
| |
| } |
| |
| } |
| |
| public static void main(String[] args) { |
| |
| String xmlZeugs = "<?xml version='1.0'?>" |
| + "<Locations>" |
| + " <Location>" |
| + " <ID1>AAA</ID1><ID2>111</ID2><Date>01.01.2000</Date><Status>A</Status><MoreStuffHere>...</MoreStuffHere>" |
| + " </Location>" |
| + " <Location>" |
| + " <ID1>AAA</ID1><ID2>111</ID2><Date>02.02.2000</Date><Status>A</Status><MoreStuffHere>...</MoreStuffHere>" |
| + " </Location>" |
| + " <Location>" |
| + " <ID1>AAA</ID1><ID2>222</ID2><Date>05.05.2000</Date><Status>A</Status><MoreStuffHere>...</MoreStuffHere>" |
| + " </Location>" |
| + " <Location>" |
| + " <ID1>AAA</ID1><ID2>333</ID2><Date>03.03.2000</Date><Status>A</Status><MoreStuffHere>...</MoreStuffHere>" |
| + " </Location>" |
| + " <Location>" |
| + " <ID1>BBB</ID1><ID2>111</ID2><Date>07.07.2000</Date><Status>A</Status><MoreStuffHere>...</MoreStuffHere>" |
| + " </Location>" |
| + " <Location>" |
| + " <ID1>BBB</ID1><ID2>111</ID2><Date>01.01.2000</Date><Status>A</Status><MoreStuffHere>...</MoreStuffHere>" |
| + " </Location>" |
| + " <Location>" |
| + " <ID1>BBB</ID1><ID2>222</ID2><Date>09.09.2000</Date><Status>I</Status><MoreStuffHere>...</MoreStuffHere>" |
| + " </Location>" |
| + " <Location>" |
| + " <ID1>CCC</ID1><ID2>111</ID2><Date>10.10.2000</Date><Status>A</Status><MoreStuffHere>...</MoreStuffHere>" |
| + " </Location>" |
| + " <Location>" |
| + " <ID1>CCC</ID1><ID2>222</ID2><Date>13.12.2000</Date><Status>A</Status><MoreStuffHere>...</MoreStuffHere>" |
| + " </Location>" + "</Locations>"; |
| StringReader reader = null; |
| try { |
| |
| XMLReader xmlReader = XMLReaderFactory.createXMLReader(); |
| |
| |
| reader = new StringReader(xmlZeugs); |
| InputSource inputSource = new InputSource(reader); |
| |
| |
| |
| LocationXmlContentHandler contentHandler = new LocationXmlContentHandler(); |
| |
| xmlReader.setContentHandler(contentHandler); |
| |
| |
| xmlReader.parse(inputSource); |
| |
| System.out.println("treffer"); |
| for (String hit : contentHandler.treffer) { |
| System.out.println(hit); |
| } |
| } catch (FileNotFoundException e) { |
| e.printStackTrace(); |
| } catch (IOException e) { |
| e.printStackTrace(); |
| } catch (SAXException e) { |
| e.printStackTrace(); |
| } finally { |
| if (reader != null) { |
| reader.close(); |
| } |
| } |
| } |
| |
| } |
| |