gpt4 book ai didi

java - 合并多个xml文件,并将每个xml文件的公共(public)根元素设置为合并文件的根

转载 作者:行者123 更新时间:2023-11-30 02:53:25 25 4
gpt4 key购买 nike

我需要将目录中的多个 xml 文件合并到一个 xml 文件中。以下是我想要实现的目标的描述:

xml-1:

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<id>0569054</id>
<ProviderName>John</ProviderName>
</product>
</products>

xml-2

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<id>1002363</id>
<ProviderName>Paul</ProviderName>
</product>
</products>

合并输出:

<?xml version="1.0" encoding="UTF-8"?>

<products>
<product>
<id>0569054</id>
<ProviderName>John</ProviderName>
</product>
<product>
<id>1002363</id>
<ProviderName>Paul</ProviderName>
</product>
</products>

这是Java代码,我正在尝试:编辑:尝试使用StAX。现在需要在此处添加什么来删除产品?今天通过 Stax 来实现这一点,也非常欢迎纠正建议。

File dir = new File("/opt/dev/common");
File[] rootFiles = dir.listFiles();

Writer outputWriter = new FileWriter("mergedFile1.xml");
XMLOutputFactory xmlOutFactory = XMLOutputFactory.newFactory();
XMLEventWriter xmlEventWriter = xmlOutFactory.createXMLEventWriter(outputWriter);
XMLEventFactory xmlEventFactory = XMLEventFactory.newFactory();

xmlEventWriter.add(xmlEventFactory.createStartDocument());
xmlEventWriter.add(xmlEventFactory.createStartElement("", null, "products"));
XMLInputFactory xmlInFactory = XMLInputFactory.newFactory();
for (File rootFile : rootFiles) {
XMLEventReader xmlEventReader = xmlInFactory.createXMLEventReader(new StreamSource(rootFile));
XMLEvent event = xmlEventReader.nextEvent();

while (event.getEventType() != XMLEvent.START_ELEMENT) {
event = xmlEventReader.nextEvent();
}

do {
xmlEventWriter.add(event);
event = xmlEventReader.nextEvent();
} while (event.getEventType() != XMLEvent.END_DOCUMENT);
xmlEventReader.close();
}

xmlEventWriter.add(xmlEventFactory.createEndElement("", null, "products"));
xmlEventWriter.add(xmlEventFactory.createEndDocument());

xmlEventWriter.close();
outputWriter.close();

最佳答案

用 Java 来做这件事非常简单直接...下面是基于 VTD-XML 合并文件的代码。它基本上是附加不包括开始和结束标签的字节。想象一下,您打开文件并使用鼠标指针突出显示文本部分,然后将其粘贴到输出文本编辑器中。这正是此处发生的情况。

import com.ximpleware.*;
import java.io.*;

public class simpleMerge {
static VTDGen vg = new VTDGen();
public static void main(String[] s) throws VTDException,IOException{
FileOutputStream fos = new FileOutputStream("d:\\xml\\o.xml");
// write header to
byte[] header=("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<products>").getBytes();
fos.write(header);
appendSingleFile("d:\\xml\\xml-1.xml",fos);
appendSingleFile("d:\\xml\\xml-2.xml",fos);
fos.write("</products>".getBytes());

}
// write everything under root into output efficiently, ie. direct byte copying
public static void appendSingleFile(String fileName,FileOutputStream fos) throws VTDException,IOException{
if (!vg.parseFile(fileName, false)){
System.out.println("invalid file:"+fileName);
System.exit(1);
}
VTDNav vn = vg.getNav();
long l = vn.getContentFragment();
fos.write(vn.getXML().getBytes(),(int)l,(int)(l>>32));
vg.clear();
}
}

关于java - 合并多个xml文件,并将每个xml文件的公共(public)根元素设置为合并文件的根,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37967575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com