• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

want to remove node from dom

 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following xml document and i want to remove the element sub_element with the value of test2.

<element>
<element-name>Readers</element-name>
<dynamic>no</dynamic>
<html>readers.html</html>
</element>
<element>
<element-name>test</element-name>
<dynamic>yes</dynamic>
<html>leftbankart[1].html</html>
<sub_element>test2</sub_element>
</element>

However when I do it removes the entire element node and i end up with

<element>
<element-name>Readers</element-name>
<dynamic>no</dynamic>
<html>readers.html</html>
</element>

dom = documentBuilder.parse(in);
node = XPathAPI.selectSingleNode(dom,"/elements/element[sub_element='" + value + "']");
Node parentNode = node.getParentNode();
parentNode.removeChild(node);

What am I doing wrong? Thank you for your time
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that the XPath expression "/elements/element[sub_element='foo']" translates to "I want all 'element' elements with a 'sub_element' child having value 'foo'"
 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I simply get the sub_element element whit a value "foo"?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try "/elements/element/sub_element[current()='foo']"
 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that doesnt work it returns null
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah. So it seems, but "/elements/element/sub_element[text() = 'foo']" doesn't...
 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for your time that worked
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic