提取包含特定字符串的xml标记的值

时间:2015-01-22 07:36:49

标签: java xml xml-parsing string-parsing stax

我有一个xml文件,其中包含标签“PROTOCOLTAG”中的所有不同类型的协议。我想只提取包含字符串“SITELINK”的那些标签的值。目前我能够获得所有标签“PROTOCOLTAG”的值。请帮忙

xml文件..

 <?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代码..

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

预期输出

 BACNET:SITELINK_W-LDM-BREAKER/RS-485
 BACNET:SITELINK_W-LDM-SUBFEED/RS-485

当前输出..

 SNMPV1:SSL
 SNMPV2:SSL
 BACNET:SITELINK_W-LDM-BREAKER/RS-485
 BACNET:SITELINK_W-LDM-SUBFEED/RS-485

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

这是用于contains()方法;

XMLInputFactory f = XMLInputFactory.newInstance();
XMLStreamReader rdr;
try {
   rdr = f.createXMLStreamReader(new FileReader( "test.xml"));
      while (rdr.hasNext()) {
        if (rdr.next() == XMLStreamConstants.START_ELEMENT) {
          if (rdr.getLocalName().equals("PROTOCOLTAG")) {
               String str = rdr.getElementText().toString();
               if (str.toString().contains("SITELINK")) {
                 System.out.println(str);
               }
           }
      }
   }
} catch (FileNotFoundException e) { 
    e.printStackTrace();
} catch (XMLStreamException e) { 
    e.printStackTrace();
}

}