简体   繁体   English

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

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

The output is in the screenshot below, It is partially ok.输出在下面的屏幕截图中,部分正常。 But I only want to show the nodes in tag as shown in the output but here it is repeating with the number of tag.但我只想显示标签中的节点,如输出所示,但在这里它与标签数量重复。 There should only show 3 result in per reactor.每个反应器应该只显示 3 个结果。 The output is expected as I wanted but the result is showing more than usual.输出是我想要的,但结果显示的比平时多。

What is wrong with the looping ?循环有什么问题? Help please.请帮忙。

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();  
    }  
}

}

Here is the xml file:这是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>

在此处输入图片说明

When you call getElementsByTagName() , don't call it on the document, call it on the parent node.当您调用getElementsByTagName() ,不要在文档上调用它,而是在父节点上调用它。

When you call the method on doc , it will scan the entire document for elements of that name.当您在doc上调用该方法时,它将扫描整个文档以查找具有该名称的元素。

When you call the method in node , it will only scan sub-elements of that node for elements of that name.当您调用node的方法时,它只会扫描该节点的子元素以查找该名称的元素。

So, change this line:所以,改变这一行:

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

To this:对此:

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

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

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