2015-02-20 7 views
0

Я пытаюсь извлечь значения атрибутов «address», «programaticName» из тегов «datapoint», «event» & «command». Проблема, с которой я сталкиваюсь, заключается в том, что я хочу извлечь значения, принадлежащие определенному протоколу «BACNET», а не все протоколы в файле XML.Получить значение атрибута specfic tag

XML FILE ..

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

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 

ответ

1

Здесь мы идем .....

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/Stackoverflow/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