This code needs to extract data from in XML file. Specifically it needs to iterate over it, looking for a node called CHARGE_CODES
that repeats over and over and has several elements. it needs to extract the values of one of them, called CODE
. the XML contents are composed of repeating blocks like these:
<CHARGE_CODE> <CODE>some value</CODE> <OtherElement>some value</Otherelement> .. .. </CHARGE_CODE> .. ..
The code then needs to put these values of "CODE" into an array. I create several arrays like these each containing the values of one of these tags.
Then all these arrays are used to send SOAP POSTS and enter together (all elements from one CHARGE_CODES block) into a database as the details of a service.
This code contains one such array collecting the values of one node.
public class Main { public static void main(String argv[]){ String test = "URL"; File inputFile = new File(test); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(inputFile); doc.getDocumentElement().normalize(); NodeList blocks = doc.getElementsByTagName("CHARGE_CODE"); ArrayList<String> codes = new ArrayList<>(); for(int i = 0; i<blocks.getLength(); i++){ Element children = (Element) blocks.item(i); if(children.getElementsByTagName("CODE") != null){ codes.add(children.getElementsByTagName("CODE").item(0).getTextContent()); }else{ codes.add(""); } } } }
xml.etree.ElementTree
is good enough for this task.\$\endgroup\$