【问题标题】:Is it possible to convert OutStream to a string?是否可以将 OutStream 转换为字符串?
【发布时间】: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)outputString 不匹配方法的签名,即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


【解决方案1】:

我使用了另一种 Java 构建 XML 文件的方法。

新方法调用不需要OutputStream 参数,只需一个字符串,我将其设置为文件路径。

public static void createXml(String directory) throws IOException, TransformerException, ParserConfigurationException {

    BufferedReader sourceReader = new BufferedReader(new FileReader(db));

    .
    .

    // Use DocumentBuilder and construct the XML file using NodeList Collection
    .
    .

    DOMSource source = new DOMSource(doc);

    // Write to console or file
    StreamResult console = new StreamResult(System.out);
    StreamResult file = new StreamResult(new File(directory));
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    // For pretty print
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");

    // Write data
    transformer.transform(source, console);
    transformer.transform(source, file);
    sourceReader.close();

因此,通过使用 DOM 解析器、Transformer 和 StreamResult,我可以构建 .xml 文件并将其存储在带有 StreamResult 的流中(这需要一个字符串参数才能输出)。 瞧!

【讨论】:

    猜你喜欢
    • 2017-09-12
    • 2019-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多