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

Reply via email to