简体   繁体   中英

Cannot add attribute to an element in XML

I want to add an attribute "driver" to an element "testDetails" in my XML file. I have few elements testDetails in the XML and I want to add the attribute to the last one. I created this:

Element testDetails;        
NodeList findRootElementList = document.getElementsByTagName("testDetails");
int iterator = findRootElementList.getLength();
Node node = findRootElementList.item(iterator);
testDetails = (Element)node;
testDetails.setAttribute("driver", driver)

;

But the variable testDetails is null at testDetails.setAttribute("driver", driver);

But when I previously had this:

Element testSuite = null;       
        NodeList findRootElementList = document.getElementsByTagName("testSuite");
        for(int iterator = 0; iterator < findRootElementList.getLength(); iterator++) {
            Node node = findRootElementList.item(iterator);
            if(node.getNodeName().equals("testSuite")) {
                testSuite = (Element)node;
            }

this variable (here it is called testSuite ) was not null, the line testSuite = (Element)node; work fine here.

What is the difference here? Why won't it set the node to testDetails ?

使用getLength() - 1因为索引基于零,所以例如5的列表中的最后一项具有索引4。

list的最后一个索引= len(list)-1因为list的索引从0开始,长度从1开始。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM