获取特定标签的属性值

Get Attribute value of specfic tag

我正在尝试提取标签 "datapoint"、"event" 和 "command" 的属性 "address"、"programmaticName" 的值。我面临的问题是我想提取属于特定协议 "BACNET" 的值,而不是 XML 文件中的所有协议。

XML 文件..

<elementDefinitionModel manufacturerInSymbol="LIEBERT" minSupportedVersionInSymbol="1.4" modelInSymbol="DS070ADA0EI833A" modelQualifierInSymbol="DS070ADA0EI833A" symbolTag="LIEBERTDS070ADA0EI833ADS070ADA0EI833A">
<supportedprotocols>
    <supportedProtocol isSubscribable="true" protocolName="VELOCITY/IP">
    <datapoints>
        <datapoint address="5327" deviceToNormal="VAL 10 /" nature="PARAMETRIC" programmaticName="val_ext_tmp_airDailyHigh" />
    </datapoints>
    <events>
        <event address="5015" programmaticName="evt_sys_tmpAirSupplyOver" values="a:3 a:11 a:19 a:27 i:0 i:4" />
    </events>
    <commands>
        <command access="RW" address="5008" deviceToNormal="VAL 10 /" division="CONFIGURATION" nature="PARAMETRIC" normalToDevice="VAL 10 *" programmaticName="val_sys_tmp_airSetPt" valueTypeInDevice="DATA_POINT_VALUE_TYPE_INTEGER" />
    </commands>
    </supportedProtocol>
    <supportedProtocol isSubscribable="true" protocolName="BACNET:SITELINK_W-ICOM_PA71/RS-485">
    <datapoints>
        <datapoint address="bs3_{PORTNUMBER}.85" nature="ENUM" programmaticName="st_sys_opStateCooling" valueTypeInDevice="DATA_POINT_VALUE_TYPE_INTEGER" />
    </datapoints>
    <events>
        <event address="bs2_{PORTNUMBER}.85" programmaticName="evt_sys_unitStandby" />
    </events>
    <commands>
        <command access="RW" address="m907_{PORTNUMBER}.85" division="CONFIGURATION" nature="PARAMETRIC" programmaticName="val_fan_pct_maxSetPt" valueTypeInDevice="DATA_POINT_VALUE_TYPE_INTEGER" />
    </commands>
    </supportedProtocol>
 </supportedprotocols>
</elementDefinitionModel>

JAVA 代码..

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    org.w3c.dom.Document document = db.parse(new File("C:\Users\aniruddh.mathur\Desktop\ds1.xml"));
    NodeList nodeList = document.getElementsByTagName("datapoint");
    for(int x=0,size= nodeList.getLength(); x<size; x++) {              System.out.println(nodeList.item(x).getAttributes().getNamedItem("address").getNodeValue()+"\t"+nodeList.item(x).getAttributes().getNamedItem("programmaticName").getNodeValue());
    }
     System.out.println("\n");
     NodeList nodeList1 = document.getElementsByTagName("event");
    for(int x1=0,size1= nodeList1.getLength(); x1<size1; x1++) {
    System.out.println(nodeList1.item(x1).getAttributes().getNamedItem("address").getNodeValue()+"\t"+nodeList1.item(x1).getAttributes().getNamedItem("programmaticName").getNodeValue());
    }
     System.out.println("\n");
     NodeList nodeList2 = document.getElementsByTagName("command");
    for(int x1=0,size1= nodeList2.getLength(); x1<size1; x1++) {
    System.out.println(nodeList2.item(x1).getAttributes().getNamedItem("address").getNodeValue()+"\t"+nodeList2.item(x1).getAttributes().getNamedItem("programmaticName").getNodeValue());
       }

实际输出..

5327    val_ext_tmp_airDailyHigh
bs3_{PORTNUMBER}.85 st_sys_opStateCooling


5015    evt_sys_tmpAirSupplyOver
bs2_{PORTNUMBER}.85 evt_sys_unitStandby


5008    val_sys_tmp_airSetPt
m907_{PORTNUMBER}.85    val_fan_pct_maxSetPt

预期输出..

bs3_{PORTNUMBER}.85 st_sys_opStateCooling

bs2_{PORTNUMBER}.85 evt_sys_unitStandby

m907_{PORTNUMBER}.85    val_fan_pct_maxSetPt

我们开始了.....

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ReadXMLFile {
    public static void main(String argv[]) {

        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            org.w3c.dom.Document document = db.parse(new File(
                    "E:/All WorkSpaces in laptop on 02092-14/SelfLearning/Whosebug/src/test.xml"));


            NodeList nodeList = document.getElementsByTagName("datapoint");
            for (int x = 0, size = nodeList.getLength(); x < size; x++) {

                if( nodeList.item(x).getParentNode().getParentNode().getAttributes().getNamedItem("protocolName").getNodeValue().contains("BACNET"))
                System.out.println(nodeList.item(x).getAttributes()
                        .getNamedItem("address").getNodeValue()
                        + "\t"
                        + nodeList.item(x).getAttributes()
                                .getNamedItem("programmaticName")
                                .getNodeValue());


            }
            System.out.println("\n");
            NodeList nodeList1 = document.getElementsByTagName("event");
            for (int x1 = 0, size1 = nodeList1.getLength(); x1 < size1; x1++) {
                if( nodeList1.item(x1).getParentNode().getParentNode().getAttributes().getNamedItem("protocolName").getNodeValue().contains("BACNET"))
                System.out.println(nodeList1.item(x1).getAttributes()
                        .getNamedItem("address").getNodeValue()
                        + "\t"
                        + nodeList1.item(x1).getAttributes()
                                .getNamedItem("programmaticName")
                                .getNodeValue());
            }
            System.out.println("\n");
            NodeList nodeList2 = document.getElementsByTagName("command");
            for (int x1 = 0, size1 = nodeList2.getLength(); x1 < size1; x1++) {
                if( nodeList2.item(x1).getParentNode().getParentNode().getAttributes().getNamedItem("protocolName").getNodeValue().contains("BACNET"))
                System.out.println(nodeList2.item(x1).getAttributes()
                        .getNamedItem("address").getNodeValue()
                        + "\t"
                        + nodeList2.item(x1).getAttributes()
                                .getNamedItem("programmaticName")
                                .getNodeValue());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

输出为:-

bs3_{PORTNUMBER}.85 st_sys_opStateCooling


bs2_{PORTNUMBER}.85 evt_sys_unitStandby


m907_{PORTNUMBER}.85    val_fan_pct_maxSetPt