XmlPullParser: get inner text including XML tags

丶灬走出姿态 提交于 2019-12-03 07:05:49

This method should about cover it, but does not deal with singleton tags or namespaces.

public static String getInnerXml(XmlPullParser parser)
        throws XmlPullParserException, IOException {
    StringBuilder sb = new StringBuilder();
    int depth = 1;
    while (depth != 0) {
        switch (parser.next()) {
        case XmlPullParser.END_TAG:
            depth--;
            if (depth > 0) {
                sb.append("</" + parser.getName() + ">");
            }
            break;
        case XmlPullParser.START_TAG:
            depth++;
            StringBuilder attrs = new StringBuilder();
            for (int i = 0; i < parser.getAttributeCount(); i++) {
                attrs.append(parser.getAttributeName(i) + "=\""
                        + parser.getAttributeValue(i) + "\" ");
            }
            sb.append("<" + parser.getName() + " " + attrs.toString() + ">");
            break;
        default:
            sb.append(parser.getText());
            break;
        }
    }
    String content = sb.toString();
    return content;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!