如何使xml架构与JTable,xml java相关联(链接)? [英] How to make xml schema relate(link) to JTable,xml java?

查看:121
本文介绍了如何使xml架构与JTable,xml java相关联(链接)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里你可以看到我的申请:



所以我需要做的事情:



我不知道如何通过代码链接xml架构所有类型的JTable(int,string,float)。比如说,Year是int类型,架构是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;
返回新的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;
}


类行{
@XmlAttribute
字节ID;

@XmlElement
String VIN;
@XmlElement
String Make;
@XmlElement
String Model;
@XmlElement
int年;
@XmlElement
字符串描述;
@XmlElement
浮动成本;
}

这里写的是xml文件:

  public void CreateXml(JTable tb,JTable tb2,String FileName){
try {

file = FileName;
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();


文件doc = docBuilder.newDocument();
元素rootElement = doc.createElement(Auto);
doc.appendChild(rootElement);


int i = 0,j = 0,k = 0;

while(i< tb.getRowCount()){

j = 0;
元素行= doc.createElement(行);
rootElement.appendChild(rows);

Attr attr = doc.createAttribute(id);
attr.setValue((i + 1)+);
rows.setAttributeNode(attr);

// Pirma lentele
while(j< tb.getColumnCount() - 1){

元素元素= 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){
元素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();
变压器变压器= transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult结果;
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;
文件f =新文件(文件+。xml);
if(f.exists()){
try {
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
文件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){

元素eElement =(元素)nNode;

DefaultTableModel model =(DefaultTableModel)tb.getModel();
model.addRow(new Object [] {,,,,删除});
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),,,,删除});
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(例外e){
e.printStackTrace();
}
}
if(!f.exists()){
CreateXml(tb,tb2,file);
CreateSchema(file);
}


}

但如何使用xml shema和JTable,xml?

解决方案

这里写的是xml文件: - --- 这里加载我的xml文件:



为什么你使用DOM来读写xml,当你已经使用JAXB Mapping。如果你正确地进行映射 1 ,那只需要使用

  • JAXB规格总数






  • 注意:使用上面的JAXB注释,您可以创建模式,并且您希望针对它验证xml,您可以在解组时设置模式。类似于:

      private static AutoModel unmarshal(String file)throws Exception {
    JAXBContext context = JAXBContext.newInstance(AutoModel) 。类);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = factory.newSchema(new File(src / table / autos.xsd));
    unmarshaller.setSchema(schema);
    AutoModel model =(AutoModel)unmarshaller.unmarshal(new File(file));
    返回模型;
    }


    Here you can see my application:

    So what i need to do:

    I dont know how I can through code link xml schema with JTable with all type(int,string,float). Say like Year is type int and in schema is int and how i can link it? I dont know how will be in english link or relate. In this application i write all data to xml file, and when application loaded it loads all data from xml file.

    Here i creating xml schema:

          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();
            }
    
      }
    

    Here is all types:

       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;
       }
    

    Here is writing to xml file:

    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");
    
       }
    

    And here loading my xml file:

        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); 
          }
    
    
      }
    

    But how to use xml shema with JTable, xml?

    解决方案

    "Here is writing to xml file:" ---- "And here loading my xml file:"

    Why are you using DOM to read and write the xml, when you are already using JAXB Mapping. If you are doing the mapping correctly 1, it's just a matter of using the Marshaller and Unmarshaller to write and read, respectively. Make sure to look at those API links yo see example usage. It's only around 5 lines of code to handle each operaion.

    (1) Please see the JAXB tutorial for more info about JAXB mapping.

    Also, you can just create your own AbstractTableModel and unmarshal and marshal straight to and from the table model. This is probably the most effective way to keep everything sync. Create a class Auto to represent each row, and a class AutoModel, that will be the root element in the xml document, as well as the TableModel for the JTable. Something like:

    Auto class

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "Auto", propOrder = {
            "id", "VIN", "Make", "Model", "Year", "Description", "Cost"
    })
    public class Auto {
        @XmlElement(name = "id")
        Integer id;
        @XmlElement(name = "VIN")
        String VIN;
        @XmlElement(name = "Make")
        String Make;
        @XmlElement(name = "Model")
        String Model;
        @XmlElement(name = "Year")
        Integer Year;
        @XmlElement(name = "Description")
        String Description;
        @XmlElement(name = "Cost")
        Float Cost;
    
        // === DON'T FORGET YOUR GETTERS and SETTERS
    }
    

    AutoModel class

    @XmlRootElement(name = "AutoList")
    public class AutoModel extends AbstractTableModel {
        String[] columnNames = {"VIN", "Make", "Model", "Year"};
    
        @XmlElement(name = "Auto")
        protected List<Auto> autos;
    
        public AutoModel() {
            autos = new ArrayList<Auto>();
        }
    
        @Override
        public int getRowCount() {
            return autos.size();
        }
    
        @Override
        public int getColumnCount() {
            return columnNames.length;
        }
    
        @Override
        public String getColumnName(int columnIndex) {
            return columnNames[columnIndex];
        }
    
        @Override
        public boolean isCellEditable(int row, int col) {
            return false;
        }
    
        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Auto auto = autos.get(rowIndex);
            Object value = null;
            switch (columnIndex) {
                case 0 : value = auto.getVIN(); break;
                case 1 : value = auto.getMake(); break;
                case 2 : value = auto.getModel(); break;
                case 3 : value = auto.getYear(); break;
            }
            return value;
        }
    }
    

    Test, using this xml file

    <?xml version="1.0" encoding="UTF-8"?>
    <AutoList>
        <Auto>
            <id>1</id>
            <VIN>123456788910FASDE</VIN>
            <Make>Mercedes</Make>
            <Model>CL 550</Model>
            <Year>2012</Year>
            <Description>Hello World</Description>
            <Cost>80000.00</Cost>
        </Auto>
    </AutoList>
    

    import java.awt.Dimension;
    import java.io.File;
    import javax.swing.*;
    import javax.xml.bind.*;
    
    public class TestTableMarshall {
    
        private static final String INPUT_FILE = "src/table/autos.xml";
        private static final String OUTPUT_FILE = "src/table/autos1.xml";
    
        public static void main(String[] args) throws Exception {
            AutoModel model = unmarshal(INPUT_FILE);
            JTable table = new JTable(model) {
                @Override
                public Dimension getPreferredScrollableViewportSize() {
                    return getPreferredSize();
                }
            };
            JOptionPane.showMessageDialog(null, new JScrollPane(table));
            marshal(model, OUTPUT_FILE);
    
        }
    
        private static void marshal(AutoModel model, String file) throws Exception {
            JAXBContext context = JAXBContext.newInstance(AutoModel.class);
            Marshaller marshaller = context.createMarshaller();
            File f= new File(file);
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(model, f);
        }
    
        private static AutoModel unmarshal(String file) throws Exception {
            JAXBContext context = JAXBContext.newInstance(AutoModel.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            AutoModel model = (AutoModel)unmarshaller.unmarshal(new File(file));
            return model;
        }
    }
    


    As far the the AutoModel goes, it only works for your first table. You will need to create another model for your repairs table. Also, the model, currently only offered read-only. You will need to add other functionality to say add a row and set individual values.

    Here are some resources to look at:


    NOTE: With the JAXB annotations above you can create the schema, and you you want to validate the xml against it, you could just set the schema when you unmarshal. Something like:

    private static AutoModel unmarshal(String file) throws Exception {
        JAXBContext context = JAXBContext.newInstance(AutoModel.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new File("src/table/autos.xsd"));
        unmarshaller.setSchema(schema);
        AutoModel model = (AutoModel)unmarshaller.unmarshal(new File(file));
        return model;
    }
    

    这篇关于如何使xml架构与JTable,xml java相关联(链接)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆