• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

want to remove node from dom

 
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would I simply get the sub_element element whit a value "foo"?
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try "/elements/element/sub_element[current()='foo']"
 
john mattucci
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that doesnt work it returns null
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your time that worked
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic