|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
removing element from documentHello All,
I need to remove all the Elements from a Document who have a particular attribute w/ a particular value. My first (unsuccessful) attempt was Element#removeContent w/in an iterator loop which throws a ConcurrentModificationException. Can someone suggest to me the correct way to do this sort of thing? Thanks! -- john List features = root.getChild("RESPONSE").getChild("FEATURES").getChildren("FEATURE"); for (Iterator i=features.iterator();i.hasNext();) { feature = (Element) i.next(); fields = feature.getChild("FIELDS").getChildren("FIELD"); for (Iterator j=fields.iterator(); j.hasNext();) { field = (Element)j.next(); if ("#SHAPE#".equals(field.getAttributeValue("name"))) { //throws ConcurrentModificationException feature.getChild("FIELDS").removeContent(field); } } } _______________________________________________ To control your jdom-interest membership: http://www.jdom.org/mailman/options/jdom-interest/youraddr@... |
|
|
Re: removing element from documentSorry to reply to myself, but the following XPath approach seems to
work - is the the recommended solution? -- john List nodesToDelete = XPath.selectNodes(root, "//FIELD[@name='#SHAPE#']"); int size = nodesToDelete.size(); for (int i=0; i<size; i++){ ((Element)nodesToDelete.get(i)).detach(); } John Cartwright wrote: > Hello All, > > I need to remove all the Elements from a Document who have a > particular attribute w/ a particular value. My first (unsuccessful) > attempt was Element#removeContent w/in an iterator loop which throws a > ConcurrentModificationException. > > Can someone suggest to me the correct way to do this sort of thing? > > Thanks! > > -- john > > List features = > root.getChild("RESPONSE").getChild("FEATURES").getChildren("FEATURE"); > for (Iterator i=features.iterator();i.hasNext();) { > feature = (Element) i.next(); > fields = feature.getChild("FIELDS").getChildren("FIELD"); > for (Iterator j=fields.iterator(); j.hasNext();) { > field = (Element)j.next(); > if ("#SHAPE#".equals(field.getAttributeValue("name"))) { > //throws ConcurrentModificationException > feature.getChild("FIELDS").removeContent(field); > } > } } > > _______________________________________________ > To control your jdom-interest membership: > http://www.jdom.org/mailman/options/jdom-interest/youraddr@... To control your jdom-interest membership: http://www.jdom.org/mailman/options/jdom-interest/youraddr@... |
|
|
RE: removing element from documentJust change the inside of the loop from:
//throws ConcurrentModificationException feature.getChild("FIELDS").removeContent(field); to: //throws ConcurrentModificationException j.remove(); It is 'well-documented' that if you have an integrator, you can't change the collection apon which the iterator is based, unless you use the iterator to make the change. See the ConcurrentModificationException javadoc. Rolf -----Original Message----- From: jdom-interest-bounces@... [mailto:jdom-interest-bounces@...] On Behalf Of John Cartwright Sent: Thursday, December 06, 2007 2:10 PM To: jdom-interest@... Subject: [jdom-interest] removing element from document Hello All, I need to remove all the Elements from a Document who have a particular attribute w/ a particular value. My first (unsuccessful) attempt was Element#removeContent w/in an iterator loop which throws a ConcurrentModificationException. Can someone suggest to me the correct way to do this sort of thing? Thanks! -- john List features = root.getChild("RESPONSE").getChild("FEATURES").getChildren("FEATURE"); for (Iterator i=features.iterator();i.hasNext();) { feature = (Element) i.next(); fields = feature.getChild("FIELDS").getChildren("FIELD"); for (Iterator j=fields.iterator(); j.hasNext();) { field = (Element)j.next(); if ("#SHAPE#".equals(field.getAttributeValue("name"))) { //throws ConcurrentModificationException feature.getChild("FIELDS").removeContent(field); } } } _______________________________________________ To control your jdom-interest membership: http://www.jdom.org/mailman/options/jdom-interest/youraddr@... -------------------------------------------------------- This email and any files transmitted with it are confidential and proprietary to Algorithmics Incorporated and its affiliates ("Algorithmics"). If received in error, use is prohibited. Please destroy, and notify sender. Sender does not waive confidentiality or privilege. Internet communications cannot be guaranteed to be timely, secure, error or virus-free. Algorithmics does not accept liability for any errors or omissions. Any commitment intended to bind Algorithmics must be reduced to writing and signed by an authorized signatory. -------------------------------------------------------- _______________________________________________ To control your jdom-interest membership: http://www.jdom.org/mailman/options/jdom-interest/youraddr@... |
|
|
Re: removing element from documentWhatever works for you my friend. I use the XPath approach myself.
Another way would be to use XSLT - if you can understand it. Manipulating XML is like a London tube map; there are many valid ways to get to the same destination. Some useful links: http://www.edankert.com/defaultnamespaces.html http://www.zvon.org/xxl/XPathTutorial/General/examples.html
|
| Free Forum Powered by Nabble | Forum Help |