下面列出了android.content.res.XmlResourceParser#getText() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private String ParseReleaseTag(XmlResourceParser aXml) throws XmlPullParserException, IOException {
String _Result = "<h1>Release: " + aXml.getAttributeValue(null, "version") + "</h1><ul>";
int eventType = aXml.getEventType();
while ((eventType != XmlPullParser.END_TAG) || (aXml.getName().equals("change"))) {
if ((eventType == XmlPullParser.START_TAG) && (aXml.getName().equals("change"))) {
eventType = aXml.next();
_Result = _Result + "<li>" + aXml.getText() + "</li>";
}
eventType = aXml.next();
}
_Result = _Result + "</ul>";
return _Result;
}
private String ParseReleaseTag(XmlResourceParser aXml) throws XmlPullParserException, IOException {
String _Result = "<h1>版本: " + aXml.getAttributeValue(null, "version") + "</h1><ul>";
int eventType = aXml.getEventType();
while ((eventType != XmlPullParser.END_TAG) || (aXml.getName().equals("change"))) {
if ((eventType == XmlPullParser.START_TAG) && (aXml.getName().equals("change"))) {
eventType = aXml.next();
_Result = _Result + "<li>" + aXml.getText() + "</li>";
}
eventType = aXml.next();
}
_Result = _Result + "</ul>";
return _Result;
}
private String ParseReleaseTag(XmlResourceParser aXml) throws XmlPullParserException, IOException {
String _Result = "<h1>Release: " + aXml.getAttributeValue(null, "version") + "</h1><ul>";
int eventType = aXml.getEventType();
while ((eventType != XmlPullParser.END_TAG) || (aXml.getName().equals("change"))) {
if ((eventType == XmlPullParser.START_TAG) && (aXml.getName().equals("change"))) {
eventType = aXml.next();
_Result = _Result + "<li>" + aXml.getText() + "</li>";
}
eventType = aXml.next();
}
_Result = _Result + "</ul>";
return _Result;
}
private String ParseReleaseTag(XmlResourceParser aXml) throws XmlPullParserException, IOException {
String _Result = "<h1>版本: " + aXml.getAttributeValue(null, "version") + "</h1><ul>";
int eventType = aXml.getEventType();
while ((eventType != XmlPullParser.END_TAG) || (aXml.getName().equals("change"))) {
if ((eventType == XmlPullParser.START_TAG) && (aXml.getName().equals("change"))) {
eventType = aXml.next();
_Result = _Result + "<li>" + aXml.getText() + "</li>";
}
eventType = aXml.next();
}
_Result = _Result + "</ul>";
return _Result;
}
public static Map<String,String> getHashMapResource(Context c, int hashMapResId) {
Map<String,String> map = null;
XmlResourceParser parser = c.getResources().getXml(hashMapResId);
String key = null, value = null;
try {
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_DOCUMENT) {
Log.d("utils","Start document");
} else if (eventType == XmlPullParser.START_TAG) {
if (parser.getName().equals("map")) {
boolean isLinked = parser.getAttributeBooleanValue(null, "linked", false);
map = isLinked ? new LinkedHashMap<String, String>() : new HashMap<String, String>();
} else if (parser.getName().equals("entry")) {
key = parser.getAttributeValue(null, "key");
if (null == key) {
parser.close();
return null;
}
}
} else if (eventType == XmlPullParser.END_TAG) {
if (parser.getName().equals("entry")) {
map.put(key, value);
key = null;
value = null;
}
} else if (eventType == XmlPullParser.TEXT) {
if (null != key) {
value = parser.getText();
}
}
eventType = parser.next();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return map;
}
private void initLocalConfig() throws Exception {
XmlResourceParser xmlResourceParser = mContext.getResources().getXml(R.xml.config_defaults);
int eventType = -1;
String key = null;
String value = null;
mLocalConfig = new HashMap<>();
while (eventType != xmlResourceParser.END_DOCUMENT) {
if (eventType == xmlResourceParser.START_TAG) {
if (xmlResourceParser.getName().equals("key")) {
// Key and value should be empty
if (key != null || value != null) {
throw new IllegalStateException("Invalid local config: bad key-value state");
}
// Go to next element and verify
eventType = xmlResourceParser.next();
if (eventType != xmlResourceParser.TEXT) {
throw new IllegalStateException("Invalid local config: error when getting key");
}
// Get key and validate
key = xmlResourceParser.getText();
if (key == null) {
throw new IllegalStateException("Invalid local config: null key");
}
continue;
}
if (xmlResourceParser.getName().equals("value")) {
// Key should be set, value should be empty
if (key == null || value != null) {
throw new IllegalStateException("Invalid local config: bad key-value state");
}
// Go to next element and verify
eventType = xmlResourceParser.next();
if (eventType != xmlResourceParser.TEXT) {
throw new IllegalStateException("Invalid local config: error when getting value");
}
// Get value and validate
value = xmlResourceParser.getText();
if (value == null) {
throw new IllegalStateException("Invalid local config: null value");
}
// Store key-value pair
mLocalConfig.put(key, xmlResourceParser.getText());
// Reset key and value
key = null;
value = null;
}
}
eventType = xmlResourceParser.next();
}
}
static HashMap<String, String> getDefaultsFromXml(Context context, int resourceId) {
HashMap<String,String> defaultsMap = new HashMap<>();
try {
Resources resources = context.getResources();
if (resources == null) {
Log.e("ProductConfig", "Could not find the resources of the current context while trying to set defaults from an XML.");
return defaultsMap;
}
XmlResourceParser xmlParser = resources.getXml(resourceId);
String curTag = null;
String key = null;
String value = null;
for (int eventType = xmlParser.getEventType(); eventType != XmlPullParser.END_DOCUMENT; eventType = xmlParser.next()) {
if (eventType == XmlPullParser.START_TAG) {
curTag = xmlParser.getName();
} else if (eventType != XmlPullParser.END_TAG) {
if (eventType == XmlPullParser.TEXT && curTag != null) {
byte tagType = -1;
switch (curTag) {
case XML_TAG_KEY:
tagType = XML_TAG_TYPE_KEY;
break;
case XML_TAG_VALUE:
tagType = XML_TAG_TYPE_VALUE;
}
switch (tagType) {
case XML_TAG_TYPE_KEY:
key = xmlParser.getText();
break;
case XML_TAG_TYPE_VALUE:
value = xmlParser.getText();
break;
default:
Log.w(LOG_TAG_PRODUCT_CONFIG, "Encountered an unexpected tag while parsing the defaults XML.");
}
}
} else {
if (xmlParser.getName().equals(XML_TAG_ENTRY)) {
if (key != null && value != null) {
defaultsMap.put(key, value);
} else {
Log.w(LOG_TAG_PRODUCT_CONFIG, "An entry in the defaults XML has an invalid key and/or value tag.");
}
key = null;
value = null;
}
curTag = null;
}
}
} catch (IOException | XmlPullParserException var11) {
Log.e("ProductConfig", "Encountered an error while parsing the defaults XML file.", var11);
}
return defaultsMap;
}
public static List<License> Parse(XmlResourceParser parser)
throws XmlPullParserException, IOException {
List<License> licenses = new ArrayList<License>();
int event = parser.getEventType();
String name = null;
String type = null;
String license = null;
while (event != XmlResourceParser.END_DOCUMENT) {
if (event == XmlResourceParser.START_TAG) {
if (!parser.getName().equals(TAG_ROOT)
&& !parser.getName().equals(TAG_CHILD))
throw new XmlPullParserException(
"Error in xml: tag isn't '" + TAG_ROOT + "' or '"
+ TAG_CHILD + "' at line:"
+ parser.getLineNumber());
name = parser.getAttributeValue(null, ATTR_NAME);
type = parser.getAttributeValue(null, ATTR_TYPE);
} else if (event == XmlResourceParser.TEXT) {
license = parser.getText();
} else if (event == XmlResourceParser.END_TAG) {
if (name != null && type != null && license != null
&& !parser.getName().equals(TAG_ROOT)) {
if (type.equals(VALUE_FILE)) {
licenses.add(new License(name, License.TYPE_FILE,
license));
System.out.println(name);
} else if (type.equals(VALUE_LIBRARY)) {
licenses.add(new License(name, License.TYPE_LIBRARY,
license));
System.out.println(name);
} else {
throw new XmlPullParserException(
"Error in xml: 'type' isn't valid at line:"
+ parser.getLineNumber());
}
} else if (name == null) {
throw new XmlPullParserException(
"Error in xml: doesn't contain a 'name' at line:"
+ parser.getLineNumber());
} else if (type == null) {
throw new XmlPullParserException(
"Error in xml: doesn't contain a 'type' at line:"
+ parser.getLineNumber());
} else if (license == null){
throw new XmlPullParserException(
"Error in xml: doesn't contain a 'license text' at line:"
+ parser.getLineNumber());
}
}
event = parser.next();
}
parser.close();
return licenses;
}