I have an XML (actually a String
) and I want to find all tags which contain attributes width
and height
and to modify their values.
XML example:
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://1.bp.blogspot.com/-b5iKjQ5ivZQ/VOoBX9NinU3232I/AAAAAAAADZU332/zq3apERWFms/s800/IMG_3426.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;">
<img border="0" height="426" src="http://1.bp.blogspot.com/-b5iKjQ5ivZQ/VOoBX9NinUI/AAAAAAAADZU/zq32323apERWFms/s800/IMG_3426.JPG" width="640" />
</a>
</div>
<div class="separator" style="clear: both; text-align: center;">
<iframe allowfullscreen="" frameborder="0" height="315" src="https://www.youtube.com/embed/sI9Qf7UmXl0" width="420"></iframe>
</div>
In the above example I want to modify:
- 426 and 640 values
- 560 and 315 values
I was able to identify some of the values using XmlPullParser
: the code below identifies 426 and 640 values, but not 560 and 315.
Also I do not know how to modify them in XML:
public void parse(String inputString) throws XmlPullParserException, IOException {
XmlPullParser parser = Xml.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(new StringReader(inputString));
parser.nextTag();
readXml(parser);
}
private void readXml(XmlPullParser parser) throws XmlPullParserException, IOException {
int eventType = parser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
break;
case XmlPullParser.END_TAG:
String h = parser.getAttributeValue(null, "height");
String w = parser.getAttributeValue(null, "width");
if (!TextUtils.isEmpty(h) && !TextUtils.isEmpty(w)) {
// TODO: need to change here h and w values in XML
}
break;
}
eventType = parser.next();
}
}