我查看了以下链接中的讨论 - Merging xml file using java NodeList
我只需要使用相同的功能使用SAX解析器,因为我只需要合并两个文件,但它们的大小非常大。
请帮忙。
档案1
<root>
<Item>
<a>jhiuo55jhj</a>
<b>jhjoiohj</b>
<c>jhjh334j</c>
</Item>
</root>
文件2
<root>
<Item>
<x>jhi99jhj</x>
<y>jhyty66jhj</y>
<z>jhxdx3jhj</z>
</Item>
</root>
预期输出
<root>
<Item>
<a>jhiuo55jhj</a>
<b>jhjoiohj</b>
<c>jhjh334j</c>
<x>jhi99jhj</x>
<y>jhyty66jhj</y>
<z>jhxdx3jhj</z>
</Item>
</root>
答案 0 :(得分:0)
基本解决方案,不包括命名空间或属性
代码
public class XmlMerger {
public static void main(String[] args) throws Exception {
FileOutputStream outputStream = new FileOutputStream("output.xml");
XMLStreamWriter out = XMLOutputFactory.newInstance().createXMLStreamWriter(new OutputStreamWriter(outputStream));
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
Handler handler = new Handler(out);
out.writeStartDocument();
out.writeStartElement("root");
saxParser.parse(new File("input1.xml"), handler);
saxParser.parse(new File("input2.xml"), handler);
out.writeEndElement();
out.close();
}
private static class Handler extends DefaultHandler {
private XMLStreamWriter out;
private boolean dumping;
public Handler(XMLStreamWriter out) {
this.out = out;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if ("root".equals(qName)) {
dumping = true;
} else {
try {
out.writeStartElement(qName);
// TODO attributes if you need them...
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if ("root".equals(qName)) {
dumping = false;
} else {
try {
out.writeEndElement();
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (!dumping) {
return;
}
try {
out.writeCharacters(ch, start, length);
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
}
}
答案 1 :(得分:0)
我会说你在这里采用SAX-DOM方法来获得更好的性能。
让我们说下面是输入的xml文件
1) First.xml
<root>
<Item>
<a>1</a>
<b>2</b>
<c>3</c>
</Item>
<Item>
<a>1</a>
<b>2</b>
<c>3</c>
</Item>
</root>
2) Second.xml
<root>
<Item>
<x>11</x>
<y>22</y>
<z>33</z>
</Item>
<Item>
<x>44</x>
<y>55</y>
<z>66</z>
</Item>
</root>
合并这些xmls的步骤: