首页 > 解决方案 > 使用 Stax 解析器解析 XML 1.1 文档时出错

问题描述

我正在尝试解析 Burp Suite XML 导出。我使用过 Stax 解析器和 XPath 解析器。但我越来越

Location: /py/message/viewBill.pt [id parameter]]]></location>
<severity>High</severity>
<confidence>Certain</confidence>
<issueBackground><![CDATA[Reflected 
javax.xml.stream.XMLStreamException: ParseError at [row,col]:[66,2357]
Message: The element type "location" must be terminated by the matching end-tag "< /location>".
    at com.sun.org.apache.xerces.internal.impl.XMLStreamReaderImpl.next(XMLStreamReaderImpl.java:604)
    at com.sun.xml.internal.stream.XMLEventReaderImpl.nextEvent(XMLEventReaderImpl.java:83)

一直出错。虽然有一个结束标签,但解析器找不到它。我的代码是:

XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader = factory.createXMLEventReader(new StringReader(str));

while (eventReader.hasNext()) {
    XMLEvent event = eventReader.nextEvent();

    switch (event.getEventType()) {

        case XMLStreamConstants.START_ELEMENT:
            StartElement startElement = event.asStartElement();
            String qName = startElement.getName().getLocalPart();

            if (qName.equalsIgnoreCase(ISSUES)) {
                issues = true;
            } else if (qName.equalsIgnoreCase(ISSUE)) {
                issue = true;
            } else if (qName.equalsIgnoreCase(NAME)) {
                name = true;
            } else if (qName.equalsIgnoreCase(HOST)) {
                host = true;
            } else if (qName.equalsIgnoreCase(PATH)) {
                path = true;
            } else if (qName.equalsIgnoreCase(LOCATION)) {
                location = true;
            } else if (qName.equalsIgnoreCase(SEVERITY)) {
                severity = true;
            }
            break;

        case XMLStreamConstants.CHARACTERS:
            Characters characters = event.asCharacters();
            if (name) {
                System.out.println("Name: " + characters.getData());
                name = false;
            } else if (host) {
                System.out.println("Host: " + characters.getData());
                host = false;
            } else if (path) {
                System.out.println("Path: " + characters.getData());
                path = false;
            } else if (location) {
                System.out.println("Location: " + characters.getData());
                location = false;
            } else if (severity) {
                System.out.println("severity: " + characters.getData());
                severity = false;
            }
            break;

        case XMLStreamConstants.END_ELEMENT:
            EndElement endElement = event.asEndElement();
            String endElementName = endElement.getName().getLocalPart();

            if (endElementName.equalsIgnoreCase(ISSUE)) {
                issue = false;
            } else if (endElementName.equalsIgnoreCase(NAME)) {
                name = false;
            } else if (endElementName.equalsIgnoreCase(HOST)) {
                host = false;
            } else if (endElementName.equalsIgnoreCase(PATH)) {
                path = false;
            } else if (endElementName.equalsIgnoreCase(LOCATION)) {
                location = false;
            } 
            break;
    }
}

我正在尝试解析我在https://github.com/mtesauro/parse-tools/blob/master/examples/brief-burp-export.xml上找到的报告。

有人可以给一些建议吗?

标签: javaxpathstax

解决方案


我也面临同样的问题。在网上搜索了一段时间后,我找到了以下解决方案

由于 xml 值具有 CDATA,因此事件类型将为 XMLEvent.CDATA 而不是 XMLEvent.CHARACTERS

Switch(reader.hasNext()) {
case TAG:
    eventType = reader.next();
    if (eventType == XMLEvent.CDATA || eventType == XMLEvent.CHARACTERS) {
        System.out.println(reader.getText());
    }
    break;
........
}

我还添加了以下依赖项。我不确定这种依赖有什么帮助,但如果没有这种依赖,我们将得到上面提到的相同异常。

但是在添加这个依赖问题之后得到了解决。

<dependency>
    <groupId>com.fasterxml.woodstox</groupId>
    <artifactId>woodstox-core</artifactId>
    <version>5.0.0</version>
</dependency>

https://github.com/FasterXML/woodstox https://mvnrepository.com/artifact/com.fasterxml.woodstox/woodstox-core/5.0.0


推荐阅读