I have a xml file containing all different types of protocols in the tag "PROTOCOLTAG". I want to extract the values of only those tags which contain the string "SITELINK". Currently I'm able to get the values of all the tag "PROTOCOLTAG". Please help
The xml file..
<?xml version="1.0" encoding="UTF-8"?>
<CDMDataXML xmlns="http://www.avocent.org/trellis/CDMLoaderXMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.avocent.org/trellis/CDMLoaderXMLSchema CDMLoaderXMLSchema.xsd">
<NetworkProtocolDefinition oid="00000000-0000-0000-0000-000000004236">
<NAME>res.dcim.networkprotocol.def.SNMPV1-SSL</NAME>
<PROTOCOLTAG>SNMPV1:SSL</PROTOCOLTAG>
</NetworkProtocolDefinition>
<NetworkProtocolDefinition oid="00000000-0000-0000-0000-000000004237">
<NAME>res.dcim.networkprotocol.def.SNMPV2-SSL</NAME>
<PROTOCOLTAG>SNMPV2:SSL</PROTOCOLTAG>
</NetworkProtocolDefinition>
<NetworkProtocolDefinition oid="00000000-0000-0000-0000-000000004238">
<NAME>res.dcim.networkprotocol.def.BACNET-SITELINK_W-LDM-BREAKER-RS-485</NAME>
<PROTOCOLTAG>BACNET:SITELINK_W-LDM-BREAKER/RS-485</PROTOCOLTAG>
</NetworkProtocolDefinition>
<NetworkProtocolDefinition oid="00000000-0000-0000-0000-000000004239">
<NAME>res.dcim.networkprotocol.def.BACNET-SITELINK_W-LDM-SUBFEED-RS-485</NAME>
<PROTOCOLTAG>BACNET:SITELINK_W-LDM-SUBFEED/RS-485</PROTOCOLTAG>
</NetworkProtocolDefinition>
Java code..
XMLInputFactory f = XMLInputFactory.newInstance();
XMLStreamReader rdr = f.createXMLStreamReader(new FileReader("test.xml"));
while (rdr.hasNext()) {
if (rdr.next() == XMLStreamConstants.START_ELEMENT) {
if (rdr.getLocalName().equals("PROTOCOLTAG")) {
System.out.println(rdr.getElementText());
}
}
}
Expected output
BACNET:SITELINK_W-LDM-BREAKER/RS-485
BACNET:SITELINK_W-LDM-SUBFEED/RS-485
Current Output..
SNMPV1:SSL
SNMPV2:SSL
BACNET:SITELINK_W-LDM-BREAKER/RS-485
BACNET:SITELINK_W-LDM-SUBFEED/RS-485
XMLInputFactory f = XMLInputFactory.newInstance();
XMLStreamReader rdr = f.createXMLStreamReader(new FileReader("C:\\Users\\pramod.karandikar\\Desktop\\Test.xml"));
while (rdr.hasNext()) {
if (rdr.next() == XMLStreamConstants.START_ELEMENT) {
if (rdr.getLocalName().equals("PROTOCOLTAG")) {
String txt = rdr.getElementText();
if (txt.indexOf("SITELINK") > 0) {
System.out.println(txt);
}
}
}
}