tags:

views:

42

answers:

2
<message priority="info">PARAMETRI:</message>
<message priority="info">vrednost: 2.0</message>
<message priority="info">rank: 0.75</message>
−
<message priority="info">
objekt: irc.kis.model.pomozniRazredi.CasovniInterval.CasovniInterval(Date, Date)
</message>
<message priority="info">iid_tipa: 3</message>
<message priority="info">iid_metrike: 14</message>
<message priority="info">iid_izracuna: 140</message>
<message priority="info">done in 205776 ms</message>
<message priority="info">---------</message>
<message priority="info">Indeksi kakovosti</message>
<message priority="info">QI01: 3.9249</message>
<message priority="info">QI02: 4.0335</message>
<message priority="info">QI03: 4.0966</message>
<message priority="info">QI04: 4.3823</message>
<message priority="info">---------</message>
<message priority="info">QI05: 3.9401</message>
<message priority="info">QI06: 4.2479</message>
<message priority="info">QI07: 4.4984</message>
<message priority="info">QI08: 4.3534</message>
<message priority="info">QI09: 3.8455</message>
<message priority="info">QI10: 4.0195</message>
<message priority="info">QI11: 4.6222</message>

this is my xml log. Can i with java SAXParser get out just

Indeksi kakovosti
QI01: 3.9249
QI02: 4.0335
QI03: 4.0966
QI04: 4.3823

anything between ----

If yes how?

+1  A: 

It must be something like this.

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();

    DefaultHandler handler = new DefaultHandler() {
        boolean indexes;

        public void characters(char ch[], int start, int length)
                throws SAXException {
            String value = new String(ch, start, length);
            boolean changeState = value.startsWith("---");//change this, as you need
            if (!changeState && indexes){
                System.out.println(new String(ch, start, length));
            }
            if(changeState) indexes = !indexes;
        }

    };
    parser.parse(PATH_TO_FILE, handler);

But you document have to be well formed(with root element and without unknown characters like in fourth row).

Stas
thx for helping
senzacionale
@senzacionale : welcome. thx for slovenian language lesson)
Stas
+1  A: 

Yes, you can do that (assuming your xml is well-formed). You would have to create a ContentHandler, with a counter instance variable to tell how many of the --------- delimiters you've found so far. I wouldn't use characters() to do this because characters() can be called multiple times, I would buffer the text read using characters() and I would use endElement() to read the final text and test and increment the counters. So the ContentHandler would look like:

DefaultHandler hander = new DefaultHander() {
    private String marker = "---------";
    private int markerCount = 0;

    private java.io.CharArrayWriter buffer = new java.io.CharArrayWriter();

    public void characters(char ch[], int start, int length) {
        buffer.append(ch, start, length);
    }   

    public void endElement( String namespaceURI, String localName, String qName ) {
        String elementText = buffer.toString();
        if (elementText.startsWith(marker) {
            markerCount += 1;
        }
        else if (markerCount == 1) {
            System.out.println(elementText);
        }
        buffer.reset();
    }
};
Nathan Hughes
thx for helping
senzacionale