[From nobody Sat Mar 12 08:59:41 2005 Return-Path: <jdom-interest-bounces@jdom.org> Delivered-To: el@grit.de Received: from localhost (localhost [127.0.0.1]) by mail.grit.de (Postfix) with ESMTP id CB3A737079 for <el@grit.de>; Sat, 12 Mar 2005 17:59:04 +0100 (CET) Received: from servlets.kattare.com (servlets.com [65.212.180.182]) by mail.grit.de (Postfix) with ESMTP id 97ACF37074 for <el@grit.de>; Sat, 12 Mar 2005 17:59:03 +0100 (CET) Received: from servlets.kattare.com (localhost [127.0.0.1]) by servlets.kattare.com (8.12.10/8.12.11) with ESMTP id j2CGc3Ke029744; Sat, 12 Mar 2005 08:38:05 -0800 Received: from csmtp.b-one.net (csmtp.b-one.net [195.47.247.21]) by servlets.kattare.com (8.12.10/8.12.11) with ESMTP id j2CGbxCI027839 for <jdom-interest@jdom.org>; Sat, 12 Mar 2005 08:38:00 -0800 Received: from [10.0.1.129] (unknown [84.217.11.189]) by csmtp.b-one.net (Postfix) with ESMTP id 430FAFB9A0; Sat, 12 Mar 2005 17:37:55 +0100 (CET) Message-ID: <42331ADE.2020902@austers.se> Date: Sat, 12 Mar 2005 17:37:50 +0100 From: Per Norrman <per.norrman@austers.se> User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: sv, en-us, en MIME-Version: 1.0 To: =?ISO-8859-1?Q?Kai_W=F6rner?= <kai.woerner@uni-hamburg.de> Subject: Re: [jdom-interest] removing pcdata from jdom-Elements References: <200503111336.j2BDa0mw077502@rzaixsrv2.rrz.uni-hamburg.de> In-Reply-To: <200503111336.j2BDa0mw077502@rzaixsrv2.rrz.uni-hamburg.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Cc: jdom-interest@jdom.org X-BeenThere: jdom-interest@jdom.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: JDOM Mailing List for General Issues and Updates <jdom-interest.jdom.org> List-Unsubscribe: <http://www.jdom.org/mailman/listinfo/jdom-interest>, <mailto:jdom-interest-request@jdom.org?subject=unsubscribe> List-Archive: <http://www.jdom.org/pipermail/jdom-interest> List-Post: <mailto:jdom-interest@jdom.org> List-Help: <mailto:jdom-interest-request@jdom.org?subject=help> List-Subscribe: <http://www.jdom.org/mailman/listinfo/jdom-interest>, <mailto:jdom-interest-request@jdom.org?subject=subscribe> Sender: jdom-interest-bounces@jdom.org Errors-To: jdom-interest-bounces@jdom.org X-Virus-Scanned: by AMaViS 0.3.12pre8 Hi, Document.getDescendants returns an iterator that uses other iterators internally, so I think you'll be getting concurrent modification exceptions with your approach. An approach that works is to 'manually' traverse the tree an 'rebuilding' the content for each element. Somthing like this: private static List makeList(Text text) { List l = new ArrayList(); StringTokenizer st = new StringTokenizer(text.getText()); while(st.hasMoreTokens()) { Element w = new Element("w"); w.setText(st.nextToken()); l.add(w); } return l; } private static void process(Element element) { List content = new ArrayList(); for (Iterator i = element.removeContent().iterator(); i.hasNext();) { Object o = i.next(); System.out.println(o); if (o instanceof Element) { Element e = (Element) o; process(e); content.add(e); } else if (o instanceof Text){ content.addAll(makeList((Text)o)); } else { content.add(o); } } element.setContent(content); } public static void main(String[] args) throws Exception { String xml = "<s>someone said: <q>this sucks bigtime</q> and i agreed</s>"; Document doc = new SAXBuilder().build(new StringReader(xml)); process(doc.getRootElement()); new XMLOutputter().output(doc, System.out); } Kai Wörner skrev: > Hi all, > > I want to do this to a XML-Document: > > (before:) > <s>someone said: <q>this sucks bigtime</q> and i agreed</s> > > (after:) > <s><w>someone</w><w>said:</w><q><w>this</w><w>sucks</w><w>bigtime</w></q><w> > and</w><w>i</w><w>agreed</w></s> > > I thought I'll get all Elements via > > Iterator myI = doc.getDescendants(new ElementFilter()); > > iterate through them, look for PCDATA via Element.getText, chop it with a > StringTokenizer, add the Tokens as new <w>-Elements to the actual Element > and get rid of the PCDATA itself. But how do I do this? Is there something > like Element.removeContent(new onlyThePCDATAContentSparingElementsFilter())? > > Thanks > > Kai > > _______________________________________________ > To control your jdom-interest membership: > http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com > _______________________________________________ To control your jdom-interest membership: http://www.jdom.org/mailman/options/jdom-interest/youraddr@yourhost.com ]