Jason, Thanks for your response! I'm probably doing something wrong then. The source is almost self-explaining. I use a properties-file to supply the xpath-expressions. The keys of it are the expressions, the values are not used.
Greetings, Edwin ---------------------------------------------------------------------------- ------------------------------------------------------ import org.dom4j.io.*; import org.dom4j.*; import java.util.*; import java.io.*; public class DomFilter { public Document in_doc; public Document out_doc; public Element in_root; public Element out_root; public String output_file; public Properties filter_data; private DocumentFactory factory = DocumentFactory.getInstance(); public DomFilter( String input_file, String filter_file, String output_file ) { try { SAXReader saxReader = new SAXReader(); in_doc = saxReader.read( new FileInputStream( input_file ) ); String root_name = in_doc.getRootElement().getQualifiedName(); out_doc = DocumentFactory.getInstance().createDocument(); out_doc.addElement( root_name ); out_root = out_doc.getRootElement(); filter_data = new Properties(); filter_data.load(new FileInputStream(filter_file)); Enumeration filter_keys = filter_data.propertyNames(); while (filter_keys.hasMoreElements()) { String filter_key = (String)filter_keys.nextElement(); filter( filter_key ); } FileOutputStream out = new FileOutputStream( output_file ); OutputFormat outformat = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(out, outformat); writer.write(out_doc); writer.flush(); } catch (Exception e) { e.printStackTrace(); } this.output_file = output_file; } public void filter( String filter_key ) { List nodes = in_doc.selectNodes( filter_key ); for (int i=0; i<nodes.size(); i++) { Node node = (Node)nodes.get(i); if (node.getNodeType()==Node.ELEMENT_NODE) { Element el = (Element)node; Element el_copy = el.createCopy(); add_to_result( el_copy, el.getPath() ); } } } public void add_to_result( Element el, String path ) { path = path.substring(0,path.lastIndexOf("/")); StringTokenizer st = new StringTokenizer( path,"/" ); Element parent_el=out_root; String expr = "/"+st.nextToken(); while (st.hasMoreTokens()) { String el_name = st.nextToken(); expr += "/" + el_name; List nodes = parent_el.selectNodes( expr ); if (nodes.size()!=0) { parent_el = (Element)nodes.get(0); } else { parent_el = parent_el.addElement( el_name ); } } parent_el.add( el ); } public static void logMessage(String msg) { System.out.println( msg ); } public static void usage() { System.out.println("DomFilter <input_file> <filter_file> <output_file>"); } public static void main( String[] args ) { if (args.length<3) { usage(); return; } new DomFilter( args[0], args[1], args[2] ); } } ---------------------------------------------------------------------------- ---------------------------------------------------- > ---------- > From: Jason Stortz[SMTP:[EMAIL PROTECTED]] > Sent: donderdag 1 augustus 2002 12:56 > To: Bulter, Edwin; [EMAIL PROTECTED] > Subject: RE: [dom4j-user] XPATH expression problem: Am I trying to > use an unimplemented feature? > > Edwin, > > I do this kind of stuff all the time and it works. > As a matter of fact I exclusively use subelement selections > instead of attributes because I read somewhere, who knows > where, that it is faster (can anyone confirm this?). > > Soo...can you post your java method, maybe I can try > running it over here? > > It should work! > > -jason > > -----Original Message----- > From: Bulter, Edwin [mailto:[EMAIL PROTECTED]] > Sent: Thursday, August 01, 2002 4:19 AM > To: '[EMAIL PROTECTED]' > Subject: [dom4j-user] XPATH expression problem: Am I trying to use an > unimplemented feature? > > > I am using the xpath-syntax for filtering an XML-document and pass the > result through. > To filter the nodes I use the method "public List selectNodes(String > xpathExpression)" on my document. > > Example : > <AAA> > <BBB A="1"> > <C>a</C> > </BBB> > <BBB A="2"> > <C>b</C> > </BBB> > <BBB A="3"> > <C>c</C> > </BBB> > </AAA> > > Selecting an element on attribute-values works fine, i.e.: > expression: > /AAA/BBB[@A='2'] > result: > <BBB A="2"> > <C>b</C> > </BBB> > > Selecting an element on the element-text of an inner-element is a problem, > i.e.: > expression: > /AAA/BBB[C='b'] > result: > <BBB A="1"> > <C>a</C> > </BBB> > <BBB A="2"> > <C>b</C> > </BBB> > <BBB A="3"> > <C>c</C> > </BBB> > > In this last case I would expect only the second BBB-element as the > result. > > I used xpath-expressions like this before in XSL-documents which worked > fine. > Why doesn't it with DOM4J? > > Regards, > > Edwin > > **********Internet Email Confidentiality Footer********** > Privileged/Confidential Information may be contained in this message. If > you are not the addressee indicated in this message (or responsible for > delivery of the message to such person), you may not copy or deliver this > message to anyone. In such case, you should destroy this message and > kindly > notify the sender by reply email. Please advise immediately if you or your > employer do not consent to Internet email for messages of this kind. > Opinions, conclusions and other information in this message that do not > relate to the official business of my firm shall be understood as neither > given nor endorsed by it. > > > ------------------------------------------------------- > This sf.net email is sponsored by:ThinkGeek > Welcome to geek heaven. > http://thinkgeek.com/sf > _______________________________________________ > dom4j-user mailing list > [EMAIL PROTECTED] > https://lists.sourceforge.net/lists/listinfo/dom4j-user > > **********Internet Email Confidentiality Footer********** Privileged/Confidential Information may be contained in this message. If you are not the addressee indicated in this message (or responsible for delivery of the message to such person), you may not copy or deliver this message to anyone. In such case, you should destroy this message and kindly notify the sender by reply email. Please advise immediately if you or your employer do not consent to Internet email for messages of this kind. Opinions, conclusions and other information in this message that do not relate to the official business of my firm shall be understood as neither given nor endorsed by it. ------------------------------------------------------- This sf.net email is sponsored by:ThinkGeek Welcome to geek heaven. http://thinkgeek.com/sf _______________________________________________ dom4j-user mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/dom4j-user