简体   繁体   中英

XmlPullParser replace text in tags

eg Client: xmlpullparser extracts "Client: " successfully but I want to replace "Client: " with "HELLO WORLD" and write it into the xml file i just read from. How do i do this?

    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;
}
}

Why don't you use the DOM model instead? Using the Document you should be able to modify the dom and write it back easily.

See here for an example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM