Java 如何从XML文件的解析数据输出创建单个XML文件?

Java 如何从XML文件的解析数据输出创建单个XML文件?,java,android,xml,parsing,dom,Java,Android,Xml,Parsing,Dom,我编写了一个程序(DOM解析器),用于解析XMl文件中的数据。我想为从xml文档解析的每一组数据创建一个具有相应名称的单独文件。如果解析后的输出是Single、Double、Triple,我想创建一个单独的xml文件(Single.xml、Double.xml、Triple.xml),其中包含相应的名称。如何创建xml文件并为每个文件提供解析数据输出的名称?提前感谢你的帮助 import java.io.IOException; import javax.xml.parsers.Docume

我编写了一个程序(DOM解析器),用于解析XMl文件中的数据。我想为从xml文档解析的每一组数据创建一个具有相应名称的单独文件。如果解析后的输出是Single、Double、Triple,我想创建一个单独的xml文件(Single.xml、Double.xml、Triple.xml),其中包含相应的名称。如何创建xml文件并为每个文件提供解析数据输出的名称?提前感谢你的帮助

import java.io.IOException;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class MyDomParser {

  public static void main(String[] args) {
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  try {
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document doc = builder.parse("ENtemplate.xml");
  doc.normalize();

  NodeList rootNodes = doc.getElementsByTagName("templates");
  Node rootNode = rootNodes.item(0);
  Element rootElement = (Element) rootNode;
  NodeList templateList = rootElement.getElementsByTagName("template");

  for(int i=0; i < templateList.getLength(); i++) {
  Node theTemplate = templateList.item(i);
  Element templateElement = (Element) theTemplate;
  System.out.println("Template" + ": " +templateElement.getAttribute("name")+ ".xml");

  }
  } catch (ParserConfigurationException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } catch (SAXException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  } catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }

  }


}
import java.io.IOException;
导入javax.xml.parsers.DocumentBuilder;
导入javax.xml.parsers.DocumentBuilderFactory;
导入javax.xml.parsers.parserConfiguration异常;
导入org.w3c.dom.Document;
导入org.w3c.dom.Element;
导入org.w3c.dom.Node;
导入org.w3c.dom.NodeList;
导入org.xml.sax.SAXException;
公共类MyDomParser{
公共静态void main(字符串[]args){
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
试一试{
DocumentBuilder=factory.newDocumentBuilder();
documentdoc=builder.parse(“ENtemplate.xml”);
doc.normalize();
NodeList rootNodes=doc.getElementsByTagName(“模板”);
节点rootNode=rootNodes.item(0);
元素rootElement=(元素)rootNode;
NodeList templateList=rootElement.getElementsByTagName(“模板”);
对于(int i=0;i
只需像往常一样使用文件I/O API在循环中创建xml文件即可

// for every Node in the template List
for(int i=0; i < templateList.getLength(); i++) {

  // cast each Node to a template Element
  Node theTemplate = templateList.item(i);
  Element templateElement = (Element) theTemplate;

  // get the xml filename as = template's name attribute + .xml
  String fileName = templateElement.getAttribute("name") + ".xml";

  // create a Path that points to the current directory
  Path filePath = Paths.get(fileName);

  // create the xml file at the specified Path
  Files.createFile(filePath);

  System.out.println("File "+ fileName + ".xml created");
}
//用于模板列表中的每个节点
对于(int i=0;i

上面的代码将在当前工作目录中创建xml文件。您还需要处理选中的
IOException

@ravionline如果可能,您可以为代码添加注释吗?