0
我試圖從代碼片段中的namednodemap中刪除nameditem。如果我刪除它跳過一些屬性,可能是地圖索引調整時,我刪除一個項目。從NamedNodeMap中刪除nameditem屬性
有關如何解決此問題的任何想法?
NamedNodeMap map = thisNode.getAttributes();
for (int i=0; i < map.getLength(); i++)
{
String itemName = map.item(i).getNodeName();
String itemValue = map.item(i).getNodeValue();
logger.debug("Attributes [" + itemName + ":" + itemValue + "]");
if (itemName.equals("xmlns:xsd") || itemName.equals("elementFormDefault") || itemName.equals("targetNamespace"))
{
logger.debug("Keep this attribute[" + itemName + "]");
}
else if (itemValue.contains(IDENTIFIER))
{
logger.debug("Keep this attribute [" + itemName + ":" + itemValue + "]");
}
else //if (removeThis)
{
// remove these attribute name spaces
logger.debug("Remove [" + itemName + "]");
if (itemName.equalsIgnoreCase(itemName))
{
//map.removeNamedItem(map.item(i).getNodeName());
}
}
}
我很困惑,爲什麼這會工作。理解這種行爲的任何解釋都會有幫助。 – Venu
如果您從列表中刪除具有索引i的節點,則以下節點(最初具有索引i + 1)將成爲索引爲i的新節點。在你的代碼中,這個節點不會被測試(因爲計數器我會被for循環增加)。向後計數消除了這一點。只需通過示例進行調試即可瞭解這一點。 – wero
這是有道理的,很好的解釋 – Venu