Fala galera. Depois de muito trabalho. Final cheguei onde gostaria. Merge usando StAX. Segue abaixo somente a função.
A lógica não é muito direta. Mas foi a forma que encontrei para resolver o problema. :!:
fileFtp:
<?xml version="1.0" encoding="UTF-8"?>
<notasfiscais>
<notafiscal>fdfdfdfd</notafiscal>
<notafiscal>fdsfgsd</notafiscal>
</notasfiscais>
fileLocal:
[code]<notasfiscais>
<notafiscal>rerer</notafiscal>
<notafiscal>saffdsf</notafiscal>
</notasfiscais>
[/code]
Obs: É claro que os arquivos principais existem para mais de 300.000 eventos, estes acima são somente para ilustrar.
teste <----- Resultado
<notasfiscais>
<notafiscal>fdfdfdfd</notafiscal>
<notafiscal>fdsfgsd</notafiscal>
<notafiscal>rerer</notafiscal>
<notafiscal>saffdsf</notafiscal>
</notasfiscais>
[code]import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.XMLEvent;
import javax.xml.stream.XMLInputFactory;
public class Teste {
//fileFtp e fileLocal são arquivos XML. Pelo menos o mapeamento dele. E levo em consideração que os dois existem!
public InputStream merge(File fileFtp , File fileLocal) throws FileNotFoundException, XMLStreamException{
// TODO Auto-generated constructor stub
XMLEventWriter eventWriter = null;
XMLEventFactory eventFactory = null;
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
File teste = new File("C:\\XMLTESTE.xml");
try {
teste.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream(
teste));
eventFactory = XMLEventFactory.newInstance();
XMLEvent tagEspacoLinha = eventFactory.createDTD("\n");
eventWriter.add(eventFactory.createStartDocument());
eventWriter.add(tagEspacoLinha);
eventWriter
.add(eventFactory.createStartElement("", "", "notasfiscais"));
String[] filenames = new String[] { "C:/XMLftp.xml", "C:/XMLLocal.xml" };
boolean i = false;
for (String filename : filenames) {
i = true;
FileInputStream inputStream = new FileInputStream(filename);
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
XMLEventReader test = inputFactory
.createXMLEventReader(inputStream);
while (test.hasNext()) {
XMLEvent event = test.nextEvent();
XMLEvent event1 = test.peek();
if(event.isStartElement()&&i==true){
i = false;
test.close();
continue;
}
if (event.getEventType() != XMLEvent.END_DOCUMENT
&& event.getEventType() != XMLEvent.START_DOCUMENT
&& !event1.isEndDocument()) {
eventWriter.add(event);
}
test.close();
}
eventWriter.add(tagEspacoLinha);
}
eventWriter.add(eventFactory.createEndElement("", "", "notasfiscais"));
eventWriter.close();
//Função extra.
Teste testeNew = new Teste();
return testeNew.transformar(teste);
}
public InputStream transformar(File file) throws FileNotFoundException{
FileInputStream fis = new FileInputStream (file);
System.out.println("Entrou na função");
return fis;
}
}[/code]
Agora é só correr pro abraço!! Agradecimentos aos que deram a ideal de fazer por arquivo.