如何将我解析的数组数据从一个 class 传输到另一个?

How do I transfer my parsed array data from one class to another?

我的程序使用 Picocli 解析 XML 数据并将其存储在 ArrayList 中。出于某种原因,当我尝试从另一个 class.

访问它时,信息被删除了

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

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

所以我尝试从另一个 class:

访问 sourceArray
class getArrayElements extends SourceSentences{

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

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

Picocli 设置片段:

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

一旦您 运行 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);
    }
}