【问题标题】:How do I transfer my parsed array data from one class to another?如何将我解析的数组数据从一个类传输到另一个类?
【发布时间】:2022-01-23 07:24:55
【问题描述】:

我的程序使用 Picocli 解析 XML 数据并将其存储在 ArrayList 中。出于某种原因,当我尝试从其他类访问该信息时,该信息会被删除。

我运行下面的代码,它显示的元素很好:

public class SourceSentences {
    static String source;
    static ArrayList<String> sourceArray = new ArrayList<>();


    public static void translate() throws ParserConfigurationException, IOException, SAXException {
        String xmlFileLocation = "C:\\Users\\user\\Desktop\\exercise\\source.txml";
        System.out.println("---------------");
        System.out.println("Get Text From Source File: ");
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        builderFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

        //parse '.txml' file
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document document = builder.parse(new File(xmlFileLocation));
        //...
        document.getDocumentElement().normalize();

        //specify tag in the '.txml' file and iterate
        NodeList nodeList = document.getElementsByTagName("segment");

        for (int i = 0; i < nodeList.getLength(); i++) {
            //this is tag index of where line of el are
            Node node = nodeList.item(i);

            //check if actually a node
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                //create a node object that will retrieve the element in the XML file
                Element element = (Element) node;
                //get the element from the specified node in nodeList
                source = element.getElementsByTagName("source").item(0).getTextContent();
                //check what it looks like
                System.out.println(source);
                //add to arraylist
                sourceArray.add(source);
            }

            /*String[] arr = source.split("\\s");
            System.out.println(Arrays.toString(arr));
            System.out.println(Arrays.toString(arr));*/
        }

        //get its data type to make sure
        System.out.println("data type: " + source.getClass().getSimpleName());
        System.out.println(sourceArray);
    }
}

所以我尝试从另一个类访问 sourceArray:

class getArrayElements extends SourceSentences{

    public static void main(String[] args) {
        System.out.println(SourceSentences.sourceArray);
    }
}

并导致变量为 [],因此无法将数据传输到另一个类。

Picocli 设置 sn-p:

public class TranslateTXML implements Callable<String> {

    @Option(names = "-f", description = " path to source txml file")

    private String file;

    @Option(names = "-o", description = "output path")
    private String output;

    public static void main(String... args) throws Exception {
        int exitCode = new picocli.CommandLine(new TranslateTXML()).execute(args);
        System.exit(exitCode);
    }

    public String call() throws Exception {
        if (file != null) {
            if (file.equals("C:\\Users\\gnier\\Desktop\\exercise\\source.txml")) {
                sourceSent("C:\\Users\\gnier\\Desktop\\exercise\\source.txml");
                System.out.println("source.txml data retrieved\n");
            } else {
                System.out.println("File \"source.txml\" not found. Check FileName and Directory.");
                System.exit(2);
            }
        }

        WriteSourceTranslatedToTXML.makeTranslated(System.out);
        System.out.println("translated made");
        System.out.println("------");
        System.out.println("File \"translated.txml\" has been outputted to designated path");
    }
}

【问题讨论】:

    标签: java xml picocli


    【解决方案1】:

    运行getArrayElements.main() 方法后,SourceSentences.main()static 上下文将丢失。就getArrayElements.main() 而言,XML 数据的解析从未发生过。

    您需要从getArrayElements' main 函数内部调用translate 方法。

    class getArrayElements extends SourceSentences {
    
        public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
            SourceSentences.translate();
            System.out.println(SourceSentences.sourceArray);
        }
    }
    

    【讨论】:

    • 感谢您的回复!但是当我尝试从 getArrayElements 中的方法访问 sourceArray 中的数组元素时,它仍然会导致 []。我怎样才能做到这一点?或者也许你说的有些东西我不明白。对不起!!!
    • 基本上我所做的是能够从我计算机中的 source.xml 中提取句子,存储它们,然后将它们放在 sourceArray.get 下的新 xml 文件中(0) 我写的。但由于我无法获取 sourceArray 元素,因此无法将它们放入新的 xml 文件中。那有意义吗?也许将整个创建 xml 代码扔到 main 方法中?
    • @Barath Kumar 我尝试了您的建议,但没有成功。数组元素仍未转移。我最初从 nodeList 获取元素,然后将它们放入 arrayList。这可能是为什么?
    • 嘿@EmekaA,您是否尝试再次从不同的上下文访问sourceArray?基本上,您需要完成所有处理,访问数组并再次将其从单个 main 方法写入不同的 xml。每个主要方法都有自己的运行时环境,并且您对数据结构所做的任何操作都不会在此上下文之外持久化,除非它是您正在写入的文件(保存到硬盘上)。
    • 糟糕。我并不是要在其他类中添加这些主要方法。好的,所以我需要从命令行设置类中的 main() 调用所有内容。因此,在一个 cmd 选项中,我可以构建 xml 文件,然后在另一个选项中,只需执行文件 I/O 并在从 api 获取数据后完成添加任何其他内容,以及其他任何内容?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多