我需要将目录中的残缺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();发布于 2016-06-22 21:42:46
用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();
}
}发布于 2016-06-22 15:25:11
您真的不想在Java中这样做。在XSLT2.0中,
<xsl:template name="main">
<products>
<xsl:copy-of select="collection('file://mydir')/*/*"/>
</products>
</xsl:template>https://stackoverflow.com/questions/37967575
复制相似问题