From e49a792fcd72617122738891de67a39b386cb365 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 27 Jan 2013 18:15:09 +1300 Subject: Add simple XML parser --- src/org/traccar/web/client/Traccar.java | 38 ++++++++++++++++++++++- src/org/traccar/web/shared/model/XmlParser.java | 40 +++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 src/org/traccar/web/shared/model/XmlParser.java (limited to 'src/org') diff --git a/src/org/traccar/web/client/Traccar.java b/src/org/traccar/web/client/Traccar.java index 8fa67ecd..c596972d 100644 --- a/src/org/traccar/web/client/Traccar.java +++ b/src/org/traccar/web/client/Traccar.java @@ -16,14 +16,50 @@ package org.traccar.web.client; import org.traccar.web.client.controller.LoginController; +import org.traccar.web.shared.model.XmlParser; import com.google.gwt.core.client.EntryPoint; +import com.google.gwt.xml.client.DOMException; +import com.google.gwt.xml.client.Document; +import com.google.gwt.xml.client.Node; +import com.google.gwt.xml.client.XMLParser; +import com.sencha.gxt.widget.core.client.box.AlertMessageBox; public class Traccar implements EntryPoint, LoginController.LoginHandler { @Override public void onModuleLoad() { - new LoginController().login(this); + //new LoginController().login(this); + + try { + String messageXml = "12"; + //parse the XML document into a DOM + String x = XmlParser.getElement(messageXml, "b"); + XmlParser.enumerateElements(messageXml); + + + + + + // find the sender's display name in an attribute of the tag + /*Node fromNode = messageDom.getElementsByTagName("from").item(0); + String from = ((Element)fromNode).getAttribute("displayName"); + fromLabel.setText(from); + + // get the subject using Node's getNodeValue() function + String subject = messageDom.getElementsByTagName("subject").item(0).getFirstChild().getNodeValue(); + subjectLabel.setText(subject); + + // get the message body by explicitly casting to a Text node + Text bodyNode = (Text)messageDom.getElementsByTagName("body").item(0).getFirstChild(); + String body = bodyNode.getData(); + bodyLabel.setText(body);*/ + + new AlertMessageBox("Info", "a=" + x).show(); + + } catch (DOMException e) { + new AlertMessageBox("Error", e.getMessage()).show(); + } } @Override diff --git a/src/org/traccar/web/shared/model/XmlParser.java b/src/org/traccar/web/shared/model/XmlParser.java new file mode 100644 index 00000000..f19c75a8 --- /dev/null +++ b/src/org/traccar/web/shared/model/XmlParser.java @@ -0,0 +1,40 @@ +package org.traccar.web.shared.model; + +import java.util.LinkedList; +import java.util.List; + +import com.google.gwt.regexp.shared.MatchResult; +import com.google.gwt.regexp.shared.RegExp; + +public class XmlParser { + + public static List enumerateElements(String input) { + List list = new LinkedList(); + + RegExp parser = RegExp.compile("<([^/<][^<]*)>", "g"); + MatchResult result = parser.exec(input); + while (result.getGroupCount() > 0) { + list.add(result.getGroup(1)); + result = parser.exec(input); + } + + return list; + } + + public static String getElement(String input, String key) { + + StringBuilder pattern = new StringBuilder(); + pattern.append("<").append(key).append(">"); + pattern.append("([^<]*)"); + pattern.append(""); + + RegExp parser = RegExp.compile(pattern.toString()); + MatchResult result = parser.exec(input); + if (result.getGroupCount() > 0) { + return result.getGroup(1); + } + + return null; + } + +} -- cgit v1.2.3