简体   繁体   English

获取节点 XML Java 的所有子节点

[英]Get all child nodes of a node XML Java

I am reading a XML file and showing the output of the file.我正在读取 XML 文件并显示文件的输出。

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!提前致谢!

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我创建了一个存储节点名的字符串,如果下一个节点与字符串 var 同名,则 id 不打印名称

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM