在這裏你可以看到我的應用程序: 如何使xml架構與JTable相關(鏈接),xml java?
所以我需要做的:
我不知道我可以通過代碼鏈接的XML模式與JTable中所有類型(整型,字符串,浮點)。比如說Year是int類型,Schema是int類型,我可以如何鏈接它?我不知道英文鏈接或關係如何。 在這個應用程序中,我將所有數據寫入xml文件,並且當應用程序加載時,它會從xml文件加載所有數據。
在這裏,我創建XML模式:
public void CreateSchema(String FileName){
file=FileName;
JAXBContext jc;
try {
jc = JAXBContext.newInstance(XmlSchemaType.class);
jc.generateSchema(new SchemaOutputResolver() {
@Override
public javax.xml.transform.Result createOutput(String namespaceURI, String suggestedFileName)throws IOException {
suggestedFileName=file+".xsd";
return new StreamResult(suggestedFileName);
}
});
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
}
這裏是所有類型:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Auto")
public class XmlSchemaType {
row[] Row;
}
class row {
@XmlAttribute
byte ID;
@XmlElement
String VIN;
@XmlElement
String Make;
@XmlElement
String Model;
@XmlElement
int Year;
@XmlElement
String Description;
@XmlElement
float Cost;
}
這裏被寫入到XML文件:
public void CreateXml(JTable tb,JTable tb2,String FileName){
try {
file=FileName;
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("Auto");
doc.appendChild(rootElement);
int i=0,j=0,k=0;
while (i<tb.getRowCount()){
j=0;
Element rows = doc.createElement("Row");
rootElement.appendChild(rows);
Attr attr = doc.createAttribute("id");
attr.setValue((i+1)+"");
rows.setAttributeNode(attr);
//Pirma lentele
while (j<tb.getColumnCount()-1){
Element element = doc.createElement(tb.getTableHeader().getColumnModel().getColumn(j).getHeaderValue()+"");
element.appendChild(doc.createTextNode(tb.getModel().getValueAt(i, j)+""));
rows.appendChild(element);
j++;
}
//Antra lentele
j=2;//pirmu lauku nereikia
while (j<tb2.getColumnCount()-1){
Element element2 = doc.createElement(tb2.getTableHeader().getColumnModel().getColumn(j).getHeaderValue()+"");
element2.appendChild(doc.createTextNode(tb2.getModel().getValueAt(i, j)+""));
rows.appendChild(element2);
if (j==2){
tb2.getModel().setValueAt(tb.getModel().getValueAt(i, 0),i,1);
}
j++;
}
i++;
}
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result;
try {
FileOutputStream fileOutputStream = null;
fileOutputStream = new FileOutputStream(
new File(file+".xml"));
result = new StreamResult(fileOutputStream);//new FileOutputStream(file+".xml"));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");//new line... kad butu naujoje eiluteje
transformer.transform(source, result);
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
}
//file.renameTo(FileName+".xml");
}
這裏裝載我的xml文件:
public void PopulateDataSet(JTable tb,JTable tb2,String FileName){
file=FileName;
File f= new File(file+".xml");
if (f.exists()){
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(f);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Row");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
DefaultTableModel model = (DefaultTableModel) tb.getModel();
model.addRow(new Object[] { "", "","","","Delete" });
tb.getModel().setValueAt(eElement.getElementsByTagName("VIN").item(0).getTextContent(),temp, 0);
tb.getModel().setValueAt(eElement.getElementsByTagName("Make").item(0).getTextContent(),temp, 1);
tb.getModel().setValueAt(eElement.getElementsByTagName("Make").item(0).getTextContent(),temp, 2);
tb.getModel().setValueAt(eElement.getElementsByTagName("Year").item(0).getTextContent(),temp, 3);
tb.getModel().setValueAt("Delete",temp, 4);
DefaultTableModel model2 = (DefaultTableModel) tb2.getModel();
model2.addRow(new Object[] { (tb2.getRowCount()+1), "","","","Delete" });
tb2.getModel().setValueAt(eElement.getElementsByTagName("VIN").item(0).getTextContent(),temp, 1);
tb2.getModel().setValueAt(eElement.getElementsByTagName("Description").item(0).getTextContent(),temp, 2);
tb2.getModel().setValueAt(eElement.getElementsByTagName("Cost").item(0).getTextContent(),temp, 3);
tb2.getModel().setValueAt("Delete",temp, 4);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (!f.exists()){
CreateXml(tb,tb2,file);
CreateSchema(file);
}
}
但如何使用xml shema與JTable,xml?
你想從XML模式中獲得什麼?基於XML的驗證?具有編組/解組支持的JAXB風格的類映射?還有別的嗎? – kjhughes 2014-09-21 21:28:25