Search code examples
parsingjava-mexml-parsingkxml

How to reuse j2me kxml parser?


I am useing kxml parser for my j2me application. I am reading the file from phone memory and parsing the xml file to display the data(have various level of filter). On each filter i need to read the data from this file. For first time i created the parser and every time i re-assign this parser1(reference-original) to the paerser2(used to parse data). For first time i got the correct answer, but second time i haven't got the file content it shows null as data.

Here is my code:

FileConnection fc = (FileConnection)Connector.open(rmsObj.rmsData.elementAt(0).toString());
InputStream in = fc.openInputStream();
InputStreamReader is = new InputStreamReader(in);
commonAppObj.externParser = new XmlParser(is);

commonAppObj class file.

protected void fileread() {
    try {
        if(externParser != null){
            parser = externParser;
            fileparser(parser);
        }else{
            InputStream in = this.getClass().getResourceAsStream(this.dataBase);
            InputStreamReader is = new InputStreamReader(in);
            parser = new XmlParser(is);
            fileparser(parser);
        }
    } catch (IOException ioe) {
    } finally {
        parser = null;
    }
}

private void fileparser(XmlParser parser){
    try {
        ParseEvent event = null;
        dataflag = 0;
        dataflagS = 0;
        System.out.println("findtags = " + findtags);
        while (((event = parser.read()).getType() != Xml.END_DOCUMENT) && (dataflag != 1)) {
            if (event.getType() == Xml.START_TAG) {
                String name = event.getName();
                if (name != null && name.equals(findtags)) {
                    dataflag = 0;
                    parseAddressTag(parser);
                }
                name = null;
            }
            event = null;
        }
    } catch (IOException ioe) {
    } finally {
        parser = null;
    }
}
}

Solution

  • If your InputStream returns true in a call to markSupported you may reset it at the end of fileparser method, but first you need to call mark right after creating it.

    if (in.markSupported()) {
      in.mark(in.available());
    }