java - 使用 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上找到的报告。
有人可以给一些建议吗?
解决方案
我也面临同样的问题。在网上搜索了一段时间后,我找到了以下解决方案
由于 xml 值具有 CDATA,因此事件类型将为 XMLEvent.CDATA 而不是 XMLEvent.CHARACTERS
- https://docs.oracle.com/javase/8/docs/api/javax/xml/stream/events/XMLEvent.html
- https://github.com/dturanski/stax-xml-parser/blob/master/src/main/java/staxparser/xml/CDataContentExtractor.java
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
推荐阅读
- android - 在 SQLite/Room 上创建数据库以容纳每个会话的传感器数据?
- python - Python数据框 - 每年的条件平均值
- apache-spark - Spark 作业每天因 java.io.OptionalDataException 失败一次
- graphviz - 当rankdir为LR时,为什么同一rank的节点是从下到上而不是从上到下?
- c# - 在 HeroCard 的 CardImage 中添加图片 URL
- android - 蓝牙低功耗 4.1。频道强迫
- azure - 如何在 Azure 测试计划中运行自动化测试
- javascript - 浏览器缩放上的 ScaleX() 问题
- maven - 使用 Maven 在 WebSphere Liberty 19.0.0.8 服务器中实现 CI/CD
- android - 向具有 root 访问权限的第三方应用授予不可更改的权限