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

DEBASHIS BAIDYA :

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. 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 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>

enter image description here

Andreas :

When you call getElementsByTagName(), don't call it on the document, call it on the parent node.

When you call the method on doc, it will scan the entire document for elements of that name.

When you call the method in node, it will only scan sub-elements of that node for elements of that name.

So, change this line:

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

To this:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=25255&siteId=1