1 package net.osdn.gokigen.a01d.camera.utils;
3 import android.util.Log;
4 import android.util.Xml;
6 import org.xmlpull.v1.XmlPullParser;
7 import java.io.StringReader;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.LinkedList;
11 import java.util.List;
14 import androidx.annotation.NonNull;
16 public class XmlElement
18 private static final String TAG = XmlElement.class.getSimpleName();
19 private static final XmlElement NULL_ELEMENT = new XmlElement();
21 private String tagName = "";
22 private String tagValue;
24 private LinkedList<XmlElement> childElements;
25 private Map<String, String> attributes;
26 private XmlElement parentElement;
30 //Log.v(TAG, "XmlElement()");
32 childElements = new LinkedList<>();
33 attributes = new HashMap<>();
37 public XmlElement getParent()
39 return (parentElement);
41 public String getTagName()
43 //Log.v(TAG, "XmlElement Tag [" + tagName + "]");
47 private void setTagName(String name)
52 public String getValue()
54 //Log.v(TAG, "XmlElement Value [" + tagValue + "]");
57 private void setValue(String value)
62 private void putChild(XmlElement childItem)
64 childElements.add(childItem);
65 childItem.setParent(this);
68 public XmlElement findChild(String name)
70 for (final XmlElement child : childElements)
72 if (child.getTagName().equals(name))
77 return (new XmlElement());
80 public List<XmlElement> findChildren(String name)
82 final List<XmlElement> tagItemList = new ArrayList<>();
83 for (final XmlElement child : childElements)
85 if (child.getTagName().equals(name))
87 tagItemList.add(child);
93 private void setParent(XmlElement parent)
95 parentElement = parent;
98 private void putAttribute(String name, String value)
100 attributes.put(name, value);
103 public String getAttribute(String name, String defaultValue)
105 String ret = attributes.get(name);
113 private static XmlElement parse(XmlPullParser xmlPullParser)
115 XmlElement rootElement = XmlElement.NULL_ELEMENT;
118 XmlElement parsingElement = XmlElement.NULL_ELEMENT;
122 switch (xmlPullParser.next())
124 case XmlPullParser.START_DOCUMENT:
125 Log.v(TAG, "------- START DOCUMENT -----");
127 case XmlPullParser.START_TAG:
128 final XmlElement childItem = new XmlElement();
129 childItem.setTagName(xmlPullParser.getName());
130 if (parsingElement == XmlElement.NULL_ELEMENT) {
131 rootElement = childItem;
133 parsingElement.putChild(childItem);
135 parsingElement = childItem;
138 for (int i = 0; i < xmlPullParser.getAttributeCount(); i++)
140 parsingElement.putAttribute(xmlPullParser.getAttributeName(i), xmlPullParser.getAttributeValue(i));
144 case XmlPullParser.TEXT:
145 parsingElement.setValue(xmlPullParser.getText());
148 case XmlPullParser.END_TAG:
149 parsingElement = parsingElement.getParent();
152 case XmlPullParser.END_DOCUMENT:
153 Log.v(TAG, "------- END DOCUMENT -------");
164 rootElement = XmlElement.NULL_ELEMENT;
166 return (rootElement);
169 public static XmlElement parse(@NonNull String xmlStr)
173 XmlPullParser xmlPullParser = Xml.newPullParser();
174 xmlPullParser.setInput(new StringReader(xmlStr));
175 return parse(xmlPullParser);
181 return (new XmlElement());