我再次需要您的专业知识。我有一个 java 类,它在一个目录中搜索 xml 文件(在 eclipse 控制台窗口中显示它找到的文件),将指定的 xslt 应用于这些文件并将输出发送到一个目录。
我现在要做的是创建一个包含文件名和文件格式类型的 xml。格式应该是这样的;
<file>
<fileName> </fileName>
<fileType> </fileType>
</file>
<file>
<fileName> </fileName>
<fileType> </fileType>
</file>
它在目录中找到的每个文件都会创建一个新的 <file>
.
非常感谢任何帮助。
最佳答案
使用 XML 库。周围有很多,第三方的几乎都比 Java 中内置的 DOM API 更容易使用。上次用过,JDom很好。 (最近我不必做太多 XML。)
类似于:
Element rootElement = new Element("root"); // You didn't show what this should be
Document document = new Document(rootElement);
for (Whatever file : files)
{
Element fileElement = new Element("file");
fileElement.addContent(new Element("fileName").addContent(file.getName());
fileElement.addContent(new Element("fileType").addContent(file.getType());
}
String xml = XMLOutputter.outputString(document);
关于java - 使用 java 创建 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/380639/