logo       

Re: DOM : removeall nodes with xpath: msg#00029

mozilla.devel.dom

Subject: Re: DOM : removeall nodes with xpath



rvj wrote:

Ive attempted to use the currentNode.parentNode.removeChild(currentNode)
syntax you suggested.

The node is removed but the document appears to be corrupted AFTER the
deleted node.

Does the xmldom have to be refreshed or something. Have I misunderstood ?

I posted code example where XPath created a snapshot and then an iteration through the snapshot items removed them e.g.


var xmlDocument = document.implementation.createDocument('', 'root', null);
xmlDocument.addEventListener(
'load',
function testAttributeDeletion (evt) {
var xpathResult = xmlDocument.evaluate(
'//node[@about = "node:3"]',
xmlDocument,
null,
7,
null
);
for (var i = 0; i < xpathResult.snapshotLength; i++) {
var currentNode = xpathResult.snapshotItem(i);
currentNode.parentNode.removeChild(currentNode);
}
alert(new XMLSerializer().serializeToString(xmlDocument));
},
false
);
xmlDocument.load('test2004101202.xml')

If I do that with your example document below all is fine, the alert shows the XML document as expected with one <node> removed.

**************************************************

<?xml version="1.0"?>
<root>
<node about="node:1" />
<node about="node:2" />
<node about="node:3" label="hello"/>
<node about="node:4" />
<node about="node:5" />
</root>

***************************************************

<HTML>
<SCRIPT>
var xmldom= document.implementation.createDocument("","test",null)

function docloaded(e)
{
// select node:3 for deletion
xpath="//node[@about='node:3']"

nsResolver =xmldom.createNSResolver(xmldom.documentElement)
try
{
result = xmldom.evaluate(xpath, xmldom, nsResolver, 0, null);
}
catch(e)
{
alert("error"+e)
}
results = [];
while ( node = result.iterateNext ()) results.push ( node );

alert(results.length)

for (var i = 0; i < results.length; i++)
{
var currentNode = results[i]
try
{
currentNode.parentNode.removeChild(currentNode);
}
catch(e)
{
alert("delete error "+e)
}
}
alert("deleted node is "+results[0].getAttribute('label') )

node=xmldom.documentElement.firstChild.nextSibling // get the node's first
child !
while (node!=null)
{
if (node.tagName=="node")
{
alert("node= "+node.getAttribute('about'))
}
node=node.nextSibling.nextSibling
}
}

Why that odd loop to go through the document? I think the problem you run into is that you use nextSibling.nextSibling trying to move from element to element node however after the removal of one <node about="node:3" label="hello"/> element node there are two adjacent white space text nodes between
<node about="node:2" />
and
<node about="node:4" />
So the document is fine, only your loop is not adequate to loop through element nodes, consider using xmlDocument.getElementsByTagName('node') if you want to loop through the element nodes.

--

Martin Honnen
http://JavaScript.FAQTs.com/


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise