SAXParser problem with parsing data
Asked Answered
B

2

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?

Branson answered 6/10, 2010 at 10:28 Comment(0)
D
0

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 your document has to be well formed (with root element and without unknown characters like in fourth row).

Damascene answered 6/10, 2010 at 12:19 Comment(1)
The code example is incorrect, you shouldn't use the characters method to do this kind of thing. The characters method cannot be relied on to deliver all the element content in one call.Heartbeat
H
1

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.

Do not use characters() to do this, because characters() can be called multiple times. Instead buffer the text read using characters(), 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();
    }
};
Heartbeat answered 6/10, 2010 at 12:32 Comment(0)
D
0

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 your document has to be well formed (with root element and without unknown characters like in fourth row).

Damascene answered 6/10, 2010 at 12:19 Comment(1)
The code example is incorrect, you shouldn't use the characters method to do this kind of thing. The characters method cannot be relied on to deliver all the element content in one call.Heartbeat

© 2022 - 2024 — McMap. All rights reserved.