我将从xml文件中读取数据并列出页面中元素的属性。 使用我的代码,您可以读取数据并将其存储在列表中。但不幸的是,数据绑定不起作用。请帮忙..
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();
}
}
}
向导
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>
答案 0 :(得分:1)
即使没有仔细阅读您的代码,我也想提一下,您正在努力工作以实现您的需求。使用JAXB将XML模式映射到java类。在15分钟内学习基础知识,在模型类上加上适当的注释,编写3-4行代码,然后就完成了。
修改
首先看一下本教程:http://www.vogella.com/tutorials/JAXB/article.html
或者在Web上搜索任何其他JAXB教程。