I need to get Device
ID
, DeviceType
, data of first LastKnownValue
tag. The XML is
<?xml version="1.0" encoding="utf-8"?>
<DeviceList Devid="" Count="35">
<Device ID="01">
<Name>@@@@@</Name>
<DeviceType>Me</DeviceType>
<ValueVariables Count="3">
<LastKnownValue Index="1" EndPoint="1" Name="STATE" Type="Bool">false</LastKnownValue>
<LastKnownValue Index="2" EndPoint="1" Name="LOW BATTERY" Type="Bool">0</LastKnownValue>
<LastKnownValue Index="3" EndPoint="1" Name="TAMPER" Type="Bool">false</LastKnownValue>
</ValueVariables>
</Device>
</DeviceList>
I tried the following code, but unable to get the values of the attributes.
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse("xml path");
doc.getDocumentElement().normalize();
NodeList devicelist = doc.getDocumentElement().getElementsByTagName("Device");
System.out.println(devicelist.getLength());
for (int i = 0; i < devicelist.getLength(); ++i)
{
Element rule = (Element) devicelist.item(i);
//Device ID
String ruleid = rule.getAttribute("ID");
System.out.println(ruleid);
}