繁体   English   中英

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

[英]Merge mutiple xml files and set the common root element of each xml file as the root of the merged file

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM