• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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
 
Message for you sir! I think it is a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic