0

I am reading a XML file and showing the output of the file.

But the problem is that when a node has many child nodes this happens:

---------character
---------id
------------>148
---------name
------------>Arya Stark
---------gender
------------>Female
---------culture
------------>Northmen
---------born
------------>In 289 AC, at Winterfell
---------died
---------alive
------------>TRUE
---------titles
---------title
------------>Princess
---------aliases
---------alias
------------>Arya Horseface
---------alias
------------>Arya Underfoot
---------alias
------------>Arry
---------alias
------------>Lumpyface
---------alias
------------>Lumpyhead
---------alias
------------>Stickboy
---------alias
------------>Weasel
---------alias
------------>Nymeria
---------alias
------------>Squan
---------alias
------------>Saltb
---------alias
------------>Cat of the Canaly
---------alias
------------>Bets
---------alias
------------>The Blind Girh
---------alias
------------>The Ugly Little Girl
---------alias
------------>Mercedenl
---------alias
------------>Mercye

As you can see, that aliases has many alias, well I would want to output them all at the same time.

This is my code:

public class JavaApplication20 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ParserConfigurationException, org.xml.sax.SAXException, IOException {

        String path = "got.xml";

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder = factory.newDocumentBuilder();
        org.w3c.dom.Document document = builder.parse(path);

        document.getDocumentElement().normalize();

        iterateNodes(document.getDocumentElement());

    }

    private static void iterateNodes(Node node) {

        NodeList nodeList = node.getChildNodes();

        for (int i = 0; i < nodeList.getLength(); i++) {
            Node currentode = nodeList.item(i);

            if (currentode.getNodeType() == Node.ELEMENT_NODE) {

                System.out.print("---------");
                System.out.println(currentode.getNodeName().trim());

                Element element = (Element) currentode;
                iterateNodes(element);
            } else if (!nodeList.item(i).getTextContent().trim().equals("")) {
                System.out.println("------------>" + nodeList.item(i).getTextContent().trim());

            }

        }

    }

}

I am trying to detect when a node has many child nodes but I don't have an idea.

Thanks in advance!

2
  • 1
    detect when a node has many child nodes --> nodeList.getLength() > 1?
    – Carlos
    Oct 2, 2020 at 16:09
  • I want to print all child of a node when it has more than 1 Oct 2, 2020 at 16:13

1 Answer 1

0

I have solved it, it's a bad solution but fixes the problem!

I created a string that stores the nodename, if the next node has the same name as the string var, id doesn't print the name

   if(!anterior.equals(currentode.getNodeName().trim())){
         System.out.print("---------");
            System.out.println(currentode.getNodeName().trim());
     } 
            
            
            anterior=currentode.getNodeName().trim();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.