lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Roberts <m...@andy-roberts.net>
Subject Digester and simple XML files
Date Fri, 22 Apr 2005 16:26:31 GMT
Hi all,

Just been playing with Digester after reading chapter 7 in LIA. Seems to fit 
my needs as I have a relatively simple XML structure. 

<xml>
	<header>
		<!-- meta data about the file here. This is not important to me. -->
	</header>

	<text>
		<front>
			<!-- info here too but it's nothing I care about -->
		</front>
		<body>
			<p>some sentences</p>
			<p>some more sentences</p>
		</body>
	</text>
</xml>

Now, I want the text that's found between the <p> tags within the body 
section. So, I wrote a little test class using a largely bastardised version 
of DigesterXMLHandler from LIA.

I'm a tad confused to say the least. I create a Paragraph object upon seeing 
text/body. Then, call setText when I see the next <p> tag. And that's all I 
want per object, so I've added the addSetNext to call printParagraph which I 
hope prints the previous paragraph contents. But it doesn't!

My code looks so wrong but I've been hacking at it for a while with little 
fun. Any suggestions?

Thanks,
Andy

public class DigesterTest {
    
    private Digester dig;
    
    public DigesterTest(File inFile) throws IOException, SAXException {
        dig = new Digester();
        dig.setValidating(false);
              
        dig.addObjectCreate("text/body/", Paragraph.class);

        
        dig.addCallMethod("text/body/p", "setText", 0);
        
        dig.addSetNext("text/body/p", "printParagraph");
        
        System.out.println(inFile);
        dig.parse(inFile);
        
    }
    
    public void printParagraph(Paragraph p) {
	    System.out.println(p.getText());
	}
    
   
    public static void main(String[] args) {
        try {
            new DigesterTest(new File(args[0]));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }
    public class Paragraph {
    
    	private String text;
    
    	public Paragraph() {
    	}
    
    	public String getText() {
    	    return text;
    	}
    
    	public void setText(String inText) {
        
    	    if (inText != null) {
    	        text = inText;
    	    }
    	}
    	
   }    

}

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Mime
View raw message