Reputation: 13
I will read the data from an xml file and list the properties of the elements in a page. With my code you can already read the data and store them in a list. But unfortunately, data binding does not work. Please help ..
bookHandler
public class bookHandler implements ContentHandler {
private String currentValue;
boolean tablecaption;
private boolean bookObjectCreated = false;
private boolean inbookSection = false;
bookObject currentbookObject;
private boolean inbody = false;
private boolean namebook = false;
private boolean authbook = false;
private List<bookObject> booksList = null;
public List<bookObject> getbookList() {
return booksList;
}
public void characters(char[] ch, int start, int length) throws SAXException {
currentValue = new String(ch, start, length);
}
public void startElement(String uri, String localName, String qname, Attributes atts) throws SAXException {
if ((localName.equals("TABLE-PRE")) && (atts.getValue("ID").equals("action"))) {
inbookSection = true;
}
if (inbookSection) {
if (localName.equals("BODY")) {
inbody = true;
}
}
if (inbody) {
if (localName.equals("ROW")) {
bookObjectCreated = true;
currentbookObject = new bookObject();
if (booksList == null)
booksList = new ArrayList<bookObject>();
}
if (bookObjectCreated) {
if (localName.equals("ENTRY") && (atts.getValue("COLNAME").equals("col1"))) {
namebook = true;
} else if (localName.equals("ENTRY") && (atts.getValue("COLNAME").equals("col2"))) {
authbook = true;
}
}
}
}
public void endElement(String uri, String localName, String atts) throws SAXException {
if (bookObjectCreated) {
if (namebook) {
if (atts.equals("P")) {
currentbookObject.setName(currentValue);
namebook = false;
}
}
else if (authbook) {
if (atts.equals("P")) {
currentbookObject.setAuth(currentValue);
authbook = false;
}
}
if (atts.equals("ROW")) {
bookObjectCreated = false;
booksList.add(currentbookObject);
authbook = false;
}
}
if (atts.equals("BODY")) {
inbody = false;
inbookSection = false;
}
}
bookObject.java
public class bookObject {
private String name;
private String Auth;
public bookObject() {
}
public String getName() {
return name;
}
public String getAuth() {
return Auth;
}
public void setName(String name) {
this.name = name;
}
public void setAuth(String status) {
this.Auth = status;
}
}
Main.java
public class Main {
private String m_InputPath = "bspXml.xml";
public void createBooks() {
// m_ResultGroups = new ResultGroups();
try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
FileReader reader = new FileReader(m_InputPath);
InputSource inputSource = new InputSource(reader);
xmlReader.setContentHandler(new bookHandler());
xmlReader.parse(inputSource);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
}
}
Wizard
public Wizard(App app) {
super("wizardPage");
//books
App.createBooks();
}
public void createControl(Composite parent) {
Composite container = new Composite(parent, SWT.NULL);
setControl(container);
container.setLayout(new BorderLayout(0, 0));
checkboxTableViewer = CheckboxTableViewer.newCheckList(container, SWT.BORDER | SWT.FULL_SELECTION);
checkboxTableViewer.setAllGrayed(true);
checkboxTableViewer.setAllChecked(false);
table = checkboxTableViewer.getTable();
table.setHeaderVisible(true);
table.setLayoutData(BorderLayout.CENTER);
tableViewerColumn = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
tblclmnName = tableViewerColumn.getColumn();
tblclmnName.setWidth(255);
tblclmnName.setText("Name");
tableViewerColumn_1 = new TableViewerColumn(checkboxTableViewer, SWT.NONE);
tblclmnVariant = tableViewerColumn_1.getColumn();
tblclmnVariant.setWidth(122);
tblclmnVariant.setText("Author");
m_bindingContext = iDataBindings();
}
protected DataBindingContext iDataBindings() {
DataBindingContext bindingContext = new DataBindingContext();
//
IObservableValue observeTextTblclmnNameObserveWidget = WidgetProperties.text().observe(tblclmnName);
IObservableValue namebookObjectObserveValue = PojoProperties.value("name").observe(bookObject);
bindingContext.bindValue(observeTextTblclmnNameObserveWidget, namebookObjectObserveValue, null, null);
//
IObservableValue observeTextTblclmnVariantObserveWidget = WidgetProperties.text().observe(tblclmnVariant);
IObservableValue statusbookObjectObserveValue = PojoProperties.value("author").observe(bookObject);
bindingContext.bindValue(observeTextTblclmnVariantObserveWidget, statusbookObjectObserveValue, null, null);
//
return bindingContext;
}
}
xml
<?xml version="1.0" encoding="iso-8859-1" ?>
<BOOK>
<TABLE>
<TABLE-PRE ID="sinceBook">
<NAME>sinceBook</NAME>
</TABLE-PRE>
</TABLE>
<TABLE>
<TABLE-PRE ID="actionBook">
<NAME>actionBook</NAME>
</TABLE-PRE>
<TABLEGR COLS="2">
<COLSPE COLNUM="1" COLWIDTH="4.31*" COLNAME="col1"/>
<COLSPE COLNUM="2" COLWIDTH="1.00*" COLNAME="col2"/>
<TD>
<ROW>
<ENTRY COLNAME="col1">
<P>Name</P>
</ENTRY>
<ENTRY COLNAME="col2">
<P>Author</P>
</ENTRY>
</ROW>
</TD>
<BODY>
<ROW>
<ENTRY COLNAME="col1">
<P>Harry Potter </P>
</ENTRY>
<ENTRY COLNAME="col2">
<P>Joanne K. Rowling</P>
Upvotes: 0
Views: 62
Reputation: 115328
Even without reading your code carefully I would like to mention that you are working too hard to achieve what you need. Use JAXB to map XML schema to java class. Learn the basics during 15 minutes, put appropriate annotations on your model classes, write 3-4 lines of code and you are done.
EDIT
Take a look on this tutorial first: http://www.vogella.com/tutorials/JAXB/article.html
Or alternatively search for any other JAXB tutorial on web.
Upvotes: 1