拆分xml而不在Java中创建新元素到另一个文件 [英] Splitting an xml without creating new element in java to another file

查看:77
本文介绍了拆分xml而不在Java中创建新元素到另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的XML,我想将其拆分为另一个文件,而无需 创建一个新元素. 有没有一种方法可以将基于子节点的XML拆分为另一个XML 没有创建新标签?

Below is my XML and I want to split that into another file without creating a new element. Is there a way to split that XML based on the child node to another XML without creating new tag?

File 1.xml
<Envelope>
<Data>
<SomeData>SampleData</SomeData>
<NextData>NextData</NextData>
</Data>
<Body>
<Name>ABC</Name>
<Age>12</Age>
<Country>India</Country>
</Body>
</Envelope>

文件1是现有文件,我希望文件2仅包含正文内容 如下图所示

File1 is an existing one and I want file2 to have only contents of body as shown below

File2.xml
<Body>
<Name>ABC</Name>
<Age>12</Age>
<Country>India</Country>
</Body>

我也希望file2是一个新文件,但我不想 创建任何新元素. 我在这里引用的示例只能做到这一点 通过创建一个新元素 下面的链接使用创建新元素

Also I want the file2 to be a new file but I don't want to have any new elements created. The examples i referred here had a way to do this only by creating a new element Below link uses creating new element

*http://stackoverflow.com/questions/2056910/split-xml-in-multiple-xml-files*

我提到的代码:

public class XmlSplit {

public static void main(String [] args) throws Exception {
File input = new File("input.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Document doc = dbf.newDocumentBuilder().parse(input);
XPath xpath = XPathFactory.newInstance().newXPath();

NodeList nodes = (NodeList) xpath.evaluate("//T0020/IRP_ACCOUNT", doc, XPathConstants.NODESET);

int itemsPerFile = 5;
int fileNumber = 0;
Document currentDoc = dbf.newDocumentBuilder().newDocument();
Node rootNode = currentDoc.createElement("T0020");
File currentFile = new File(fileNumber+".xml");
for (int i=1; i <= nodes.getLength(); i++) {
Node imported = currentDoc.importNode(nodes.item(i-1), true);
rootNode.appendChild(imported);

if (i % itemsPerFile == 0) {
writeToFile(rootNode, currentFile);

rootNode = currentDoc.createElement("T0020");
currentFile = new File((++fileNumber)+".xml");
}
}

writeToFile(rootNode, currentFile);
}

private static void writeToFile(Node node, File file) throws Exception {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(node), new StreamResult(new FileWriter(file)));
}
}

推荐答案

我将使用XSLT转换并将其转换.
Java代码:

I would use the XSLT transformation and transform it.
Java Codes:

TransformerFactory factory = TransformerFactory.newInstance();
Source xslt = new StreamSource(new File("Split.xslt"));
Transformer transformer = factory.newTransformer(xslt);

Source text = new StreamSource(new File("File1.xml"));
transformer.transform(text, new StreamResult(new File("File2.xml")));

文件:Split.xslt

File: Split.xslt

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output indent="yes" />

    <xsl:template match="/">
        <xsl:copy-of select="/Envelope/Body"/>
    </xsl:template>
</xsl:stylesheet>

输出文件:

<?xml version="1.0" encoding="UTF-8"?>
<Body>
    <Name>ABC</Name>
    <Age>12</Age>
    <Country>India</Country>
</Body>

这篇关于拆分xml而不在Java中创建新元素到另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆