Search code examples
javaxmldomnodesnodelist

Getting child node Java


I have the following code

        try {
            String xml = "<ADDITIONALIDENT><FEATURE MID=\"TEST\"><NAME>ONE NAME</NAME><VALUE>ONE VALUE</VALUE></FEATURE><FEATURE MID=\"TEST\"><NAME>TWO NAME</NAME><VALUE>TWO VALUE</VALUE></FEATURE><FEATURE MID=\"TEST\"><NAME>THREE NAME</NAME><VALUE>THREE VALUE</VALUE></FEATURE><FEATURE MID=\"TEST\"><NAME>FOUR NAME</NAME><VALUE>FOUR VALUE</VALUE></FEATURE><FEATURE MID=\"TEST\"><NAME>FIVE NAME</NAME><VALUE>FIVE VALUE</VALUE></FEATURE></ADDITIONALIDENT>";

            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            dbFactory.setNamespaceAware(true);
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document document = dBuilder.newDocument();
            document = dBuilder.parse(new InputSource(new StringReader(xml)));

            NodeList featureList = document.getElementsByTagName("FEATURE");
            for (int i = 0; i < featureList.getLength(); i++) {
                Element featureElement = (Element) featureList.item(i);
                NodeList nameList = featureElement.getElementsByTagName("NAME");
                NodeList valueList = featureElement.getElementsByTagName("VALUE");
                System.out.println("THIS IS NAME: " + nameList.item(0).getTextContent());
                System.out.println("THIS IS VALUE: " + valueList.item(0).getTextContent());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

It works fine and it finds the correct values, but I don't think I am doing it the right way. I feel like I shouldn't be using lists within the actual featureList Element.

Is there a way to get the values without making two lists?

<ADDITIONALIDENT>
    <FEATURE MID="TEST">
        <NAME>ONE NAME</NAME>
        <VALUE>ONE VALUE</VALUE>
    </FEATURE>
    <FEATURE MID="TEST">
        <NAME>TWO NAME</NAME>
        <VALUE>TWO VALUE</VALUE>
    </FEATURE>
<ADDITIONALIDENT>

Solution

  • try with following solution,

    try {
        String xml = "YOUR_XML_CONTEN";
    
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        dbFactory.setNamespaceAware(true);
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document document = dBuilder.newDocument();
        document = dBuilder.parse(new InputSource(new StringReader(xml)));
    
        NodeList featureList = document.getElementsByTagName("FEATURE");
            for (int i = 0; i < featureList.getLength(); i++) {
                Element featureElement = (Element) featureList.item(i);
                System.out.println("THIS IS NAME: " + 
                featureElement.getElementsByTagName("NAME").item(0).getTextContent());
                System.out.println("THIS IS VALUE: " + 
                featureElement.getElementsByTagName("VALUE").item(0).getTextContent());
            }
        } catch (Exception e) {
           e.printStackTrace();
    }
    

    output,

    THIS IS NAME: ONE NAME
    THIS IS VALUE: ONE VALUE
    THIS IS NAME: TWO NAME
    THIS IS VALUE: TWO VALUE
    THIS IS NAME: THREE NAME
    THIS IS VALUE: THREE VALUE
    THIS IS NAME: FOUR NAME
    THIS IS VALUE: FOUR VALUE
    THIS IS NAME: FIVE NAME
    THIS IS VALUE: FIVE VALUE