aspose file tools*
The moose likes XML and Related Technologies and the fly likes want to remove node from dom Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "want to remove node from dom" Watch "want to remove node from dom" New topic
Author

want to remove node from dom

john mattucci
Ranch Hand

Joined: Nov 03, 2000
Posts: 331
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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'"


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
john mattucci
Ranch Hand

Joined: Nov 03, 2000
Posts: 331
How would I simply get the sub_element element whit a value "foo"?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Try "/elements/element/sub_element[current()='foo']"
john mattucci
Ranch Hand

Joined: Nov 03, 2000
Posts: 331
that doesnt work it returns null
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Ah. So it seems, but "/elements/element/sub_element[text() = 'foo']" doesn't...
john mattucci
Ranch Hand

Joined: Nov 03, 2000
Posts: 331
thank you for your time that worked
 
 
subject: want to remove node from dom