My program uses Picocli to parse XML data and store it in an ArrayList. For some reason, the information gets removed when I try to access it from another class.
I run the code below, and it shows the elements just fine:
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); } }
So I try to access sourceArray from another class:
class getArrayElements extends SourceSentences{ public static void main(String[] args) { System.out.println(SourceSentences.sourceArray); } }
and results in variables being [], thus not able to transfer data to another class.
Picocli setup snippet:
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 retrievedn"); } 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"); } }
Advertisement
Answer
The static
context of the SourceSentences.main()
is lost once you run the getArrayElements.main()
method. The parsing of your XML data never happened as far as getArrayElements.main()
was concerned.
You need to call the translate
method from inside the getArrayElements
‘ main function.
class getArrayElements extends SourceSentences { public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException { SourceSentences.translate(); System.out.println(SourceSentences.sourceArray); } }