【问题标题】:Merge mutiple xml files and set the common root element of each xml file as the root of the merged file合并多个xml文件,并将每个xml文件的公共根元素设置为合并文件的根
【发布时间】:2016-10-24 08:50:40
【问题描述】:

我需要将目录中的多个 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 xml merge xml-parsing


【解决方案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();
    }
}

【讨论】:

  • 尚未对此进行测试。过段时间会测试。但是,XML 不仅限于两个文件。文件有多个,可以大于 100。这段代码是否支持相同的文件?
  • 它经过测试可以处理 2 个文件...您只需将所有这 100 个文件汇集到程序中,很可能在一个 while 循环中...它应该可以工作
【解决方案2】:

您真的不想在 Java 中执行此操作。在 XSLT 2.0 中是

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

【讨论】:

  • 谢谢!但对 XSLT 不太熟悉。这个很紧急,也许在不久的将来,我会试一试。
  • 越紧急,使用专为这项工作设计的技术就越重要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 1970-01-01
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
相关资源
最近更新 更多