XmlPullParser替换标记中的文本

时间:2014-10-06 07:10:47

标签: java android

e.g。客户端:xmlpullparser提取"客户端:"成功,但我想取代"客户:"与" HELLO WORLD"并将其写入我刚读取的xml文件中。我该怎么做?

    public static ArrayList<String> extract_xml (String path) throws XmlPullParserException, IOException {
    ArrayList<String> xml_results = new ArrayList<String>();

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();

    InputStream open_xml = new FileInputStream(path);
    xpp.setInput(open_xml, null);

    int eventType = xpp.getEventType();
    String currentTag = null;
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            currentTag = xpp.getName();
        } else if(eventType == XmlPullParser.TEXT) {
            if("t".equals(currentTag) && xpp.getText().equals("Client: ")) {//tag to search for
                xml_results.add(xpp.getText());

            }
        }
        eventType = xpp.next();
    }
   return xml_results;
}
}

1 个答案:

答案 0 :(得分:0)

为什么不使用DOM模型呢?使用Document,您应该能够修改dom并轻松地将其写回。

有关示例,请参阅here