我试图打上Xml文档,但似乎不理解它是如何运作的。 我已经花了几个小时,但似乎无法得到正确的价值。 我已设法使该守则为跟踪名单标签工作,但并非为反馈名单标签及其儿童标签工作。
I m would like to have the values of a playback device, in the future more will be added.
这是Xml来源:
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<tracklist>
<track>track001.mp3</track>
<track>track002.mp3</track>
<track>track003.mp3</track>
<track>track004.mp3</track>
<track>track005.mp3</track>
<track>track006.mp3</track>
<track>track007.mp3</track>
<track>track008.mp3</track>
<track>track009.mp3</track>
<track>track010.mp3</track>
</tracklist>
<playbacklist>
<playback>
<name>Speaker1</name>
<ip>192.168.1.103</ip>
<room>Kitchen</room>
<options>0</options>
<state>NotPlaying</state>
</playback>
</playbacklist>
</root>
这是《 j法》(法典版):该法典正在为我工作。
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(inStream);
NodeList nodeList = doc.getElementsByTagName("root");
for (int index = 0; index < nodeList.getLength(); index++) {
Node node = nodeList.item(index);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
NodeList nameNode = element.getChildNodes();
for (int iIndex = 0; iIndex < nameNode.getLength(); iIndex++) {
if (nameNode.item(iIndex).getNodeType() == Node.ELEMENT_NODE) {
Element nameElement = (Element) nameNode.item(iIndex);
if(nameElement.getNodeName().equals("tracklist")){
NodeList trackNodes = nameElement.getChildNodes();
for(int i=0;i<trackNodes.getLength();i++){
if (trackNodes.item(i).getNodeType() == Node.ELEMENT_NODE) {
Element trackElement = (Element) trackNodes.item(i);
playlist.add(trackElement.getFirstChild().getNodeValue());
}
}
}
这部法典对我来说是徒劳的:
if(nameElement.getNodeName().equals("playbacklist")){
NodeList devicesNodes = nameElement.getChildNodes();
for(int j=0;j<=devicesNodes.getLength();j++){
Node nodeDevice = devicesNodes.item(j);
NodeList childNodes = nodeDevice.getChildNodes();