Skip to content

Instantly share code, notes, and snippets.

@abdullahriaz95
Created December 11, 2019 05:23
Show Gist options
  • Save abdullahriaz95/d6e85574e0a8618dce05a1a2ef4d4f14 to your computer and use it in GitHub Desktop.
Save abdullahriaz95/d6e85574e0a8618dce05a1a2ef4d4f14 to your computer and use it in GitHub Desktop.
JAVA STAX XML Parser (Working Perfectly) - an example xml is given in the code.
import android.os.Bundle;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
import java.util.Stack;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
/**
* Created by Abdullah Riaz on 12/3/19.
*/
public class ConfigParser {
private static final String TAG = MainActivity.class.getSimpleName();
private static ConfigParser configParser;
private boolean subBundleStarted = false;
private Bundle staxBundle = new Bundle();
private Stack<String> nameStack = new Stack<>();
private Stack<Bundle> bundleStack = new Stack<>();
private ConfigParser() {
}
public static ConfigParser getInstance() {
if (configParser == null) {
configParser = new ConfigParser();
}
return configParser;
}
public Bundle parseAndReturnBundle(String schema) {
try {
ByteArrayInputStream input = new ByteArrayInputStream(schema.getBytes("UTF-8"));
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader = factory.createXMLEventReader(input);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
switch (event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("parm")) {
Iterator<Attribute> attributes = startElement.getAttributes();
HashMap<String, String> hmap = new HashMap<>();
while (attributes.hasNext()) {
Attribute a = attributes.next();
hmap.put(a.getName().toString(), a.getValue());
}
if (Objects.equals(hmap.get("type"), "bundle")) {
nameStack.push(hmap.get("name"));
bundleStack.push(new Bundle());
subBundleStarted = true;
} else {
if (subBundleStarted) {
addToLastBundle(hmap);
} else {
addNormalValue(hmap);
}
}
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if (endElement.getName().getLocalPart().equalsIgnoreCase("bundle")) {
if (subBundleStarted && bundleStack.size() >= 2) {
addValuesToPreviousBundleInStacks();
} else {
staxBundle.putBundle(nameStack.pop(), bundleStack.pop());
if (bundleStack.empty() && nameStack.empty()) {
subBundleStarted = false;
}
}
}
break;
}
}
return staxBundle;
} catch (
Exception e) {
e.printStackTrace();
}
return null;
}
private void addValuesToPreviousBundleInStacks() {
Bundle b = bundleStack.pop();
String s = nameStack.pop();
Bundle toAddDataTo = bundleStack.pop();
toAddDataTo.putBundle(s, b);
bundleStack.push(toAddDataTo);
}
private void addToLastBundle(HashMap<String, String> hm) {
Bundle b = bundleStack.pop();
if (hm.get("type").equals("integer")) {
b.putInt(hm.get("name"), Integer.parseInt(hm.get("value")));
} else if (hm.get("type").equals("string")) {
b.putString(hm.get("name"), hm.get("value"));
}
bundleStack.push(b);
}
private void addNormalValue(HashMap<String, String> hm) {
if (hm.get("type").equals("integer")) {
staxBundle.putInt(hm.get("name"), Integer.parseInt(hm.get("value")));
} else if (hm.get("type").equals("string")) {
staxBundle.putString(hm.get("name"), hm.get("value"));
}
}
}
// <characteristic type="com.airwatch.android.androidwork.app:com.innowi.checoutappconfig" uuid="8aac143a-03b6-4bb7-a94d-079f5a8b6399">
// <parm name="SetupOEM" value="com.innowi" type="string" />
// <parm name="Notification" type="bundle">
// <bundle>
// <parm name="AppNotification" type="bundle">
// <bundle>
// <parm name="EnableNotificationForPackages" value="com.android.email , com.android.music" type="string" />
// <parm name="DisableNotificationForPackages" value="com.android.contacts , com.android.settings" type="string" />
// </bundle>
// </parm>
// </bundle>
// </parm>
// </characteristic>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment