简体   繁体   English

使用Java创建xml

[英]Creating xml from with java

I need your expertise once again. 我再次需要您的专业知识。 I have a java class that searches a directory for xml files (displays the files it finds in the eclipse console window), applies the specified xslt to these and sends the output to a directory. 我有一个Java类,它在目录中搜索xml文件(显示它在eclipse控制台窗口中找到的文件),将指定的xslt应用于这些文件并将输出发送到目录。

What I want to do now is create an xml containing the file names and file format types. 我现在想做的是创建一个包含文件名和文件格式类型的xml。 The format should be something like; 格式应类似于:

<file>
      <fileName>         </fileName>
      <fileType>       </fileType>
</file>

<file>
     <fileName>         </fileName>
     <fileType>       </fileType>
</file>

Where for every file it finds in the directory it creates a new <file> . 它在目录中为每个文件找到的位置都会创建一个新的<file>

Any help is truely appreciated. 任何帮助是由衷的感谢。

Use an XML library. 使用XML库。 There are plenty around, and the third party ones are almost all easier to use than the built-in DOM API in Java. 周围有很多东西,而第三方的几乎比Java中内置的DOM API都更易于使用。 Last time I used it, JDom was pretty good. 上次使用它时, JDom很好。 (I haven't had to do much XML recently.) (我最近不必做太多的XML工作。)

Something like: 就像是:

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);

Have a look at DOM and ECS. 看看DOM和ECS。 The following example was adapted to you requirements from here : 以下示例从此处适应您的要求:

XMLDocument document = new XMLDocument();
for (File f : files) {
    document.addElement( new XML("file")
        .addXMLAttribute("fileName", file.getName())
        .addXMLAttribute("fileType", file.getType())
      )
    );
}

You can use the StringBuilder approach suggested by Vinze, but one caveat is that you will need to make sure your filenames contain no special XML characters, and escape them if they do (for example replace < with &lt;, and deal with quotes appropriately). 您可以使用Vinze建议的StringBuilder方法,但需要注意的是,您需要确保文件名不包含特殊的XML字符,如果确实包含特殊的XML字符,请对其进行转义(例如,将<替换为&lt ;,并适当地处理引号) 。

In this case it probably doesn't arise and you will get away without it, however if you ever port this code to reuse in another case, you may be bitten by this. 在这种情况下,它可能不会出现,如果没有它,您将逃脱,但是,如果您曾经移植此代码以在另一种情况下重用,则可能会为此感到痛苦。 So you might want to look at an XMLWriter class which will do all the escaping work for you. 因此,您可能想看一下XMLWriter类,它将为您完成所有转义工作。

Well just use a StringBuilder : 好吧,只需使用StringBuilder:

StringBuilder builder = new StringBuilder();
for(File f : files) {
    builder.append("<file>\n\t<fileName>").append(f.getName).append("</fileName>\n)";
    [...]
}
System.out.println(builder.toString());

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

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