You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by Andy Roberts <ma...@andy-roberts.net> on 2005/04/22 18:26:31 UTC
Digester and simple XML files
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