【发布时间】:2022-02-21 21:32:39
【问题描述】:
我有一个使用 DOM 解析来构建 XML 文件的方法,它需要一个 OutputStream 作为参数。我正在尝试从命令行运行程序,但命令行选项只接受字符串。
我可以通过输入 System.out 作为参数并运行程序来运行它,但仅此而已。
这是一段代码:
public class WriteSourceTranslatedToTXML extends GetSourceSentences {
public static String makeTranslated(OutputStream output, String out) throws ParserConfigurationException, IOException, SAXException, URISyntaxException {
System.out.println("---creating XML file---");
Document doc = new Document();
doc.setRootElement(new Element("txml"));
// Built XML here and inserted sentences
xmlOutputter.setFormat(Format.getPrettyFormat());
xmlOutputter.output(doc, output);
// Write to file
XMLOutputter xout = new XMLOutputter();
String xml = xout.outputString(doc.getRootElement().getContent());
try(FileOutputStream fileOutputStream =
new FileOutputStream(out)) {
xmlOutputter.output(doc, fileOutputStream);
这是命令行的代码:
@Command(name = "fileCli", description = "Performs file manipulation operations", mixinStandardHelpOptions = true, version = "File Client 1.0")
public class TranslateTXML implements Callable<String> {
@Option(names = "-o", description = "output path")
private String output;
if (output != null) {
WriteSourceTranslatedToTXML.makeTranslated(output); // Red line under output
System.out.println("translated made");
System.out.println("------");
System.out.println("File \"translated.txml\" has been outputted to designated path");
我该怎么做?
【问题讨论】:
-
调用
makeTranslated(output)与output是String不匹配方法的签名,即makeTranslated(OutputStream output, String out)。目前尚不清楚您的意图是什么。 -
我只是想看看我可以让它与 2 个参数一起工作。本质上,我的程序所做的是它从 xml 文件中读取 xml 数据,为另一组数据调用 API,构建一个新的 xml 文件并将数据输入到新的 xml 文件中。
-
outputStream 方法负责写入整个 xml 文件并设置文件在驱动器上的目的地。也许我可以这样做:调用我的 makeTranslated() 方法来创建所有 xml 数据 > 创建另一个方法“String myMethod(String filePath)”,它将检索数据并返回文件路径,以防止命令行检索任何输出流数据。
标签: java parsing command-line command-line-arguments picocli