Hi Franz

I'm not sure I totally understand whats going wrong here. The same
ElementHandler object instance gets used by a SAXReader so I don't
understand whats going wrong.

Do you think you could make a JUnit test case (see
dom4j/src/test/org/dom4j/Test*.java for an example) that demonstrates the
problem you're having then we can try fix it?

FWIW I'd like to introduce a full XPath/XSLT pattern style ElementHandler
mechansim to provide more powerful parsing so this code will be reviewd &
updated soon.

James
----- Original Message -----
From: "Franz Beil" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, March 16, 2002 12:07 PM
Subject: [dom4j-user] loosing embedded SAXReader ElementHandler


> Hi,
>
> this is about a problem with an embedded ElementHandler added to a
SAXReader
> called in a main ElementHandler.
>
> I have a project in which I want to construct a DOM from a main XML file
> that contains <import name="href"/> tags for component files. To do this I
> added an ElementHandler to the SAXReader that reads the main file. This
> MainHandler calls another SAXReader to read component files. The <import
> .../> elements are to be replaced by the component root elements.
> Since I have to do some modification of the component DOMs, I added
another
> ElementHandler (ComponentHandler) to the component SAXReader.
> My problem occurs with the handler of the component reader: although the
> reader processes all imports, its handler seems to be lost after
processing
> of the first <import .../>.
>
> The very ugly workaround for the time being is to get a new SAXReader with
> added componentHandler on each call of MainHandler.onEnd().
>
> Did anyone encounter this or a similar problem. Any help or suggestions
are
> greatly appreciated.
> Best,
> Franz
>
> -------------
>
> This code skeleton illustrates the problem: only the /file/content
elements
> of the first imported file are handled (i.e. printed) although the
resulting
> DOM contains all imports. With the commented lines replacing their
> respective counterparts, all /file/content elements will be printed.
>
> public class MainHandler implements ElementHandler {
>
> SAXReader myReader;
>
>     public MainHandler() {
>         this.myReader = new SAXReader();
>         mainReader.addHandler("/file/content", new ComponentHandler());
>     }
>
>     public void onStart(ElementPath path) {}
>
>     public void onEnd(ElementPath path) {
>         String href = path.getCurrent().attribute("href").getValue();
>         Element importRef = path.getCurrent();
>         Element parentElement = curr.getParent();
> //        SAXReader onEndReader = new SAXReader();
> //        onEndReader.addHandler("/file/content", new ComponentHandler());
>
>         File file = new File(href);
>         try {
>             Element importElement = myReader.read(file).getRootElement();
> //            Element importElement =
> onEndReader.read(file).getRootElement();
>         } catch {}
>
>         // prune and replace
>         importRef.detach();
>         parentElement.add(importElement);
>     }
> }
>
> public class ComponentHandler implements ElementHandler {
>     public void onStart(ElementPath path) {
>
System.out.println(path.getCurrent().attribute("name").getValue());
>     }
>     public void onEnd(ElementPath path) {}
> }
>
>
> _______________________________________________
> dom4j-user mailing list
> [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/dom4j-user


_______________________________________________
dom4j-user mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/dom4j-user

Reply via email to