我需要获取Device
ID
,DeviceType
,第一个LastKnownValue
标记的数据。 XML是
<?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>
我尝试了以下代码,但无法获取属性的值。
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);
}
答案 0 :(得分:1)
我建议使用XPath直接从XML获取节点值,而不是遍历并获取所需的节点。
如果Device
节点只出现一次,那么您可以使用下面的XPath表达式来获取数据。
XPath
的 Device ID
是 - //*/Device/@ID
或/DeviceList/Device/@ID
XPath
的 DeviceType
是 - //*/DeviceType
或/DeviceList/Device/DeviceType
XPath
获得第一个LastKnownValue
- (//*/LastKnownValue)[1]
答案 1 :(得分:1)
如评论中所述,您可以使用XPath查询直接从doc
中提取信息。
尝试查看this link。
这是一个代码示例:
try {
// Build structures to parse the String
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
// Create an XPath query
XPath xPath = XPathFactory.newInstance().newXPath();
// Query the DOM object with the query
NodeList nodeList = (NodeList) xPath.compile("//hello").evaluate(document, XPathConstants.NODESET);
} catch (Exception e) {
e.printStackTrace();
}
答案 2 :(得分:1)
使用JAXB的完整功能示例:
public static void main(String[] args) throws Exception {
JAXBContext ctx = JAXBContext.newInstance(DeviceList.class);
Unmarshaller um = ctx.createUnmarshaller();
DeviceList deviceList = (DeviceList) um.unmarshal(new File("example.xml"));
for (Device device : deviceList.devices) {
System.out.println(device.id);
System.out.println(device.deviceType);
if (device.valueVariables.lastKnownValues.size() > 0) {
LastKnownValue lkv = device.valueVariables.lastKnownValues.get(0);
System.out.println(lkv.value);
}
}
}
@XmlRootElement(name="DeviceList")
public class DeviceList {
@XmlAttribute(name="Devid")
public String devid;
@XmlAttribute(name="Count")
public Integer count;
@XmlElement(name="Device")
public List<Device> devices;
}
public class Device {
@XmlAttribute(name="ID")
public String id;
@XmlElement(name="Name")
public String name;
@XmlElement(name="DeviceType")
public String deviceType;
@XmlElement(name="ValueVariables")
public ValueVariables valueVariables;
}
public class ValueVariables {
@XmlAttribute(name="Count")
public Integer count;
@XmlElement(name="LastKnownValue")
public List<LastKnownValue> lastKnownValues;
}
public class LastKnownValue {
@XmlAttribute(name="Index")
public Integer index;
@XmlAttribute(name="EndPoint")
public Integer endPoint; // maybe a String?
@XmlAttribute(name="Name")
public String name;
@XmlAttribute(name="Type")
public String type;
@XmlValue
public String value;
}