commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Rezaul Hoque <rho...@gmail.com>
Subject Problem facing parse xml file with BeanReader in betwixt
Date Mon, 08 Nov 2004 02:54:21 GMT
Hello,

I am facing problem with Parsing xml file through BeanReader. Here are
my classes.

----StandardMenuItem.java----

public class StandardMenuItem {
	private String idx;
	private String icon;
	private String link;
	private String statusText;
	private String tooltip;
	private String value;

	public StandardMenuItem() {
		this.idx = "";
		this.icon = "";
		this.link = "";
		this.statusText = "";
		this.tooltip = "";
		this.value = "";
	}
	public void setIdx(String idx) { this.idx = idx; }
	public String getIdx() { return idx;}
	public void setIcon(String icon) { this.icon = icon; }
	public String getIcon() { return icon; }
	public void setLink(String link) { this.link = link;}
	public String getLink() { return link;}
	public void setStatusText(String statusText) { this.statusText = statusText;}
	public String getStatusText() { return statusText; }
	public void setTooltip(String tooltip) { this.tooltip = tooltip; }
	public String getTooltip() { return tooltip; }
	public void setValue(String value) { this.value = value; }
	public String getValue() { return value; }
}

----StandardMenuItemListBean.java---

import java.util.List;
import java.util.ArrayList;

public class StandardMenuItemListBean
{
	private List menuItemList;
	public StandardMenuItemListBean() { this.menuItemList = new ArrayList(); }
	public List getStandardMenuItemList() { return menuItemList; }
	public void addStandardMenuItem(StandardMenuItem menuItem) {
menuItemList.add(menuItem); }
}


----StandardMenuItemListBean.betwixt---

<?xml version="1.0" encoding="UTF-8" ?>
<info>
<element name="menu" />
<addDefaults />
</info>

----TestMenuWrite.java----

import java.io.*;
import java.beans.IntrospectionException;

import org.apache.commons.betwixt.BindingConfiguration;
import org.apache.commons.betwixt.io.BeanWriter;
import org.xml.sax.SAXException;

public class TestMenuWrite {

public static void main(String args[]) {
new TestMenuWrite().writeMenuFile();
}

public void writeMenuFile() {
try {
PrintWriter pw = new PrintWriter(new BufferedWriter(new
FileWriter("menu.xml")));
BeanWriter writer = new BeanWriter(pw);
writer.writeXmlDeclaration("<?xml version='1.0' encoding='UTF-8'?>");
writer.getBindingConfiguration().setMapIDs(false);

StandardMenuItemListBean menu = new StandardMenuItemListBean();

StandardMenuItem item = new StandardMenuItem();
item.setIdx("aboutUs");
item.setValue("About Us");
item.setLink("viewAboutUs");
item.setTooltip("about us");
item.setStatusText("[About Us]");
menu.addStandardMenuItem(item);

item = new StandardMenuItem();
item.setIdx("service");
item.setValue("Our Services");
item.setLink("viewServiceType");
item.setTooltip("services");
item.setStatusText("[Services]");
menu.addStandardMenuItem(item);
System.out.println(menu.getStandardMenuItemList().size());

writer.write(menu);
pw.flush();
pw.close();
//writer.enablePrettyPrint();
} catch(IOException e) {
System.err.println("IOException: " + e.getMessage());
} catch(SAXException e) {
System.err.println("SAXException: " + e.getMessage());
} catch(IntrospectionException e) {
System.err.println("IntrospectionException: " + e.getMessage());
}
}
}

----TestMenuRead.java----

import java.io.*;

import java.beans.IntrospectionException;
import org.apache.commons.betwixt.io.BeanReader;
import org.apache.commons.betwixt.BindingConfiguration;
import org.xml.sax.SAXException;

public class TestMenuRead {

public static void main(String args[]) {
new TestMenuRead().readMenu();
}

private void readMenu() {
try {
BeanReader reader = new BeanReader();
reader.registerBeanClass(StandardMenuItemListBean.class);
reader.getBindingConfiguration().setMapIDs(false);
File file = new File("menu.xml");
StandardMenuItemListBean menu = (StandardMenuItemListBean)reader.parse(file);
} catch(IOException e) {
System.err.println("IOException: " + e.getMessage());
} catch(SAXException e) {
e.printStackTrace();
} catch(IntrospectionException e) {
System.err.println("IntrospectionException: " + e.getMessage());
}
}
}

Now The menu.xml file creating nicely. So TestMenuWrite is working
well. But when I am trying to read from the menu.xml file through
TestMenuRead.java I am getting the follwing error message.

------------------------------
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
        at org.apache.commons.digester.Digester.createSAXException(Digester.java
:2792)
        at org.apache.commons.digester.Digester.createSAXException(Digester.java
:2818)
        at org.apache.commons.digester.Digester.endElement(Digester.java:1070)
        at org.apache.crimson.parser.Parser2.maybeElement(Unknown Source)
        at org.apache.crimson.parser.Parser2.parseInternal(Unknown Source)
        at org.apache.crimson.parser.Parser2.parse(Unknown Source)
        at org.apache.crimson.parser.XMLReaderImpl.parse(Unknown Source)
        at org.apache.commons.digester.Digester.parse(Digester.java:1556)
        at TestMenuRead.readMenu(TestMenuRead.java:20)
        at TestMenuRead.main(TestMenuRead.java:11)


Is anybody there who can help me how I can solve this problem.. I am
eagerly waiting.

Thanks in advance....

Babu

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


Mime
View raw message