如何读取 xml 部分而不将整个 XML 加载到内存
我有这个简化的 xml 文件,在生产中它会很大。我必须读取信息元素(id、发送者、接收者),并且我正在使用带有 XMLEventReader、XMLEvent jdk 类的流 API 成功完成此操作。我也在读取像车主一样的汽车数据,我必须将车主保存到数据库中,类似于信息数据。但是关于汽车数据还有更多内容。
我的任务的第二部分是获取每个汽车节点并将其作为 CLOB 保存到数据库中,每辆车一条记录。使用 XMLEventReader 我无法做到这一点,因为我没有完整的 xml 上下文,并且当前的汽车 XMLEvent 没有类似 toString 方法将整个 xml 部分序列化为文本的方法。我也不想自己重新创建 XML 结构。我想知道如何以最有效的方式做到这一点而不是将整个 XML 加载到内存中。也许读取信息应该是解析器的一部分,第二部分是查看特定节点并以某种方式将它们从流中删除,即使不读取汽车数据本身,并将每辆车保存到数据库中?在 Java 8+ 中是否有可能?
<message>
<info>
<id>123</id>
<sender>
<name>dorothy</name>
</sender>
<recipient>
<name>dorothy</name>
</recipient>
</info>
<car>
<owner>adam1</owner>
</car>
<car>
<owner>adam2</owner>
</car>
<car>
<owner>adam3</owner>
</car>
</message>
</root>
编辑:
我可以将 xml 分为几部分:
FileInputStream fis = new FileInputStream(entry.toFile());
XMLStreamReader xmlr = xmlif.createXMLStreamReader(fis);
int eventType = xmlr.getEventType();
printEventType(eventType);
while (xmlr.hasNext()) {
eventType = xmlr.next();
if (xmlr.isStartElement() && xmlr.getLocalName().equals("car")) {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
File file = new File("out" + System.currentTimeMillis() + ".xml");
t.transform(new StAXSource(xmlr), new StreamResult(file));
//printAttributes(xmlr);
//
} else if (xmlr.isStartElement()){
printName(xmlr, eventType); //elements of car nodes are not present, propalby consumed by above if statement
}
I have this simplified xml file, in production it will be large one. I have to read info element (id, sender, recipient) and I am doing it successfully using stream API with XMLEventReader, XMLEvent jdk classes. I am also reading car data like owner, I have to save owner to database, similar to info data. But there is more about car data..
Second part of my task is to take each car node and save it to database as CLOB, one record per car. With XMLEventReader I am not able to do it because I don't have whole xml context and current car XMLEvent does not have something like toString method serializing the whole xml part to text. I would also not like to recreate XML structure by myself. I am wondering how to do that in most efficient way and not load the whole XML into memory. Maybe reading info should be one part and second part of the parser would be to peek specific nodes and cut them out from stream somehow even without reading the car data itself, and save each car to database ? Is it possible somehow in Java 8+ ?
<message>
<info>
<id>123</id>
<sender>
<name>dorothy</name>
</sender>
<recipient>
<name>dorothy</name>
</recipient>
</info>
<car>
<owner>adam1</owner>
</car>
<car>
<owner>adam2</owner>
</car>
<car>
<owner>adam3</owner>
</car>
</message>
</root>
EDIT:
I am able to divide xml into parts:
FileInputStream fis = new FileInputStream(entry.toFile());
XMLStreamReader xmlr = xmlif.createXMLStreamReader(fis);
int eventType = xmlr.getEventType();
printEventType(eventType);
while (xmlr.hasNext()) {
eventType = xmlr.next();
if (xmlr.isStartElement() && xmlr.getLocalName().equals("car")) {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
File file = new File("out" + System.currentTimeMillis() + ".xml");
t.transform(new StAXSource(xmlr), new StreamResult(file));
//printAttributes(xmlr);
//
} else if (xmlr.isStartElement()){
printName(xmlr, eventType); //elements of car nodes are not present, propalby consumed by above if statement
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论