HY, 我有一个(非常)大的XML文件(100GB)和一个foo列表,我想将它转换成一个流,就像它们引入对象的java 8一样:
是否有lib或代码示例的想法?
开头:
<foos>
<foo>...</foo>
<foo>...</foo>
</foos>
最后:
Stream<Foo> foosStream = ????("foo.xml")
streamFoos.forEach(foo->foo.doFooStuffs());
编辑: @Pierre谢谢,这是您的解决方案的实施:
try {
XMLEventReader reader = XMLInputFactory.newInstance().
createXMLEventReader(stream);
final Unmarshaller unmarshaller = JAXBContext.newInstance(XXXXX.class).createUnmarshaller();
Iterator<XXXXX> it = new XmlIterator<>(reader, unmarshaller, "xxxxxx");
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED), false);
} catch (XMLStreamException e1) {
logger.error("XMLStreamException", e1);
} catch (JAXBException e) {
logger.error("JAXBException", e);
}
和
public class XmlIterator<T> implements Iterator<T> {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
XMLEventReader reader;
XMLEvent event;
Unmarshaller unmarshaller;
String name;
public XmlIterator(XMLEventReader reader, Unmarshaller unmarshaller, String name) {
this.reader = reader;
this.unmarshaller = unmarshaller;
this.name = name;
try {
reader.next();
this.event = reader.peek();
} catch (XMLStreamException e) {
logger.error("", e);
event = null;
}
}
@Override
public boolean hasNext() {
try {
while (event != null && !(event.isStartElement() && name.equals(event.asStartElement().getName().getLocalPart()))) {
Object a = reader.next();
event = reader.peek();
}
return event != null;
} catch (XMLStreamException e) {
logger.error("", e);
event = null;
}
return event != null;
}
@Override
public T next() {
try {
T next = ((JAXBElement<T>) unmarshaller.unmarshal(reader)).getValue();
event = reader.peek();
return next;
} catch (JAXBException e) {
logger.error("error during unmarshalling ", e);
return null;
} catch (XMLStreamException e) {
logger.error("error during stream ", e);
return null;
}
}
}
答案 0 :(得分:5)
每当您看到标记'foo'(使用https://docs.oracle.com/javase/tutorial/jaxp/stax/example.html)时,请使用Stax阅读器来解析和构建对象Foo
Foo readFoo(XMLEventReader xmlIn) throws XMLStreamException {
(...)
return foo;
}
实现了一个java.util.Iterator,它将使用前一个函数返回'next()'Foo