合并多个xml文件,并将每个xml文件的公共根元素设置为合并文件的根

时间:2016-06-22 12:07:40

标签: java xml merge xml-parsing

我需要将目录中的mutilpe 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();

2 个答案:

答案 0 :(得分:1)

你真的不想在Java中这样做。在XSLT 2.0中它是

<xsl:template name="main">
  <products>
    <xsl:copy-of select="collection('file://mydir')/*/*"/>
  </products>
</xsl:template>

答案 1 :(得分:1)

在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();
    }
}