提取包含特定字符串的 xml 个标签的值
Extract value of xml tags containing a particular string
我有一个 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
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);
}
}
}
}
这是用于 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();
}
}
我有一个 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
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);
}
}
}
}
这是用于 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();
}
}