SAXParser解析数据的问题

发布于 2024-09-26 11:18:15 字数 1594 浏览 1 评论 0原文

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

这是我的 xml 日志。我可以用 java SAXParser 得到

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

----

如果是的话怎么办?

<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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2024-10-03 11:18:15

是的,您可以这样做(假设您的 xml 格式正确)。您必须创建一个 ContentHandler,并使用计数器实例变量来告诉您到目前为止已找到多少个 --------- 分隔符。

不要使用characters()来执行此操作,因为characters()可以被多次调用。相反,使用字符()缓冲读取的文本,使用endElement()读取最终文本并测试和递增计数器。

所以 ContentHandler 看起来像:

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

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();
    }
};
垂暮老矣 2024-10-03 11:18:15

一定是这样的。

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

但是您的文档必须格式良好(具有根元素并且没有未知字符,如第四行)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文