如何从 dom 元素中获取所有 xml 子元素并在 Java 中循环?

How to get all xml sub elements from a dom Element with loop in Java?

输出如下图,部分ok。但我只想显示标签中的节点,如输出所示,但这里它重复标签的数量。每个反应堆应该只显示 3 个结果。 输出符合我的预期,但结果显示的比平时多。

循环有什么问题? 请帮忙

import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;  
import org.w3c.dom.NodeList;  
import org.w3c.dom.Node;  
import org.w3c.dom.Element;
import java.io.File;


public class Javatracer {

    public static void main(String args[])   
    {  
    try   
    {  

    File file = new File("trace.xml");

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(file);  
    doc.getDocumentElement().normalize();

    System.out.println("Root element: " + doc.getDocumentElement().getNodeName());  
    NodeList nodeList = doc.getElementsByTagName("actor");  

    for (int itr = 0; itr < nodeList.getLength(); itr++)
    {  
        Node node = nodeList.item(itr);  
        System.out.println("\nNode Name :" + node.getNodeName());  

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

            NodeList portnodeList = doc.getElementsByTagName("port");
            for (int portitr = 0; portitr < portnodeList.getLength(); portitr++)
            {

                Node portnode = portnodeList.item(portitr);
                Element portElement = (Element) portnode;  
                System.out.println("Channel Name: "+ portElement.getAttribute("name")+" "
                        + "| Channel Type: "+ portElement.getAttribute("type")+" | "
                                + "Channel Rate: "+ portElement.getAttribute("rate"));  


            }

            }  
    }   
    }   
    catch (Exception e)   
    {  
    e.printStackTrace();  
    }  
}

}

这是 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<sdf3 version="1.0" type="csdf">

    <applicationGraph name="noname">

        <csdf name="noname" type="noname">

            <actor name="micf_0" type="a">
                <port name="in_channel_1" type="out" rate="16"/>
                    <port name="in_channel_2" type="out" rate="16"/>
                    <port name="in_channel_3" type="out" rate="16"/>
            </actor>
            <actor name="micf_1" type="b">
                <port name="in_channel_1" type="out" rate="16"/>
                    <port name="in_channel_2" type="out" rate="16"/>
                    <port name="in_channel_3" type="out" rate="16"/>
            </actor>
            <actor name="micf_1" type="b">
                <port name="in_channel_1" type="out" rate="16"/>
                    <port name="in_channel_2" type="out" rate="16"/>
                    <port name="in_channel_3" type="out" rate="16"/>
            </actor>
            <actor name="micf_1" type="b">
                <port name="in_channel_1" type="out" rate="16"/>
                    <port name="in_channel_2" type="out" rate="16"/>
                    <port name="in_channel_3" type="out" rate="16"/>
            </actor>
            <actor name="micf_1" type="b">
                <port name="in_channel_1" type="out" rate="16"/>
                    <port name="in_channel_2" type="out" rate="16"/>
                    <port name="in_channel_3" type="out" rate="16"/>
            </actor>

        </csdf>
</sdf3>

调用getElementsByTagName()时,不要在文档上调用,在父节点上调用。

当您在 doc 上调用该方法时,它将扫描整个文档以查找具有该名称的元素。

当您调用 node 中的方法时,它只会扫描该节点的子元素以查找该名称的元素。

因此,更改此行:

NodeList portnodeList = doc.getElementsByTagName("port");

为此:

NodeList portnodeList = ((Element) node).getElementsByTagName("port");